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.
- 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.
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:
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].