A10 · Lesson 16 · The FFT in practice
What an FFT analyzer actually shows you
Press the FFT button on an oscilloscope and a spectrum appears: but it's not the Fourier transform from the last few lessons. It's computed from a finite record of samples, so it can only show frequencies on a grid, it smears a tone that falls between grid points, and its heights depend on how you scale it. Reading such a spectrum correctly, and computing one yourself with NumPy, means knowing three things: bin spacing, amplitude scaling, and leakage with the windows that tame it.
- A 1 V tone near 1 kHz is analyzed from a 4 ms record with a rectangular window. The spectrum is in decibels.
- Set the tone to exactly 1 kHz, then nudge it. As soon as it falls between the analyzer's grid points, energy spreads into a wide skirt.
- That spreading is leakage. How far down is the skirt several bins away?
Predict
You sample at 8 kHz and take a 1024-point FFT. How far apart are the frequencies it can show?
7.8 Hz: . The bins are spaced by one over the record length, . A longer record gives finer resolution; a higher sample rate only extends the range, up to .
Name it
Bins
An -point FFT of samples taken at gives values at frequencies
The spacing is set by the record length . The highest frequency is , the Nyquist frequency. In NumPy, np.fft.rfft(x) returns the values and np.fft.rfftfreq(N, 1/fs) their frequencies.
Scaling
A sine of amplitude that lands exactly on a bin gives , so the amplitude spectrum is . (The DC bin and the Nyquist bin aren't doubled.)
Leakage
A finite record is the infinite signal multiplied by a rectangular window, so the spectrum is convolved with a sinc. A tone exactly on a bin falls on the sinc's peak and lands on the other bins' zeros. A tone between bins spreads into all of them: its peak drops (by up to 3.9 dB, the "picket fence" loss) and its skirt falls only slowly, about dB twenty bins away.
Windows
Multiplying the record by a smooth taper, such as the Hann window , kills the abrupt edges. The skirt drops to about dB twenty bins away, and the worst peak loss to 1.4 dB, at the price of a main lobe twice as wide. Normalize by instead of to keep amplitudes right.
Worked example
A 3 V, 1 kHz tone is sampled at 8 kHz for 1024 points. Where is it in the FFT, and how big?
Show the solution
- Find the bin. exactly.
- Its magnitude.
- Recover the amplitude.
Answer it lands exactly in bin 128 with magnitude 1536, which scales back to 3 V.
Worked example
The tone moves to 1003.9 Hz, half a bin off. What do the rectangular and Hann spectra show?
Show the solution
- The rectangular window. The peak reads 1.92 V (down 3.9 dB), and the skirt 20 bins away is only 32 dB down.
- The Hann window. Normalized by , the peak reads 2.55 V (down 1.4 dB), and the skirt 20 bins away is 86 dB down.
- Compare. For measuring a weak tone near a strong one, the window makes the difference.
Answer the rectangular window reads 1.92 V with its skirt only 32 dB down 20 bins away; Hann reads 2.55 V with the skirt 86 dB down.
Check
What is the bin spacing of a 1024-point FFT at a sample rate of 8000 Hz?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
At which bin index does a tone at 2500 Hz fall, for a 1024-point FFT at 8000 Hz?
Check
A tone sits exactly on a bin of a 1024-point FFT, with = 1536. What is its amplitude?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
How long a record do you need for a bin spacing of 0.5 Hz?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
What is the highest frequency an FFT of data sampled at 44100 Hz can show?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
Write spectrum(x, fs) returning (freqs, amps): the frequencies from np.fft.rfftfreq and the amplitudes from np.fft.rfft. Then a tone's amplitude and frequency can be read off the peak.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write hann_spectrum(x, fs): multiply by np.hanning(len(x)) before the FFT and normalize by 2/np.sum(w) instead of 2/N. The test compares leakage far from a tone that falls between bins.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a tone that falls between FFT bins spread into all the other bins?
The FFT treats the finite record as one period of a repeating signal. If the tone doesn't complete a whole number of cycles in the record, the repeated version has a jump where the end meets the start, and a jump contains every frequency. Equivalently, the record is the true signal times a rectangular window, so its spectrum is the tone's line convolved with the window's sinc. On a bin, the other bins sit on the sinc's zeros; off a bin, they sample its side lobes. A tapered window removes the jump and lowers the side lobes.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.