Toolyard

Fourier Series Explained: Building Waves From Sines

The idea

Any signal that repeats with period T can be written as a sum of sine and cosine waves whose frequencies are whole multiples of the basic frequency f = 1 ÷ T:

f(t) = a0 ÷ 2 + Σ (an cos nωt + bn sin nωt), with ω = 2πf

The terms are the harmonics: n = 1 is the fundamental, n = 2 the second harmonic at twice the frequency, and so on. The coefficients are found by multiplying the signal by each sine or cosine and averaging over a period: bn = (2 ÷ T) ∫ f(t) sin(nωt) dt. Joseph Fourier introduced the idea in 1807 to solve the equation for heat flow; it now underlies audio and image compression, radio, spectroscopy and the analysis of anything that vibrates.

Three standard waves

For waves that swing between −1 and +1 and are odd (only sines are needed):

  • Square wave: bn = 4 ÷ (nπ) for odd n, zero for even n. Amplitudes 1.273, 0.424, 0.255, 0.182, …
  • Sawtooth: bn = −2 ÷ (nπ) for every n (rising ramp). All harmonics, falling as 1 ÷ n.
  • Triangle: bn = 8(−1)(n−1)/2 ÷ (n²π²) for odd n. Falling as 1 ÷ n², much faster.

The rule behind these: a signal with jumps has coefficients falling like 1 ÷ n; one that is continuous but has corners, like 1 ÷ n²; smoother still, faster. Smooth signals need few terms; sharp ones need many.

A worked example

Add the first three non-zero terms of the square wave at its midpoint, ωt = π ÷ 2, where the wave is +1:

  1. sin(π ÷ 2) = 1, sin(3π ÷ 2) = −1, sin(5π ÷ 2) = 1.
  2. 1.273 × 1 + 0.424 × (−1) + 0.255 × 1 = 1.104.

Five terms give 1.273 − 0.424 + 0.255 − 0.182 + 0.141 = 1.063; the partial sums wobble around 1 and close in slowly, as the 1 ÷ n coefficients predict.

The Gibbs phenomenon

Near a jump the partial sums overshoot, and the overshoot does not shrink as terms are added. It tends to about 8.95% of the jump on each side: a square wave jumping from −1 to +1 (a jump of 2) peaks at about 1.18 however many harmonics are used. The ripple gets narrower and squeezes toward the jump, so the error averaged over a period does go to zero, but the maximum error does not. This matters in practice: cutting off high frequencies (a low-pass filter, or JPEG compression) makes ringing next to sharp edges.

Power: Parseval's theorem

The average of f2 over a period equals the sum of the harmonics' powers: mean(f2) = Σ bn2 ÷ 2 (for a sine-only series). For the square wave, mean(f2) = 1, and the fundamental alone holds (4 ÷ π)2 ÷ 2 = 81% of it; the first five odd harmonics hold 96%. This is why a square wave sounds like its fundamental pitch with a bright, buzzy color: the upper harmonics carry the remaining few percent.

Epicycles and the discrete Fourier transform

Each term c einωt is a point going round a circle of radius |c| at n turns per period. Adding the terms tip to tail is a chain of circles, each spinning at its own speed: epicycles, the same device Ptolemy used for planets. For a closed outline in the plane, write its points as complex numbers z = x + iy, sample M of them evenly, and take the discrete Fourier transform:

ck = (1 ÷ M) Σj zj e−2πijk/M

Each ck is one circle; with all M of them the chain retraces the outline exactly, and with only the largest few it draws a smoothed version. Corners and fine detail live in the small, fast circles.

Using the simulation

In the Fourier Series Simulator, choose a wave and the number of terms. The chain of circles spins, the pen's height is the partial sum, and the curve scrolls right over the exact wave in green; the chart is the spectrum. Things to try:

  • Square wave, 1 term then 2, 5, 20 and 100: the overshoot at the jump stays near 9% while the ripple narrows.
  • Triangle with 3 terms: already very close, because its coefficients fall as 1 ÷ n².
  • Tick the sound and add terms to the square wave: the pitch stays the same while the tone grows from a pure flute-like sine to a reedy buzz.
  • Draw your own shape, then slide the number of terms down to 3 or 4: the drawing melts into an ellipse-like loop.

What the model assumes

  • Ideal, perfectly periodic waves of amplitude 1, with exact coefficients; nothing is measured or sampled for the waves.
  • Sine-only series for the three waves, chosen to be odd functions; a shifted wave would also need cosines.
  • Shapes resampled to 256 evenly spaced points along the outline before the transform, closed by joining the last point to the first.
  • Sound from the same coefficients through the browser's Web Audio periodic wave, which normalizes the loudness; it is played at the pitch you set, with harmonics above about 20 kHz inaudible.

Edge cases

  • One term: every wave becomes a pure sine of amplitude b1; the square wave's is 4 ÷ π = 1.27, taller than the wave itself.
  • At the jump itself every partial sum of the square wave is exactly 0, the average of the two sides.
  • A drawing that crosses itself or has a gap is still treated as one closed loop; the transform fits whatever path the points make.
  • Very high harmonics in the sound: at 880 Hz the 100th harmonic would be 88 kHz, far above hearing and above what the audio hardware can play; the browser drops them.

Where it stops being right

  • Signals that do not repeat have no Fourier series; they need the Fourier transform, a continuous spread of frequencies. Short bursts of sound have spread-out spectra, which is why a click has no pitch.
  • Sampled data. A real signal measured at a finite rate cannot represent frequencies above half that rate (the Nyquist limit); higher ones fold back as false lower frequencies (aliasing).
  • Real instruments are not ideal waves: their harmonics are not exactly whole multiples (piano strings are stiff), they change over the course of a note, and the attack carries much of the character, as the Instrument Tuner shows when it reads a real note.
  • Pathological functions. There are continuous functions whose Fourier series diverge at some points; for anything drawn or recorded, this never matters in practice.

Related tools

The Standing Wave Simulator shows the harmonics a string or pipe can hold, and the Tone Generator plays sine, square, sawtooth and triangle waves at any frequency to compare by ear.

Tools in this guide

More guides