Overview

This article presents 10 methods for generating sine waves in software, ranging from simple library calls to highly optimized recursive algorithms. The goal is to find the fastest approach for a given hardware, from floating-point FPUs to bare 8-bit microcontrollers.

1. Math Library — sin(x)

The simplest approach: call the library function. Fast to write, but the slowest to execute on hardware without a dedicated FPU or co-processor.

y = sin(x);   // x in radians

2. Table Look-up

Pre-compute and store sine values in a table, exploiting the four-fold symmetry of the sine function to reduce memory by 4×:

float sine[91], pi = 3.141592653;
for(int i = 0; i <= 90; i++)
    sine[i] = sin(pi/180 * i);

// Accessing all quadrants using symmetry:
y = sine[x];           //   0° ≤ x ≤  90°
y = sine[180 - x];     //  90° ≤ x ≤ 180°
y = -sine[x - 180];    // 180° ≤ x ≤ 270°
y = -sine[360 - x];    // 270° ≤ x ≤ 360°

3. Table Look-up + Linear Interpolation

Increase effective resolution without enlarging the table by interpolating between adjacent entries:

\[ y \approx \sin[x] + \bigl(\sin[x{+}1] - \sin[x]\bigr) \cdot \delta x \]

The maximum relative error for a table of N samples:

\[ \varepsilon_{\max} = \cos\!\left(\frac{\pi}{N}\right) \approx 1 - \frac{\pi^2}{2N^2} \]

For N = 256: error ≈ 0.03%. For N = 23: error < 1%. In practice, N = 32 (one quadrant) provides excellent accuracy.

4. Trigonometric Identity Method

Generate a sequence of equally-spaced sine values using the angle addition formulas. Given starting angle \(a\) and step \(b\):

\[ \sin(a + b) = \sin(a)\cos(b) + \cos(a)\sin(b) \]
\[ \cos(a + b) = \cos(a)\cos(b) - \sin(a)\sin(b) \]
s1 = sin(a);   c1 = cos(a);
sd = sin(b);   cd = cos(b);   // Precomputed constants

for(i = 0; i < N; i++) {
    temp = s1*cd + c1*sd;    // sin(a + i*b)
    c1   = c1*cd - s1*sd;    // cos(a + i*b)
    s1   = temp;
}  // 4 multiplications + 2 additions per step

5. Goertzel Algorithm (Most Efficient for Sine Only)

Requires only one multiplication per step by using a second-order recurrence:

\[ \sin\bigl(a + nb\bigr) = 2\cos(b)\cdot\sin\bigl(a+(n-1)b\bigr) - \sin\bigl(a+(n-2)b\bigr) \]

Derivation: setting \(x = -1\), \(y = 2\cos(b)\) in the recurrence gives:

\[ \boxed{s_n = 2\cos(b)\cdot s_{n-1} - s_{n-2}} \]
c  = 2 * cos(b);
s0 = sin(a + b);
s1 = sin(a + 2*b);

for(i = 0; i < N; i++) {
    s  = c * s1 - s0;   // 1 multiply + 1 subtract
    s0 = s1;
    s1 = s;
}
Why this is so efficientThe "x = −1" result reduces a multiplication to a sign change. Only one multiplication (by 2·cos(b)) is needed per sample — vs. 4 for the trigonometric identity method. This is the Goertzel algorithm, also used for tone detection (DTMF).
Goertzel recurrence — generating sine samples
s_{n-2} s_{n-1} s_n s_n = 2cos(b)·s_{n-1} − s_{n-2}
Each new sample (yellow) is computed from the two previous samples (green) using only one multiply. Accumulation error grows slowly — acceptable for short sequences.

6. Taylor Series

The Taylor series around \(x = 0\):

\[ \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \]

Using Horner's rule for efficient evaluation (avoids computing high powers of x):

\[ \sin(x) \approx x\!\left(1 - \frac{x^2}{6}\!\left(1 - \frac{x^2}{20}\!\left(1 - \frac{x^2}{42}\right)\!\right)\!\right) \]
Limit range to |x| ≤ π/2The Taylor series converges for all x, but many terms are needed as x approaches π/2. Use periodicity and symmetry to fold the input into [−π/2, π/2] before evaluating.

7. Padé Rational Approximation

A rational function (ratio of two polynomials) that matches the Taylor series to higher order with fewer terms:

\[ \sin(x) \approx x \cdot \frac{1 + n_1 x^2 + n_2 x^4 + n_3 x^6}{1 + d_1 x^2 + d_2 x^4 + d_3 x^6} \]

Coefficients (Padé [6/6]):

NumeratorValueDenominatorValue
n₁−325523/2283996d₁18381/761332
n₂34911/7613320d₂1261/4567992
n₃479249/11511339840d₃2623/1644477120

Method Comparison

MethodMultiplies/sampleMemoryBest for
Library sin()~manyComputers with FPU
Table lookup0HighFast MCUs with Flash
Table + interpolation1–2LowAccuracy + speed balance
Trig identity4Very lowConsecutive samples
Goertzel1Very low8-bit MCUs, consecutive
Taylor series3–5Very lowArbitrary angles
Padé6–8LowHigh accuracy, FPU