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 with period :
The formulas come from orthogonality (Boundary Value Problems and Eigenfunctions): multiply the series by 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
on , on , extended with period . It is odd, so all , and
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 is piecewise smooth, the series converges to wherever is continuous, and to the average at a jump (here 0 at ). Near a jump, the partial sums overshoot by about 9 % of the jump height, no matter how many terms you take: the Gibbs phenomenon.
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 (square wave).
- Continuous with a corner: like (triangle wave, ).
- Smooth and periodic: faster than any power.
This is why compression works: smooth parts of a signal need few coefficients.
Complex form
With the series becomes , . The discrete Fourier transform (computed by the FFT) is this formula applied to sampled data:
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 , , , … 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.fftandscipy.fftcompute discrete Fourier coefficients in (Fourier Analysis & Signals).
Exercises
Exercise 1: Sawtooth
Find the Fourier series of on .
Answer
.
Solution
is odd, so . .
Exercise 2: Value at a jump
What does the sawtooth series converge to at ?
Answer
Solution
The periodic extension jumps from to there; the series converges to the average 0 (every term is 0 anyway).
Exercise 3: A famous sum (code)
The series of on is . Evaluate at to find , and check numerically.
Answer
.
Solution
At : , so the sum is .
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 , the Fourier series contains…
- only cosines
- only sines
- only the constant
- both sines and cosines
At a jump discontinuity the Fourier series converges to…
- the left value
- the right value
- the average of the left and right values
- infinity
The Gibbs phenomenon is…
- the series diverging
- a persistent overshoot of about 9 % of the jump near a discontinuity
- rounding error
- the coefficients being zero