2
$\begingroup$

I am a colorist in post production and I am working on tools that generally speaking transform colors. For this purpose I am looking to design a function that accommodates the following points.

$$(-\pi, 0);\; (-\frac{2\pi}{3}, -1);\; (-\frac{\pi}{3}, -0.5);\; (0, 0);\; (\frac{\pi}{3}; 0.5);\; (\frac{2\pi}{3}, 1); (\pi, 0)$$

The function should be continuous, easy to compute and as precise as possible. From the given points one can see that I am looking for something halfway between a sinus and a sawtooth curve.

This is what I found so far:

$$f(x)=\sin(x-0.57\sin(x))$$

It is relatively close but not quite as precise as I would want it to be and I don't like the little bumps in the straight part of the curve between $-\frac{2\pi}{3}$ and $\frac{2\pi}{3}$.

Any help is greatly appreciated.

$\endgroup$
1
  • 1
    $\begingroup$ Any periodic function can be written as a Fourier series. Here, you should be able to get: $$f(x)=a\sin(x)+b\sin(2x),$$ for some pair of values $a,b.$ You can solve for $a,b.$ $\endgroup$ Aug 5 at 20:17

4 Answers 4

6
$\begingroup$

This is related to the Fourier series of a function. Every "nice" function of period $2\pi$ can be written as a (possibly infinite) linear combination of the functions $\sin(kx)$ and $\cos(kx).$

Since your function is odd ($f(-x)=-f(x)$) you should be able to skip the cosines and write your function as:

$$f(x)=\sum_{k=1}^{\infty}a_k\sin(kx).$$

Here, we really only need two terms to match your datapoints:

$$f(x)=a\sin(x)+b\sin(2x)$$

We automatically get $f(0)=f(\pi)=f(-\pi)=0.$ So we need to match $f(\pi/3)$ and $f(2\pi/3).$ Here we need $\sin(\pi/3)=\sin(2\pi/3)=\frac{\sqrt3}2$ and $\sin(4\pi/3)=-\frac{\sqrt3}2.$

So you need $$0.5=\frac{\sqrt3}2(a+b)\\1=\frac{\sqrt3}2(a-b)$$

Or $a+b=1/\sqrt{3}, a-b=2/\sqrt3.$ This gives $$a=\frac{ \sqrt3}2,b=-\frac{\sqrt3}{6}.$$

So you get a function $$f(x)=\frac{\sqrt3}6(3\sin(x)-\sin(2x)).$$

This gives a graph that looks like:

Graph of two term function

If you want to match more datapoints, you will need more terms, and if the addition datapoints do not ensure the function is odd, then you'll need to add terms of the form $\cos(kx),$ too.

Another condition you might want is for $f(2\pi/3)$ to be the maximum. As it stands, $f(2)>1.$ You will need an addition term to get this condition.

If you want $1$ to be the absolute maximum, you get:

$$f(x)=\frac{\sqrt 3}{6}\left(3\sin x-\sin 2x +\frac16\sin 3x\right)$$

This also gives a slightly more linear look the graph on $[-2pi/3,2\pi/3]$: Graph of three term function

$\endgroup$
1
  • $\begingroup$ Just really beautiful. Exactly what I was looking for. You read my mind there with adding that extra condition. Genius! Thank you!! $\endgroup$
    – Foadgman
    Aug 5 at 22:33
1
$\begingroup$

Trigometric functions are extremely slow to compute compared to simple ratios of polynomials, so here is a fast alternative:

$f(x/π) = \dfrac{x-x^3}{x^4-\frac{11}9·x^2+\frac{58}{81}}$.

  Graph of $f(x/π)$ against $x$:
    graph passing through (0,0),(1/3,1/2),(2/3,1),(1,0)

It was easy to find, starting from the general shape when the coefficients are all $1$, and then solving for the necessary coefficients to make a fit.

Furthermore, nothing about "colours" indicates that you need your function to be periodic, but if you do want a periodic function, you could simply repeat the part of this $f$ over the range $[-π,π]$.

$\endgroup$
6
  • $\begingroup$ Hey user21820! Another alternative, great! True, the color context didn't provide much information. Generally I am working on the rotation of hues. They typically exist between 0° and 360° but could also be negative or greater then 360°. So I am leaning towards a periodic function, but if the function covers the range -4pi to 4pi we should be robust enough for all real world scenarios. Also, since I am neither a good mathematician nor a good programmer your remark about computational efficiency is very relevant. $\endgroup$
    – Foadgman
    Aug 6 at 10:27
  • $\begingroup$ @Foadgman: Well, as per my last paragraph, it's trivial to duplicate the middle part to make a very smooth periodic version. As for efficiency, you can just try running a few million calculations to check for yourself how many function evaluations you can perform per second (to confirm what I said about mine being much more efficient). $\endgroup$
    – user21820
    Aug 6 at 11:03
  • $\begingroup$ Yeah, trivial, right. Would you mind elaborate on how to repeat the function to the left and right? How would it look if I would like to call a value for, say, 4/3pi ? $\endgroup$
    – Foadgman
    Aug 6 at 11:36
  • $\begingroup$ If it's within [-π,π], you're done. If not, add or subtract 2π (or a multiple of 2π) to make it within range, and then you're done. Not trivial? $\endgroup$
    – user21820
    Aug 6 at 11:41
  • $\begingroup$ Sure, I will check it out. The only possible issue I see is that at the stitching points the angle of the curve may change abruptly and leave an undesirable bump. But it may or may not be an issue. $\endgroup$
    – Foadgman
    Aug 6 at 11:51
0
$\begingroup$

Fattened curve using square root.

enter image description here

$\endgroup$
0
$\begingroup$

From the given points one can see that I am looking for something halfway between a sinus and a sawtooth curve.

I got intrigued by this part of the question. Here is a family of curves that interpolate between a sinusoid and a sawtooth: $$f_t(x)=\frac1t\tan^{-1}\left(\frac{t\sin x}{1+t\cos x}\right).$$ When $t=1$, you get a sawtooth. When $t\to0$, you get a sinusoid. So let's take $t=1/2$:

plot of $f_{1/2}(x)$ for $x\in[-2\pi,2\pi]$

Amazingly, $f_{1/2}(x)$ is also maximized at precisely* $x=2\pi/3$! However, its value there is $\pi/3$, not $1$. Not a problem: just use $3f_{1/2}(x)/\pi$ instead.


*More generally, $f_t(x)$ is maximized at $x=\cos^{-1}(-t)$.

$\endgroup$
1
  • $\begingroup$ Thanks Rahul, this is unbeatable in terms of smoothness. However, I can't figure out how to fit it to my data set. What I need is: 𝑓(𝜋/3) = 2*𝑓(2𝜋/3) $\endgroup$
    – Foadgman
    Aug 6 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.