TExcelTAD
Home › Fourier Analysis & Signals › The Idea of Fourier Series

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 TT (fundamental frequency f0=1/Tf_0 = 1/T) can be written as

f(t)=∑k=−∞∞ck e2πikt/Tf(t) = \sum_{k=-\infty}^{\infty} c_k \, e^{2\pi i k t / T}

a sum of the harmonics e2πikf0te^{2\pi i k f_0 t}, each with a complex weight ckc_k (its amplitude and phase). For a real ff 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 +1+1 on the first half of each period and −1-1 on the second half has the series

sq⁡(t)=4π(sin⁡(2πt)+sin⁡(6πt)3+sin⁡(10πt)5+⋯ )\operatorname{sq}(t) = \frac{4}{\pi}\left( \sin(2\pi t) + \frac{\sin(6\pi t)}{3} + \frac{\sin(10\pi t)}{5} + \cdots \right)

with only odd harmonics. Adding more terms gets closer:

python · try it yourself
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 t=0.25t = 0.25, 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 ⟨f,g⟩=1T∫0Tf(t)g(t)‾ dt\langle f, g \rangle = \frac{1}{T}\int_0^T f(t)\overline{g(t)}\,dt. The harmonics ek(t)=e2πikt/Te_k(t) = e^{2\pi i k t / T} are orthonormal: ⟨ek,em⟩=1\langle e_k, e_m \rangle = 1 if k=mk = m and 0 otherwise. A Fourier series is just the expansion of ff in this orthonormal basis, and each coefficient is a projection, ck=⟨f,ek⟩c_k = \langle f, e_k \rangle, exactly as in Projections and Fourier.

python · try it yourself
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.fft computes.

Exercises

Exercise 1: Which harmonics?

The square-wave series uses only sin⁡(2πkt)\sin(2\pi k t) with odd kk. Why no cosines?

Answer

The square wave (as defined) is an odd function, so only sines appear.

Solution

Shifted so it is +1+1 on (0,1/2)(0, 1/2) and −1-1 on (−1/2,0)(-1/2, 0), the square wave satisfies f(−t)=−f(t)f(-t) = -f(t). 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 f(t+1/2)=−f(t)f(t + 1/2) = -f(t).

Exercise 2: A finite series

What is the Fourier series of f(t)=cos⁡2(2πt)f(t) = \cos^2(2\pi t)?

Answer

12+14e4πit+14e−4πit\tfrac{1}{2} + \tfrac{1}{4}e^{4\pi i t} + \tfrac{1}{4}e^{-4\pi i t}, that is 12+12cos⁡(4πt)\tfrac{1}{2} + \tfrac{1}{2}\cos(4\pi t).

Solution

cos⁡2x=12(1+cos⁡2x)\cos^2 x = \frac{1}{2}(1 + \cos 2x) and cos⁡2x=12(e2ix+e−2ix)\cos 2x = \frac{1}{2}(e^{2ix} + e^{-2ix}). Only c0=1/2c_0 = 1/2 and c±2=1/4c_{\pm 2} = 1/4 are nonzero; a trigonometric polynomial is its own Fourier series.

Exercise 3: Leibniz from the square wave (code)

Evaluate the square-wave series at t=1/4t = 1/4 and deduce a series for π\pi.

Answer

1−13+15−17+⋯=π/41 - \tfrac{1}{3} + \tfrac{1}{5} - \tfrac{1}{7} + \cdots = \pi/4.

Solution

At t=1/4t = 1/4, sin⁡(2πk/4)=sin⁡(kπ/2)\sin(2\pi k/4) = \sin(k\pi/2) is 1,−1,1,…1, -1, 1, \ldots for k=1,3,5,…k = 1, 3, 5, \ldots, and the square wave equals 1, so 1=4π(1−13+15−⋯ )1 = \frac{4}{\pi}(1 - \frac{1}{3} + \frac{1}{5} - \cdots).

python · try it yourself
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…

  1. a polynomial
  2. a sum of harmonics e2πikt/Te^{2\pi i k t/T}
  3. a product of sines
  4. a power series in tt

The harmonics e2πikt/Te^{2\pi i k t/T} on one period are…

  1. linearly dependent
  2. orthonormal
  3. all equal
  4. only orthogonal for even kk

The square wave contains…

  1. all harmonics
  2. only odd harmonics
  3. only even harmonics
  4. only the fundamental