Skip to content

How do sine waves build a square wave?

Add harmonics one by one and watch a square, sawtooth or triangle wave take shape, with the Gibbs overshoot and the spectrum.

Wave

Partial sum and target

Key values as a table
xPartial sumWave
000
0.1250.99181
0.251.0631
0.3750.99181
0.57.796 × 10⁻¹⁶0

Harmonic amplitudes

Key values as a table
HarmonicAmplitude bₙ
11.273
30.4244
50.2546
70.1819
90.1415

Error of the approximation (RMS)

9%

25 sine waves, harmonics 1 to 49, follow the square wave within 9%; beside each jump the sum still overshoots by 8.96% of the jump: the Gibbs phenomenon.

The partial sum

Harmonics added
1, 3, 5, 7, 9
RMS error (amplitude 1)
0.201
Highest point of the sum
1.182 at x = 0.45
Overshoot beside the jump
9.12%

Add harmonics one by one and watch a square, sawtooth or triangle wave take shape, with the Gibbs overshoot and the spectrum.

How to use

  1. Choose a wave: square, sawtooth or triangle.
  2. Move the slider under the graph to add sine waves one at a time, from 1 to 100, and watch the sum close in on the dashed wave.
  3. Read how close the sum is, the overshoot beside each jump and, in the bars, how strong each harmonic is.

Square wave: error by number of harmonics

HarmonicsRemaining errorOvershoot
143.5%13.7%
325.9%9.4%
520.1%9.1%
1014.2%9%
259%9%
506.4%9%

Overshoot is how far the peak beside the jump rises above the final level, as a share of the jump.

How it works

Any repeating wave can be built by adding sine waves whose frequencies are whole multiples of the wave's own: the harmonics. This is a Fourier series. Each new harmonic corrects the sum a little more, so the curve hugs the target ever closer. It is how a synthesizer shapes a sound and how an MP3 stores music.

For a square wave of height 1 the recipe is (4/π)·[sin(2πx) + sin(6πx)/3 + sin(10πx)/5 + …]: only the odd harmonics, with amplitude 4/(πn) for harmonic n. The sawtooth uses every harmonic with amplitude 2/(πn), and the triangle only odd ones falling as 8/(π²n²), so it converges much faster. With 5 terms the square wave is matched within about 20 % RMS and with 100 within about 4.5 %; the error is found with Parseval's theorem from the amplitudes left out.

Next to a jump the sum always overshoots by about 9 % of the jump, however many terms are added: the Gibbs phenomenon. More terms squeeze the ripple closer to the jump but never shrink its height. Sources: MathWorld, "Fourier Series" and "Gibbs Phenomenon"; Paul's Online Math Notes, Lamar University, "Fourier Series".

Frequently asked questions

What does a negative harmonic amplitude mean?

It reverses that sine wave, equivalent to shifting its phase by half a cycle. Keep the sign when adding harmonics; using only their magnitudes would produce a different shape.

What value does the series give exactly at a jump?

It gives the midpoint between the values on either side. For the square and sawtooth waves here, that midpoint is zero.

Does 20% RMS error mean every point is within 20%?

No. RMS error summarizes the squared differences over a whole period, relative to the target wave’s RMS. Individual points can differ by much more, especially near a jump.

Can I analyse a recording or draw my own wave?

No. This explorer builds three preset waves from their known harmonics. It does not analyse audio or accept a custom waveform.

Does five terms mean the fifth harmonic?

Only for the sawtooth, which uses every harmonic. For the square and triangle waves, five non-zero terms reach the ninth harmonic.

Send feedback