The idea: a periodic function is a sum of harmonics

A function that repeats every 2π can be written as a constant plus waves that fit the period a whole number of times: cos x and sin x once, cos 2x and sin 2x twice, and so on. Wave n is the n-th harmonic. The sum is the Fourier series:

f(x) = a₀/2 + a₁ cos x + b₁ sin x + a₂ cos 2x + b₂ sin 2x + …

Cutting the sum off after harmonic N gives the partial sum SN(x), the blue curve on the page. Each Add Harmonic works out one more coefficient and adds one more wave.

Left: three sine waves, 1.27 sin x, 0.42 sin 3x and 0.25 sin 5x, drawn on top of each other. Right: their sum, a wavy curve that already follows the square wave that is −1 on the left half and +1 on the right half
Three harmonics of the square wave (left) add up to a curve that is already close to the square wave (right).

Where a coefficient comes from

Two different harmonics are orthogonal: the product sin(mx)·sin(nx) has zero area over one period when m ≠ n, and the same holds for cosines and for a sine against a cosine. So if you multiply f by sin nx and take the area, every harmonic except the n-th one drops out, and what is left is π·bn:

bₙ = (1/π) ∫ f(x)·sin(nx) dx      aₙ = (1/π) ∫ f(x)·cos(nx) dx      (over −π … π)

That is step 1 of every Add Harmonic: the purple curve is f(x)·sin(nx), green strokes are area above the axis, red strokes area below. For the square wave and n = 1 the product is |sin x|, all green, with area 4, so b1 = 4/π = 1.2732. For n = 2 every green patch has a red twin and the area is exactly 0 (Demo 3: why no even harmonics).

Two plots over −π to π. Left: the square wave times sin x is |sin x|, two green humps, net area 4, so b₁ = 4/π. Right: the square wave times sin 2x has four humps, two green and two red of the same size, net area 0, so b₂ = 0
The coefficient is the net area of f·sin nx divided by π: it is 4/π for n = 1 and 0 for n = 2 ✓.

The three series on the page

functionsymmetrycoefficientsseries
square wave, sign xodd: sine terms onlybn = 4/(nπ), odd n(4/π)(sin x + sin 3x/3 + sin 5x/5 + …)
sawtooth, x/πodd: sine terms onlybn = ±2/(nπ), every n(2/π)(sin x − sin 2x/2 + sin 3x/3 − …)
triangle, |x|/πeven: cosine terms onlya0/2 = 1/2, an = −4/(n²π²), odd n1/2 − (4/π²)(cos x + cos 3x/9 + …)

An odd function (f(−x) = −f(x)) times a cosine is odd, so its area is 0: no cosine terms. An even function times a sine is odd too: no sine terms. The square wave also has a half-wave symmetry, f(x + π) = −f(x), which kills every even harmonic. a0/2 is the average height of f: 0 for the two odd functions, 1/2 for the triangle (the orange bar 0).

How fast the coefficients shrink

The coefficient chart shows the rule: a function with a jump has coefficients that shrink like 1/n; a function that is continuous but has corners shrinks like 1/n²; each extra derivative that is smooth gains one more power of n. So the triangle converges much faster: at N = 9 its RMS error is 0.0037, against 0.2010 for the square wave (Demo 5: triangle).

Bar charts of the coefficient size for n = 1, 3, 5, 7, 9, 11. Square wave: 1.27, 0.42, 0.25, 0.18, 0.14, 0.12, shrinking slowly like 1/n. Triangle: 0.41, 0.045, 0.016, 0.008, 0.005, 0.003, almost gone after n = 3, like 1/n²
A jump gives coefficients that shrink like 1/n; a corner only, like 1/n² — far fewer harmonics are needed.

The Gibbs overshoot

Next to a jump, SN overshoots. For the square wave the peak is 1.2732 with one harmonic, 1.2004 with N = 3, 1.1823 with N = 9 and 1.1790 with N = 99: it never goes below 1.1790. The overshoot of 0.179 is 8.95% of the jump from −1 to +1. More harmonics only squeeze the bump towards the jump: its peak sits at x = π/(N+1). This is the Gibbs phenomenon (Demo 2: Gibbs). The sawtooth has the same limit, 1.179, but reaches it more slowly because the function itself slopes up to the jump (Demo 6). The triangle has no jump and no overshoot: its peak creeps up to 1.

Nsquare: peaksquare: RMS errorsawtooth: peaktriangle: peak
11.27320.43520.63660.9053
91.18230.20101.07730.9798
191.17980.14231.12860.9899
991.17900.06371.16900.9980

The error still goes to zero: Parseval

Both things are true at once: the peak stays 9% too high, and SN gets closer to f. The table's last column is the RMS error, the square root of the average of (f − SN)² over a period. Parseval's identity gives it without an integral: mean of f² = (a₀/2)² + ½Σ(an² + bn²), so the squared error is whatever the harmonics so far have not yet accounted for. It shrinks to 0 because the bump gets narrower, even though it does not get lower. At every fixed x away from the jump SN(x) converges to f(x); at the jump itself it gives the midpoint, SN(0) = 0 for the square wave.

The epicycles

an cos nx + bn sin nx is the height of a point on a circle of radius √(an² + bn²) that turns n times per period. Put each circle on the tip of the one before (starting at height a₀/2) and the height of the last tip is SN(x*), carried to the red dot on the blue curve. Spin moves x* once round the period (Demo 1, Demo 4). The page draws the five biggest circles and the rest of the harmonics as one thin line, because their circles are only a few pixels wide.

See also Fourier Series and the FFT for complex epicycles that draw a shape and the discrete transform, Taylor Series for the other famous series (powers of x instead of harmonics, good near one point instead of over a whole period), Sine Waves: What a, b, c, d Do for one harmonic's amplitude and frequency, The Unit Circle for why a turning point traces a sine, and Standing Waves and Wave Interference for harmonics and sums of waves in physics.

The ringing next to a sharp edge in a low-quality JPEG is this same Gibbs overshoot: see JPEG Compression (Demo 5).

A Fourier series whose coefficients shrink too slowly can be continuous but have no derivative anywhere: see The Weierstrass Function: Continuous Everywhere, Differentiable Nowhere.

The same circles can draw a closed shape in the plane, one circle per term of its discrete Fourier transform: see Fourier Epicycles: Drawing a Shape with Spinning Circles.

What the page leaves out

Periods other than 2π (replace nx by 2πnx/T), the complex form Σ cneinx, and functions that are not piecewise smooth, whose series can fail to converge at some points. Ways to remove the Gibbs overshoot: averaging the partial sums (Fejér) or σ-factors (Lanczos). The epicycles move between 24 positions per period in straight lines, so the fast circles jump. Sampled signals and the FFT are on the FFT page.