The fast Fourier transform
The same transform with N log N work instead of N² — the butterfly that made digital signal processing practical.
- Explain the divide-and-conquer idea behind the radix-2 FFT
- Count the operations of the DFT and the FFT
- Normalise an FFT spectrum so its peaks read as amplitudes
The DFT loop of lesson 3 does \(N\) multiplications for each of \(N\) bins: \(N^2\) in all. For a second of audio at 48 kHz that is 2.3 billion complex multiplications per transform. The fast Fourier transform gets the identical answer with about \(\tfrac{N}{2}\log_2 N\).
Split, transform, combine
Split the samples into even and odd positions. Each half is a DFT of size \(N/2\), and the full transform is assembled from the two:
\[X[k] = E[k] + W^k O[k], \qquad X[k + N/2] = E[k] - W^k O[k], \qquad W = e^{-2\pi i / N}.\]One complex multiplication, \(W^k O[k]\), serves two outputs. Apply the same split to each half, and again, until the pieces have size 1. The combining step is the butterfly, named after the shape of its wiring.
Each of the \(\log_2 N\) stages does \(N/2\) butterflies. The saving grows with \(N\):
5_Fourier_FFT.ipynb measures it: the notebook’s loop DFT against scipy.fftpack.fft. Re-running that comparison here gave a factor of about 9,000 at \(N = 2{,}000\) and \(N = 4{,}096\), with the two results agreeing to \(10^{-10}\). (The notebook uses Python’s built-in sum on NumPy arrays, which makes its loop slower still; np.sum is the fair comparison.)
Using it
FFT/fft.py applies the library FFT to the synthetic ECG:
from scipy.fftpack import fft, ifft
freq_domain_signal = fft(sigs.ecg_signal)
time_domain_signal = ifft(freq_domain_signal)
magnitude = np.abs(freq_domain_signal)
Two practical notes. First, fft returns complex numbers; plotting freq_domain_signal directly makes matplotlib drop the imaginary part with a warning, so plot magnitude (or .real and .imag separately) instead. Second, the raw output is scaled by \(N\). To read peak heights as amplitudes, divide by \(N\) and double the one-sided bins. 5_Fourier_FFT.ipynb does the first half of this (“FFT still needs normalization”), so its 6 Hz sine of amplitude 2 shows a peak of height 1: the other half of the amplitude sits in the mirror bin at \(-6\) Hz.
Try it yourself. Write a recursive FFT in ten lines: return x when len(x) == 1, otherwise transform x[0::2] and x[1::2] and combine with the butterfly. Check it against np.fft.fft for N = 8 and N = 1024, and count how many times the multiply line runs.