# Introduction

Last time I showed a simple algorithm for approximating . The third harmonic was -28.6 dBc, which is not good enough for high-quality sound synthesis.

In this post, I will derive a more accurate approximation polynomial.

# Method

The first change we will make in the algorithm is to split the domain into four pieces, instead of two, and exploit additional symmetry properties of the sine function. This well-known trick allows us to decrease the input range over which our polynomial must be accurate.

Now, we pick the order of the polynomial to use. Our previous algorithm used a second-order polynomial so we’ll try a third-order one here. The third-order polynomial has four coefficients, so we’ll be able to enforce four constraints:

.

## Setting constraints

To ensure the curve starts and ends at the correct amplitude, we should constrain the end points, i.e. and . These should be equal to and , respectively. This leads to the following constraints:

and .

Now, we still have two addition constraints we can set. We could either choose two additional points our polynomial must intersect, or we could set two constraints on the derivative of the polynomial, :

.

I chose to constrain the derivatives at the end points because it makes the piece-wise sin approximation continuous in the first derivative, which is desirable from a spectral point-of-view.

The derivative of is and we equate the derivative of the polynomial at and to it:

and .

## Solving for the coefficients

Given our constraints, we can now solve the system of equations. The system we must solve is:

.

.

.

,

The first and third equations give values for and . Using these values and simplifying leads to:

.

,

which, after some manipulation, gives:

and .

# How good is it?

The polynomial P(x) follows the curve (for ) very accurately:

As can be seen from the error graph below, the error is less than +/- 1.1% (with respect to full-scale):

The following graph shows the spectrum of the approximated sinusoidal wave. It was generated using an 65536-point FFT and 2129 sine wave periods:

The spectrum graph shows that the third harmonic, which is found at -46.9 dBc, is much lower than the simple sine approximation from the previous post.

# Show us the code!

/* A better sin(2*pi*x) approximation. The caller must make sure that the argument lies within [0..1). Note: this can be optimized further by using Horner's rule for evaluating polynomials, see: http://en.wikipedia.org/wiki/Horner%27s_method Omitted here for clarity. */ float sin_poly(float x) { const float a3 = -27.469f; const float a2 = -2.2655f; const float a1 = 6.2832f; return a3*x*x*x + a2*x*x + a1*x; } float better_sin(float x) { if (x > 0.75f) return -sin_poly(1.0f - x); else if (x > 0.5f) return -sin_poly(x - 0.5f); else if (x > 0.25f) return sin_poly(0.5f - x); else return sin_poly(x); };

# Conclusion

This approximation is quite good and it has a spectral purity approximately equivalent to a 7-bit signal.

If you want the approximation to be continuous in the first derivative, you need to set P′(1/4) = 0, but you do not need to constrain P′(0). Relaxing the unneeded constraint can lower the error to about 0.43%.