Finding polynomial approximations of a sine wave I want to  approximate the sine wave given by $\sin\left(\pi x\right)$ by applying a polynomial waveshaper to a simple triangle wave, generated by the function

$$T\left(x\right)=1-4\left|\tfrac{1}{2}-\operatorname{mod}(\tfrac{1}{2}x+\tfrac{1}{4},\ 1)\right|$$

where $\operatorname{mod}(x, 1)$ is the fractional part of $x$:

$$ \operatorname{mod}(x, y) \triangleq  y \cdot \left( \left\lfloor \frac{x}{y}\right\rfloor - \frac{x}{y} \right) $$

A Taylor series could be used as a waveshaper.

$$S_1\left(x\right)=\frac{\pi x}{2}-\frac{\frac{\pi x}{2}^3}{3!}+\frac{\frac{\pi x}{2}^5}{5!}-\frac{\frac{\pi x}{2}^7}{7!}$$

Given the above functions, $S_1(T(x))$ will get us a decent approximation of a sine wave. But we need to go up to the 7th power of the series to get a reasonable result, and the peaks are a little low and won't have a slope of exactly zero.

Instead of the Taylor series, we could use a polynomial waveshaper following a few rules.


Must pass through -1,-1 and +1,+1.
Slope at -1,-1 and +1,+1 must be zero.
Must be symmetrical.


A simple function meeting our requirements:

$$S_2\left(x\right)=\frac{3x}{2}-\frac{x^3}{2}$$

The graphs of $S_2(T(x))$ and $\sin\left(\pi x\right)$ are pretty close, but not as close as the Taylor series. In between the peaks and zero-crossings they visibly deviate a bit. A heavier and more accurate function meeting our requirements:

$$S_3\left(x\right)=\frac{x(x^2-5)^2}{16}$$

This is probably close enough for my purposes, but I'm left wondering if another function exists which approximates the sine wave more closely, and is computationally cheaper. I have a pretty good grasp on how to find functions meeting the three requirements above, but I'm not sure how go about finding functions which meet those requirements and also most closely match a sine wave.

What methods exist for finding polynomials which mimic a sine wave (when applied to a triangle wave)?



To clarify, I'm not necessarily looking only for odd-symmetrical polynomials, although those are the most straightforward choice.

Something like the following function could also fit my needs:

$$S_4\left(x\right)=\frac{3x}{2}+\frac{x^2}{4}+\frac{x^4}{4}$$

This meets the requirements in the negative range, and a piecewise solution could be used to apply it to the positive range as well; for example

$$\frac{3x}{2}-\frac{P\left(x,2\right)}{4}-\frac{P\left(x,4\right)}{4}$$

where $P$ is the signed power function.

I'd also be interested in solutions using the signed power function to support fractional exponents, as this gives us another "knob to twist" without adding another coefficient.

$$a_0x\ +a_1P\left(x,\ p_1\right)$$

Given the right constants, this could potentially get very good accuracy without the heaviness of fifth- or seventh-order polynomials. Here's an example meeting the requirements described here using some hand-picked constants: $a_0=1.\overline{666}, a_1=-0.\overline{666}, p_1=2.5$.

$$\frac{5x-2P\left(x,\ \frac{5}{2}\right)}{3}$$  

In fact, those constants are very close to $\frac \pi 2$, and $1 - \frac \pi 2$, and $e$. Plugging those in gives something that looks extremely close to a sine wave.

$$\frac{\pi}{2}x\ +\left(1-\frac{\pi}{2}\right)P\left(x,e\right)$$

To put it another way, $x-\frac{x^e}{6}$ looks very close to $\sin(x)$ between 0,0 and π/2,1.  Any thoughts on the significance of this? Maybe a tool like Octave can help discover the "best" constants for this approach.
about a decade ago i did this for an unnamed music synthesizer company who had R&amp;D not too far from my condo in Waltham MA.  (can't imagine who they are.)  i don't have the coefficients.

but try this:

$$\begin{align}
f(x) &amp;\approx \sin\left(\tfrac{\pi}{2} x \right) \qquad \qquad \text{for }-1 \le x \le +1 \\
\\
     &amp;= \tfrac{\pi}{2} x (a_0 + a_1 x^2 + a_2 x^4) \\
\end{align}$$

this guarantees that $f(-x) = -f(x)$. 

To guarantee that $f'(x) \Big|_{x=\pm1} =0$ then

$$ f'(x) = \tfrac{\pi}{2}( a_0 + 3 a_1 x^2 + 5 a_2 x^4) $$

$$ a_0 + 3 a_1 + 5 a_2 = 0 \tag{1}$$

That's the first constraint.  To guarantee that $|f(\pm 1)| = 1$, then

$$ a_0 + a_1 + a_2 = \tfrac{2}{\pi} \tag{2}$$

That's the second constraint.  Eliminating $a_0$ and solving Eqs. (1) and (2) for $a_2$ in terms of $a_1$ (which is left to adjust):

$$ a_0 =  \tfrac{5}{2 \pi} - \tfrac{1}{2} a_1 $$

$$ a_2 = -\tfrac{1}{2 \pi} - \tfrac{1}{2} a_1  $$

Now you have only one coefficient, $a_1$, left to twiddle for best performance:

$$ f(x) = \tfrac{\pi}{2} x \left((\tfrac{5}{2 \pi} - \tfrac{1}{2} a_1) + a_1 x^2 -  (\tfrac{1}{2 \pi} + \tfrac{1}{2} a_1)x^4 \right) $$

This is the way I would twiddle $a_1$ for best performance for a sine wave oscillator.  I would adjust use the above and the symmetry of the sine wave about $x=1$ and place exactly one entire cycle in a buffer with a power of two number of points (say 128, i don't care) and run the FFT on that perfect cycle.

The FFT result bin 1 will be the strength of the sine and should be about $N/2$.  Now you can adjust $a_1$ to bring your 3rd harmonic distortion up and down.  I would start with $a_1 \approx \tfrac{5}{\pi} - 2$ so that $a_0 \approx 1$.  That's in bin 3 of the FFT results  But the 5th harmonic distortion (value in bin 5) will be consequential (it will go up as the 3rd harmonic goes down).  I would adjust $a_1$ so that the strength of the 5th harmonic level is equal to the 3rd harmonic level.  It will be around -70 dB from the 1st harmonic (as I recall).  That will be the nicest-sounding sine wave from a cheap, 3-coefficient, 5th-order, odd-symmetrical polynomial.

Someone else can write the MATLAB code.  How does that sound to you?