The Idea of Fourier Series
In 1807 Joseph Fourier claimed that any periodic function, even one with corners and jumps, can be written as a sum of sines and cosines. Mathematicians were sceptical, and making the claim precise took a century, but the idea is now everywhere. This lesson shows the claim at work: a square wave rebuilt from smooth sinusoids, and the linear-algebra picture that makes it natural.
The claim
A function with period (fundamental frequency ) can be written as
a sum of the harmonics , each with a complex weight (its amplitude and phase). For a real the terms pair up into real cosines and sines. The weights are found by an integral (Computing Fourier Coefficients).
Building a square wave
The square wave that is on the first half of each period and on the second half has the series
with only odd harmonics. Adding more terms gets closer:
import numpy as np
t = np.linspace(0, 1, 2000, endpoint=False)
square = np.where(t < 0.5, 1.0, -1.0)
def partial_sum(K):
s = np.zeros_like(t)
for k in range(1, K + 1, 2): # odd harmonics 1, 3, 5, ...
s += 4 / np.pi * np.sin(2 * np.pi * k * t) / k
return s
for K in (1, 3, 9, 49, 199):
err = np.sqrt(np.mean((square - partial_sum(K)) ** 2))
print(f"harmonics up to {K:3d}: RMS error {err:.4f} value at t = 0.25: {partial_sum(K)[500]:.4f}")harmonics up to 1: RMS error 0.4352 value at t = 0.25: 1.2732
harmonics up to 3: RMS error 0.3152 value at t = 0.25: 0.8488
harmonics up to 9: RMS error 0.2010 value at t = 0.25: 1.0631
harmonics up to 49: RMS error 0.0902 value at t = 0.25: 1.0127
harmonics up to 199: RMS error 0.0465 value at t = 0.25: 0.9968
The RMS error keeps shrinking, and at , in the middle of the flat part, the sum converges to 1. Near the jumps something stubborn happens, the Gibbs phenomenon.
The linear-algebra picture
On one period, functions form a vector space with the inner product . The harmonics are orthonormal: if and 0 otherwise. A Fourier series is just the expansion of in this orthonormal basis, and each coefficient is a projection, , exactly as in Projections and Fourier.
import numpy as np
N = 4000
t = np.arange(N) / N # one period, T = 1
e = lambda k: np.exp(2j * np.pi * k * t)
inner = lambda f, g: np.mean(f * np.conj(g)) # (1/T) * integral, by the rectangle rule
G = np.array([[inner(e(k), e(m)) for m in range(-2, 3)] for k in range(-2, 3)])
print("Gram matrix of e_-2..e_2 (rounded):")
print((np.round(G.real, 10) + 0.0).tolist())Gram matrix of e_-2..e_2 (rounded):
[[1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0]]
The Gram matrix is the identity: the harmonics are orthonormal.
In software
- Synthesizers do the forward direction (additive synthesis: add harmonics to build a timbre); spectrum analyzers do the reverse.
- The discrete version of this expansion, the DFT, is what
np.fft.fftcomputes.
Exercises
Exercise 1: Which harmonics?
The square-wave series uses only with odd . Why no cosines?
Answer
The square wave (as defined) is an odd function, so only sines appear.
Solution
Shifted so it is on and on , the square wave satisfies . Cosines are even, so their coefficients (inner products of an odd function with an even one) vanish. The even harmonics vanish for a different symmetry, half-wave antisymmetry .
Exercise 2: A finite series
What is the Fourier series of ?
Answer
, that is .
Solution
and . Only and are nonzero; a trigonometric polynomial is its own Fourier series.
Exercise 3: Leibniz from the square wave (code)
Evaluate the square-wave series at and deduce a series for .
Answer
.
Solution
At , is for , and the square wave equals 1, so .
import numpy as np
k = np.arange(200000)
print(4 * np.sum((-1.0) ** k / (2 * k + 1)), np.pi)3.1415876535897924 3.141592653589793
The Leibniz series converges slowly: 200000 terms give only about 5 correct digits.
Test yourself
A Fourier series represents a periodic function as…
- a polynomial
- a sum of harmonics
- a product of sines
- a power series in
The harmonics on one period are…
- linearly dependent
- orthonormal
- all equal
- only orthogonal for even
The square wave contains…
- all harmonics
- only odd harmonics
- only even harmonics
- only the fundamental