TExcelTAD
Home › Fourier Analysis & Signals › Computing Fourier Coefficients

Computing Fourier Coefficients

The Fourier coefficient ckc_k measures how much of the kk-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 ff with period TT:

ck=1T∫0Tf(t) e−2πikt/T dtc_k = \frac{1}{T} \int_0^T f(t)\, e^{-2\pi i k t / T}\, dt

Why: multiply f(t)=∑mcme2πimt/Tf(t) = \sum_m c_m e^{2\pi i m t/T} by e−2πikt/Te^{-2\pi i k t/T} and integrate. Every term with m≠km \ne k integrates to zero over a full period, and the m=km = k term gives ckTc_k T. The coefficient c0c_0 is the average of ff (its DC value).

An exact example

Take the sawtooth f(t)=tf(t) = t on [0,1)[0, 1), repeated. Then c0=1/2c_0 = 1/2, and integrating by parts gives, for k≠0k \ne 0,

ck=∫01t e−2πikt dt=i2πkc_k = \int_0^1 t\, e^{-2\pi i k t}\, dt = \frac{i}{2\pi k}

Numerically the integral becomes an average over many samples:

python · try it yourself
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 −1/(2N)=−0.000005-1/(2N) = -0.000005 instead of 0: a rectangle-rule error that shrinks with more samples. Note c−1=c1‾c_{-1} = \overline{c_1}, as for every real function.

Decay and smoothness

How fast ∣ck∣\lvert c_k \rvert shrinks tells you how smooth ff is:

Function on one period Smoothness ∣ck∣\lvert c_k \rvert decays like
square wave, sawtooth jumps 1/k1/k
triangle wave continuous, corners 1/k21/k^2
ff with mm continuous derivatives smoother 1/km+11/k^{m+1} or faster
analytic, e.g. ecos⁡2πte^{\cos 2\pi t} infinitely smooth exponentially fast
python · try it yourself
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 kk 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) / N computes c0,c1,…c_0, c_1, \ldots for samples of one period; negative kk 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 c0c_0 for f(t)=3+2cos⁡(2πt)f(t) = 3 + 2\cos(2\pi t)?

Answer

c0=3c_0 = 3.

Solution

c0c_0 is the average over a period; the cosine averages to zero, leaving 3.

Exercise 2: Conjugate symmetry

Show that c−k=ck‾c_{-k} = \overline{c_k} when ff is real.

Answer

Conjugate the integral defining ckc_k.

Solution

ck‾=1T∫0Tf(t)‾ e2πikt/T dt=1T∫0Tf(t) e−2πi(−k)t/T dt=c−k\overline{c_k} = \frac{1}{T}\int_0^T \overline{f(t)}\, e^{2\pi i k t/T}\, dt = \frac{1}{T}\int_0^T f(t)\, e^{-2\pi i (-k) t/T}\, dt = c_{-k}, using f‾=f\overline{f} = f.

Exercise 3: Coefficients of a pulse train (code)

A pulse train is 1 on [0,0.25)[0, 0.25) and 0 on [0.25,1)[0.25, 1). Compute c0,…,c4c_0, \ldots, c_4 numerically and compare ∣ck∣\lvert c_k \rvert with ∣sin⁡(πk/4)∣/(πk)\lvert \sin(\pi k/4) \rvert / (\pi k).

Answer

c0=0.25c_0 = 0.25, ∣ck∣=0.2251,0.1592,0.0750,0\lvert c_k \rvert = 0.2251, 0.1592, 0.0750, 0 for k=1..4k = 1..4.

Solution
python · try it yourself
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 c0c_0 of a Fourier series is…

  1. the maximum of ff
  2. the average of ff over a period
  3. always zero
  4. f(0)f(0)

A function with jumps has Fourier coefficients that decay like…

  1. 1/k1/k
  2. 1/k21/k^2
  3. e−ke^{-k}
  4. they do not decay

For a real function, c−kc_{-k} equals…

  1. ckc_k
  2. −ck-c_k
  3. ck‾\overline{c_k}
  4. 0