A13 · Lesson 47 · The FFT as an algorithm
Splitting a transform in half, again and again
C9 and A10 used the FFT as a black box that computes a DFT quickly. Here's what's inside. A DFT of N points is N sums of N terms: multiplications. The fast Fourier transform notices that an N-point DFT is two N/2-point DFTs (of the even and odd samples) glued together with N/2 extra multiplications, and applies that idea recursively. The cost drops to , a 200-fold saving at 1024 points and 100 000-fold at a million. The same trick makes fast convolution, fast filtering and spectral methods for differential equations possible.
- The curves count complex multiplications for a direct DFT () and a radix-2 FFT (), on log scales.
- At small N they're close; by N = 1024 the FFT is 200 times faster, and the gap keeps widening in proportion to .
- The readouts give both counts and the speedup at the marked N.
Predict
An FFT of (about a million) points is about how many times faster than a direct DFT?
About : . What would take a direct DFT about 20 minutes at a billion multiplications per second takes the FFT 10 ms.
Name it
The DFT
Decimation in time (radix 2)
Split x into even and odd samples, with N/2-point DFTs and . Then for :
the butterfly: one multiplication by the twiddle factor makes two outputs. Recursing down to 1-point DFTs takes stages of N/2 butterflies:
Done in place, the input ends up in bit-reversed order: sample n is stored at the index whose binary digits are n's, reversed.
Practical points
- Any N: mixed-radix FFTs handle products of small primes; Bluestein's algorithm turns a prime-length DFT into a convolution done by power-of-two FFTs. Libraries like FFTW choose a plan for each N.
- Real signals: an N-point real FFT costs about half as much, since the spectrum is conjugate-symmetric.
- Accuracy: the FFT is backward stable, with relative RMS error growing only as ; a direct DFT's grows faster.
Uses
- Fast convolution: zero-pad both sequences to at least points (to avoid circular wrap-around), transform, multiply, transform back. For long signals, overlap-add (C9) does it in blocks.
- Spectral differentiation: for a periodic signal, multiply coefficient k by (in radians per sample) and transform back. For smooth periodic functions this is accurate to nearly every digit, far beyond any finite difference: the basis of spectral PDE solvers.
Worked example
Compare direct and FFT cost for N = 1024.
Show the solution
- Direct. multiplications.
- FFT. multiplications.
- Speedup. The ratio is 205.
Answer 1 048 576 multiplications directly against 5120 by FFT, a speedup of 205.
Worked example
Convolve a 1000-sample signal with a 200-tap filter, directly and by FFT.
Show the solution
- Directly. multiplications.
- Pad. The result has 1199 samples, so pad to 2048.
- The transforms. Two forward transforms and one inverse cost multiplications, plus 2048 for the spectra: about 36 000.
- Compare. That's under a fifth of the direct cost. (Transforming the filter once and reusing it saves a third more.)
Answer 200 000 multiplications directly against about 36 000 by FFT, under a fifth.
Worked example
Two tones 10 Hz apart are sampled at 1 kHz. How many samples does an FFT need to separate them, and does zero padding help?
Show the solution
- Bin spacing. It is , so separating tones 10 Hz apart needs at least samples.
- With a window. A Hann window's main lobe is four bins wide, so about 200 to 400 samples are needed in practice to see two clear peaks.
- Zero padding. Padding a 100-sample record to 1024 points draws the same spectrum on a finer grid of 0.98 Hz, which makes the peaks easier to locate, but the two tones stay as blurred together as before.
- Where resolution comes from. It comes from the record length, , and nothing else.
Answer at least 100 samples, and 200 to 400 with a Hann window; zero padding doesn't help separate the tones.
Check
How many complex multiplications does a radix-2 FFT of points take?
Check
How many times fewer multiplications does an FFT of points need than a direct DFT?
Check
To convolve 2000 samples with a 64-tap filter by FFT, what's the smallest power-of-two length that avoids wrap-around?
Check
What's the real part of the twiddle factor ?
Check
In an in-place 8-point FFT, where does input sample 3 end up (its 3-bit index reversed)?
Check
You need the derivative of a smooth periodic signal from 64 samples per period. Which gives the most accurate result?
Check
A record of 256 samples at 1000 Hz is analyzed with an FFT.
(a)
What is its bin spacing?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
(b)
After zero padding to 4N points, what is the spacing of the plotted points?
Check
Count the work of a radix-2 FFT of length N.
Step 1
How many complex multiplications (one per butterfly)?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many times fewer than a direct DFT's ?
Explain why
How does splitting a DFT into even and odd samples save work?
An N-point DFT of the even-indexed samples and one of the odd-indexed samples each have only N/2 outputs, and their twiddle factors repeat with period N/2. The full transform's outputs can be built from them: output k is the even part plus a twiddle factor times the odd part, and output k + N/2 uses the same two numbers with a minus sign. So two half-size DFTs plus N/2 multiplications replace one full DFT. Applying the same split to each half, and again, until only single samples remain, gives levels with N/2 multiplications each, instead of the of the direct sum.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.