Fourier series of a square wave

Slide the number of harmonics, or press Animate and watch. The grey steps are the target, a square wave that is +1 for half a period and −1 for the other half. The blue curve is the sum of the first N odd sine waves, (4/π)(sin x + sin 3x/3 + sin 5x/5 + …). The flat parts get flatter and the corners get sharper, but look at the ear that sticks up just after each jump. Tick "zoom on the corner" and keep sliding. The ear gets narrower. It does not get shorter.

Mission. Set N = 5 and read the peak height in the readout. Predict the peak for N = 50. Then test. By what factor did the width of the ear change, and by what factor did its height change?

Answer

The peak is about 1.18 at N = 5 and still about 1.18 at N = 50. The ear sits at x = π/(2N), so its width shrank by a factor of 10, but its height stays at 1.179, which is 1 plus 9% of the jump (the jump is 2, from −1 to +1). This is the Gibbs phenomenon: the partial sums converge to the square wave at every point away from the jump, but never uniformly. Extension: the area of the ear does shrink. Why does that not contradict the overshoot staying put?

What's going on

A square wave is not a sum of finitely many sines, but it is a limit of them. The Fourier coefficients come from integrating the square wave against sin kx: every even harmonic integrates to zero by symmetry, and the odd ones give 4/(πk). So the N-th partial sum uses frequencies 1, 3, 5, …, 2N − 1, and the highest frequency present sets how sharp the corners can be: the first peak after the jump lands at x = π/(2N).

The height of that peak is a different matter. Write the sum as an integral and let N grow, and the peak height tends to (2/π) Si(π) ≈ 1.179, where Si is the sine integral. That is 0.179 above the target, about 8.9% of the jump of 2. Nothing you do with more terms removes it. Pointwise, every fixed x off the jump does converge to ±1, because the ear eventually moves inside any gap you name. The convergence just is not uniform.

This matters in practice. A sharp edge in a signal, an image or a sound passed through any filter that cuts off high frequencies will ring near the edge. The ringing around sharp edges in an over-compressed JPEG is the same mathematics.

Sheet: Fourier coefficients by integration. Related demo: Fourier series, for other waveforms.