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.
What you enter stays on your device while you use this tool. Opening a shared link sends the settings in its address to HowToolbox to prepare the page.
Partial sum and target
Key values as a table
| x | Partial sum | Wave |
|---|---|---|
| 0 | 0 | 0 |
| 0.125 | 0.9918 | 1 |
| 0.25 | 1.063 | 1 |
| 0.375 | 0.9918 | 1 |
| 0.5 | 7.796 × 10⁻¹⁶ | 0 |
Harmonic amplitudes
Key values as a table
| Harmonic | Amplitude bₙ |
|---|---|
| 1 | 1.273 |
| 3 | 0.4244 |
| 5 | 0.2546 |
| 7 | 0.1819 |
| 9 | 0.1415 |
Error of the approximation (RMS)
43.5%
One sine wave follows the square wave within 43.5%; beside each jump it overshoots by 13.7% of the jump.
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
- Choose a wave: square, sawtooth or triangle.
- 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.
- 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
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".
Sources
- Paul's Online Math Notes (Lamar University): Fourier SeriesUsed for: formula
- Wolfram MathWorld: Fourier Series, Square WaveUsed for: formula
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.
Next you might need
More in Math & numbers
- Percentage calculator, private, runs on your device
- A/B test significance calculator, private, runs on your device
- Area of shapes, private, runs on your device
- Average calculator, private, runs on your device
- Chi-square test calculator, private, runs on your device
- Circle calculator, private, runs on your device





