TExcelTAD
Home › Differential Equations › Fourier Series

Fourier Series

A Fourier series writes a periodic function as a sum of sines and cosines. It is the tool that solves the heat and wave equations, and the idea behind audio compression, image formats and the FFT.

The series

For a function ff with period 2L2L:

f(x)∼a02+∑n=1∞(ancos⁡nπxL+bnsin⁡nπxL),f(x) \sim \frac{a_0}{2} + \sum_{n=1}^\infty \left(a_n\cos\frac{n\pi x}{L} + b_n\sin\frac{n\pi x}{L}\right),
an=1L∫−LLf(x)cos⁡nπxL dx,bn=1L∫−LLf(x)sin⁡nπxL dx.a_n = \frac{1}{L}\int_{-L}^{L} f(x)\cos\frac{n\pi x}{L}\,dx, \qquad b_n = \frac{1}{L}\int_{-L}^{L} f(x)\sin\frac{n\pi x}{L}\,dx.

The formulas come from orthogonality (Boundary Value Problems and Eigenfunctions): multiply the series by cos⁡mπxL\cos\frac{m\pi x}{L} and integrate; every term but one vanishes. It is the same as finding coordinates in an orthogonal basis by dot products (Orthogonal Projections and Fourier).

Example: the square wave

f(x)=−1f(x) = -1 on (−π,0)(-\pi, 0), 11 on (0,π)(0, \pi), extended with period 2π2\pi. It is odd, so all an=0a_n = 0, and

