The discrete Fourier transform

Wind the signal around a circle at every frequency and watch where its centre of mass goes. That is the DFT.

What you'll learn
  • Read the DFT formula as a sum of rotating phasors
  • Explain the winding picture and why a matching frequency pulls the centre of mass away
  • Turn bin numbers into hertz, and recognise spectral leakage

The discrete Fourier transform of \(N\) samples \(x[n]\) is

\[X[k] = \sum_{n=0}^{N-1} x[n]\, e^{-2\pi i k n / N}, \qquad k = 0, 1, \dots, N - 1.\]

Each term takes sample \(n\) and rotates it by an angle that grows with \(n\). The scripts in DFT/ write this out with real arithmetic, splitting \(e^{-i\theta} = \cos\theta - i \sin\theta\):

for k in range(int(len(sig_src_arr)/2)):
    for i in range(len(sig_src_arr)):
        rex[k] = rex[k] + sig_src_arr[i]*math.cos(2*math.pi*k*i/len(sig_src_arr))
        imx[k] = imx[k] - sig_src_arr[i]*math.sin(2*math.pi*k*i/len(sig_src_arr))
mag[x] = math.sqrt(rex[x]**2 + imx[x]**2)

Only the first \(N/2\) bins are computed. For a real signal the upper half is the mirror image (\(X[N-k] = \overline{X[k]}\)), so nothing is lost.

The winding picture

Read the sum geometrically. Plot each sample as a point at distance \(x[n]\) from the origin, at angle \(-2\pi k n/N\): the signal is wound around the origin, \(k\) times over its length. \(X[k]/N\) is the centre of mass of the wound curve.

A one-second signal wound around the origin at a slowly increasing frequency. Most of the time the loops spread evenly and the centre of mass (orange) stays near the middle. When the winding frequency matches a frequency in the signal, the peaks all land on the same side and the centre swings out — tracing the magnitude spectrum on the right.

When the winding rate matches a frequency in the signal, every crest lands at the same angle and the curve becomes lopsided: the centre moves away from the origin. At any other rate the crests spread round the circle and cancel. That is all the DFT does — it tests the signal against every whole-number rate and reports how lopsided each winding is.

Bins, hertz and leakage

Bin \(k\) corresponds to \(k\,f_s/N\) hertz. The test signal in mysignals.py has \(N = 320\) samples at 48 kHz, so bins are 150 Hz apart:

Left: the 320-sample test signal, a slow 1 kHz wave with a fast 15 kHz ripple. Right: its DFT magnitude, a clean spike at 15 kHz and a smeared group of bins around 1 kHz.
15 kHz is exactly bin 100 and shows as one clean line. 1 kHz is bin 6.67 — between bins — so its energy leaks into its neighbours.

The 15 kHz component completes a whole number of cycles in the window and lands on one bin. The 1 kHz component completes 6.67 cycles, falls between bins 6 and 7, and its energy leaks across many bins. Leakage is not an error in the code; it is what the DFT does with a frequency that does not fit the window. Windows (lesson 7) trade a wider peak for much less leakage.

Try it yourself. Run the DFT loop on np.cos(2*np.pi*7*np.arange(320)/320) — exactly 7 cycles — and confirm a single nonzero bin of height 160. Then change 7 to 6.5 and count how many bins rise above 1. Finally check your loop against np.abs(np.fft.fft(x))[:160].

View source on GitHub →