How to design a periodic function

338 Views Asked by At

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.

4

There are 4 best solutions below

1
On BEST ANSWER

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

0
On

Fattened curve using square root.

enter image description here

1
On

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)$.

6
On

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 $[-π,π]$.