bn=2π∫0πsin⁡nx dx=2nπ(1−(−1)n)={4nπn odd0n evenb_n = \frac{2}{\pi}\int_0^\pi \sin nx\,dx = \frac{2}{n\pi}\big(1 - (-1)^n\big) = \begin{cases} \frac{4}{n\pi} & n \text{ odd} \\ 0 & n \text{ even} \end{cases}
f(x)=4π(sin⁡x+sin⁡3x3+sin⁡5x5+⋯ ).f(x) = \frac{4}{\pi}\left(\sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \cdots\right).
python · try it yourself
import numpy as np
from scipy.integrate import quad

f = lambda x: np.sign(x)
for n in range(1, 6):
    bn = quad(lambda x: f(x) * np.sin(n * x), -np.pi, np.pi, points=[0])[0] / np.pi
    print(f"b_{n} = {bn: .6f}   formula {4 / (n * np.pi) if n % 2 else 0: .6f}")
b_1 =  1.273240   formula  1.273240
b_2 =  0.000000   formula  0.000000
b_3 =  0.424413   formula  0.424413
b_4 = -0.000000   formula  0.000000
b_5 =  0.254648   formula  0.254648

Convergence and the Gibbs phenomenon

If ff is piecewise smooth, the series converges to f(x)f(x) wherever ff is continuous, and to the average f(x−)+f(x+)2\frac{f(x^-) + f(x^+)}{2} at a jump (here 0 at x=0x = 0). Near a jump, the partial sums overshoot by about 9 % of the jump height, no matter how many terms you take: the Gibbs phenomenon.

python · try it yourself
import numpy as np

x = np.linspace(1e-4, np.pi / 2, 200001)
for N in (11, 51, 201):
    S = 4 / np.pi * sum(np.sin(n * x) / n for n in range(1, N + 1, 2))
    print(f"N = {N:3d}: max of partial sum = {S.max():.4f}, value at pi/2 = {S[-1]:.4f}")
print("overshoot limit (2/pi) Si(pi) =", round(2 / np.pi * 1.851937052, 4))
N =  11: max of partial sum = 1.1813, value at pi/2 = 0.9473
N =  51: max of partial sum = 1.1791, value at pi/2 = 0.9878
N = 201: max of partial sum = 1.1790, value at pi/2 = 1.0032
overshoot limit (2/pi) Si(pi) = 1.179

The overshoot moves closer to the jump but does not shrink. Smooth functions behave much better: their coefficients decay quickly and the partial sums converge uniformly.

Coefficients decay with smoothness

  • A jump: coefficients like 1n\frac{1}{n} (square wave).
  • Continuous with a corner: like 1n2\frac{1}{n^2} (triangle wave, ∣x∣\lvert x\rvert).
  • Smooth and periodic: faster than any power.

This is why compression works: smooth parts of a signal need few coefficients.

Complex form

With eiθ=cos⁡θ+isin⁡θe^{i\theta} = \cos\theta + i\sin\theta the series becomes f(x)=∑n=−∞∞cneinπx/Lf(x) = \sum_{n=-\infty}^{\infty} c_ne^{in\pi x/L}, cn=12L∫−LLf(x)e−inπx/L dxc_n = \frac{1}{2L}\int_{-L}^L f(x)e^{-in\pi x/L}\,dx. The discrete Fourier transform (computed by the FFT) is this formula applied to sampled data:

python · try it yourself
import numpy as np

N = 256
x = 2 * np.pi * np.arange(N) / N
f = np.where(x < np.pi, 1.0, -1.0)            # one period of the square wave on [0, 2 pi)
c = np.fft.rfft(f) / N
b = -2 * c.imag                                # b_n = -2 Im(c_n)
print("b_1..b_5 from FFT:", b[1:6].round(4))
b_1..b_5 from FFT: [ 1.2732  0.      0.4242 -0.      0.2543]

The values are close to 4π≈1.2732\frac{4}{\pi} \approx 1.2732, 00, 43π≈0.4244\frac{4}{3\pi} \approx 0.4244, … The small differences come from sampling a discontinuous function.

In software

  • MP3/AAC audio and JPEG images store coefficients of cosine transforms and drop the small ones.
  • Synthesizers build sounds from harmonics (additive synthesis); the square wave's odd-only harmonics give it its hollow, clarinet-like sound.
  • numpy.fft and scipy.fft compute discrete Fourier coefficients in O(Nlog⁡N)O(N\log N) (Fourier Analysis & Signals).

Exercises

Exercise 1: Sawtooth

Find the Fourier series of f(x)=xf(x) = x on (−π,π)(-\pi, \pi).

Answer

x=2∑n=1∞(−1)n+1nsin⁡nxx = 2\sum_{n=1}^\infty \frac{(-1)^{n+1}}{n}\sin nx.

Solution

ff is odd, so an=0a_n = 0. bn=2π∫0πxsin⁡nx dx=2π⋅π(−1)n+1n=2(−1)n+1nb_n = \frac{2}{\pi}\int_0^\pi x\sin nx\,dx = \frac{2}{\pi}\cdot\frac{\pi(-1)^{n+1}}{n} = \frac{2(-1)^{n+1}}{n}.

Exercise 2: Value at a jump

What does the sawtooth series converge to at x=πx = \pi?

Answer
Solution

The periodic extension jumps from π\pi to −π-\pi there; the series converges to the average 0 (every term sin⁡nπ\sin n\pi is 0 anyway).

Exercise 3: A famous sum (code)

The series of ∣x∣\lvert x\rvert on (−π,π)(-\pi, \pi) is π2−4π∑n oddcos⁡nxn2\frac{\pi}{2} - \frac{4}{\pi}\sum_{n \text{ odd}}\frac{\cos nx}{n^2}. Evaluate at x=0x = 0 to find ∑n odd1n2\sum_{n \text{ odd}}\frac{1}{n^2}, and check numerically.

Answer

π28≈1.2337\frac{\pi^2}{8} \approx 1.2337.

Solution

At x=0x = 0: 0=π2−4π∑1n20 = \frac{\pi}{2} - \frac{4}{\pi}\sum\frac{1}{n^2}, so the sum is π28\frac{\pi^2}{8}.

python · try it yourself
import math
s = 0.0
for n in range(1, 200001, 2):
    s += 1 / n**2
print(round(s, 5), round(math.pi**2 / 8, 5))
1.2337 1.2337

Test yourself

For an odd function on (−L,L)(-L, L), the Fourier series contains…

  1. only cosines
  2. only sines
  3. only the constant
  4. both sines and cosines

At a jump discontinuity the Fourier series converges to…

  1. the left value
  2. the right value
  3. the average of the left and right values
  4. infinity

The Gibbs phenomenon is…

  1. the series diverging
  2. a persistent overshoot of about 9 % of the jump near a discontinuity
  3. rounding error
  4. the coefficients being zero