Computing Fourier Coefficients
The Fourier coefficient measures how much of the -th harmonic a periodic function contains. Because the harmonics are orthonormal, it is a single integral: multiply by the conjugate harmonic and average over one period. This lesson derives the formula, computes coefficients exactly and numerically, and shows how smoothness controls how fast they decay.
The formula
For with period :
Why: multiply by and integrate. Every term with integrates to zero over a full period, and the term gives . The coefficient is the average of (its DC value).
An exact example
Take the sawtooth on , repeated. Then , and integrating by parts gives, for ,
Numerically the integral becomes an average over many samples:
import numpy as np
N = 100000
t = np.arange(N) / N
f = t # sawtooth on [0, 1)
def coeff(k):
return np.mean(f * np.exp(-2j * np.pi * k * t))
for k in (0, 1, 2, 3, -1):
exact = complex(0.5) if k == 0 else complex(0, 1 / (2 * np.pi * k))
c = coeff(k)
print(f"k={k:2d}: numeric {c.real:+.6f}{c.imag:+.6f}i exact {exact.real:+.6f}{exact.imag:+.6f}i")k= 0: numeric +0.499995+0.000000i exact +0.500000+0.000000i
k= 1: numeric -0.000005+0.159155i exact +0.000000+0.159155i
k= 2: numeric -0.000005+0.079577i exact +0.000000+0.079577i
k= 3: numeric -0.000005+0.053052i exact +0.000000+0.053052i
k=-1: numeric -0.000005-0.159155i exact +0.000000-0.159155i
The real parts are instead of 0: a rectangle-rule error that shrinks with more samples. Note , as for every real function.
Decay and smoothness
How fast shrinks tells you how smooth is:
| Function on one period | Smoothness | decays like |
|---|---|---|
| square wave, sawtooth | jumps | |
| triangle wave | continuous, corners | |
| with continuous derivatives | smoother | or faster |
| analytic, e.g. | infinitely smooth | exponentially fast |
import numpy as np
N = 4096
t = np.arange(N) / N
funcs = {
"square": np.where(t < 0.5, 1.0, -1.0),
"triangle": 1 - 4 * np.abs(t - 0.5),
"smooth": np.exp(np.cos(2 * np.pi * t)),
}
for name, f in funcs.items():
c = np.fft.fft(f) / N # all coefficients at once (see the DFT lessons)
print(f"{name:9s}", [f"{abs(c[k]):.1e}" for k in (1, 3, 9, 27)])square ['6.4e-01', '2.1e-01', '7.1e-02', '2.4e-02']
triangle ['4.1e-01', '4.5e-02', '5.0e-03', '5.6e-04']
smooth ['5.7e-01', '2.2e-02', '5.5e-09', '1.9e-18']
Multiplying by 3 divides the square wave's coefficients by 3, the triangle's by 9, and the smooth function's by a huge factor until they hit rounding level.
In software
np.fft.fft(samples) / Ncomputes for samples of one period; negative sit at the end of the array (The FFT in NumPy).- Fast decay is why smooth signals compress well: a few coefficients describe them (Compression).
Exercises
Exercise 1: The average
What is for ?
Answer
.
Solution
is the average over a period; the cosine averages to zero, leaving 3.
Exercise 2: Conjugate symmetry
Show that when is real.
Answer
Conjugate the integral defining .
Solution
, using .
Exercise 3: Coefficients of a pulse train (code)
A pulse train is 1 on and 0 on . Compute numerically and compare with .
Answer
, for .
Solution
import numpy as np
N = 100000
t = np.arange(N) / N
f = (t < 0.25).astype(float)
for k in range(5):
c = np.mean(f * np.exp(-2j * np.pi * k * t))
formula = 0.25 if k == 0 else abs(np.sin(np.pi * k / 4)) / (np.pi * k)
print(k, round(abs(c), 4), round(formula, 4))0 0.25 0.25
1 0.2251 0.2251
2 0.1592 0.1592
3 0.075 0.075
4 0.0 0.0
The fourth harmonic is missing entirely: four periods of it fit exactly into the pulse, so it averages to zero against the pulse.
Test yourself
The coefficient of a Fourier series is…
- the maximum of
- the average of over a period
- always zero
A function with jumps has Fourier coefficients that decay like…
- they do not decay
For a real function, equals…
- 0