The Gibbs phenomenon


The Gibbs phenomenon is the name given to the oscillations observed in the Fourier series of a periodic function near to a step discontinuity. For example, the square wave defined by $f(x) = 1$ for $0 < x < 1$ and $f(x) = -1$ for $1 < x < 2$ (and repeating outside the range $(0,2)$ has the Fourier series expansion: $$ f(x) = \frac{4}{\pi}\sum_{n=1,3,5,\cdots} \frac{1}{n}\sin(n\pi x) $$

Plot $f(x)$ and its Fourier series expansion truncated at 20 terms on the same axes, (a) for $0 \le x \le 4$ and (b) zoomed in on a region exhibiting the Gibbs phenomenon.