C9 · Lesson 25 · Windows and spectral leakage
Why a pure tone smears across the DFT, and how windows tame it
Take the DFT of a pure tone and you'd expect one spike. You get one spike only if the tone fits a whole number of cycles in the record. Otherwise its energy leaks into every bin, and a weak tone a few bins away disappears under the leakage. The fix is to fade the record in and out before transforming it. Choosing that fade, the window, is a trade between seeing weak signals, separating close ones, and measuring amplitudes accurately.
- The curve is the spectrum of one tone through a window, in dB, against frequency in bins; the stems are where the DFT samples it.
- Set the offset to 0: the tone sits on a bin, and with the rectangular window (w = 0) every other stem is at a null. Move it to 0.5 and energy appears in every bin.
- Switch to the Hann window (w = 1). The peak is wider, but the leakage far from the peak drops from about −30 dB to below −70 dB at 10 bins.
Predict
With the rectangular window, a tone falls exactly halfway between two bins. How far below its true level does the largest bin read?
3.9 dB: the bin sits half a bin from the peak of the window's spectrum, where = 0.637. This scalloping loss makes the same tone read up to 36% low depending on its exact frequency. With the Hann window it's only 1.4 dB.
Name it
Where leakage comes from
An N-sample record is the signal multiplied by a window that is 1 for N samples and 0 elsewhere. Multiplying in time convolves in frequency, so every tone's line is replaced by the window's spectrum, centered on the tone. For the rectangular window that's a sinc: a main lobe 2 bins wide and side lobes that fall only as 1/(bin distance). The DFT samples this at the bins. If the tone is exactly on a bin, the samples land on the nulls; otherwise they catch the side lobes, and the tone leaks into every bin.
Windows
A window tapers the record smoothly to zero at both ends, so the periodic extension the DFT assumes has no jump. Smoother edges mean lower side lobes, but a wider main lobe.
| Window | Main lobe (null to null) | Highest side lobe | Scalloping loss | Noise bandwidth | Coherent gain |
|---|---|---|---|---|---|
| rectangular | 2 bins | −13.3 dB | 3.92 dB | 1.00 bin | 1 |
| Hann | 4 bins | −31.5 dB | 1.42 dB | 1.50 bins | 0.5 |
| Hamming | 4 bins | −42.7 dB | 1.75 dB | 1.36 bins | 0.54 |
| Blackman | 6 bins | −58.1 dB | 1.10 dB | 1.73 bins | 0.42 |
The Hann window is . Its spectrum is three shifted sincs, , whose side lobes partly cancel.
Choosing one
- Resolution: two tones closer than about one main-lobe half-width merge. The rectangular window resolves the closest tones of equal size.
- Dynamic range: to see a weak tone near a strong one, the strong tone's leakage there must be below the weak one. Hann, Hamming or Blackman.
- Amplitude: a flat-top window has almost no scalloping loss, at the cost of a very wide main lobe.
- Noise: a window's noise bandwidth is how many bins' worth of noise each bin collects. Divide a bin's power by it to get a noise density.
A windowed tone's bin height is reduced by the window's coherent gain, the average of the window: an on-bin tone of amplitude A reads , so divide by the coherent gain to measure amplitudes.
The spectrogram
Sliding a window along a long signal and taking a DFT at each position gives a short-time Fourier transform, drawn as a spectrogram. A longer window resolves frequency better and time worse; there is no window that does both.
Worked example
A 1010 Hz tone is analyzed with a 256-point DFT at 8 kHz. Where does it fall, and how much does the nearest bin lose with no window?
Show the solution
- Bin spacing.
- Where the tone falls.
- Loss at the nearest bin. Bin 32 is 0.32 bins off: which is 1.5 dB low.
- Leakage. A bin 10 away reads only about 32 dB below the peak, so the leakage spreads far across the spectrum.
Answer the tone falls at bin 32.32, and the nearest bin, 32, reads 0.839 of the full height, 1.5 dB low.
Worked example
A strong tone and a tone 50 dB weaker are 10 bins apart. Which window shows the weak one?
Show the solution
- Rectangular window. Its spectrum, 10 bins from its peak, is as high as −29.5 dB, depending on where the tone falls between bins.
- The weak tone under it. That's 20 dB above the weak tone, which vanishes.
- Hann window. Its spectrum there is below −68.5 dB, so the weak tone stands 18 dB clear.
- Main lobe. The Hann window's wider main lobe costs nothing at this spacing.
Answer the Hann window shows the weak tone, 18 dB clear; with the rectangular window it vanishes.
Worked example
A 1024-point DFT sees an on-bin tone of amplitude 2 V. What does its bin read with the rectangular window and with the Hann window?
Show the solution
- Rectangular.
- Hann. The coherent gain is 0.5, so the bin reads 512.
- Recover the amplitude. To recover 2 V from the Hann bin, compute
- The trap. Forgetting the coherent gain reads the amplitude 6 dB low.
Answer the bin reads 1024 with the rectangular window and 512 with the Hann window.
Check
A tone falls 0.25 of a bin away from the nearest bin.
(a)
How far below its true level does that bin read with the rectangular window?
(b)
And with the Hann window?
Check
A 256-point DFT runs at 44100 Hz.
(a)
About how close can two equal tones be and still be resolved with the rectangular window (one bin)?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
(b)
And with the Hann window, whose main lobe is twice as wide?
Check
An on-bin tone of amplitude 0.5 goes into a 1024-point DFT.
(a)
What does its bin read with the rectangular window?
(b)
And with the Hann window?
Check
A window with a noise bandwidth of 1.73 bins is used for a 1024-point DFT at 48000 Hz.
(a)
What noise bandwidth does each bin have, in hertz?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
(b)
How much more noise power does a bin collect than with the rectangular window?
Check
Which property of a window matters most for seeing a weak tone many bins away from a strong one?
Check
A record of N samples is taken at a rate f.
Step 1
How far apart are the DFT bins?
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 long is the record?
Step 3
What is the product of the two?
Check
Write tone_amplitude(x, window): estimate the amplitude of the single tone in x from its largest DFT bin (ignoring bin 0), where window is 'rect' or 'hann'. Apply the window, take numpy.fft.rfft, and correct for the coherent gain.
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 doesn't fit a whole number of cycles in the record leak into every DFT bin?
The DFT treats the record as one period of a periodic signal. If the tone completes a whole number of cycles, the repeated record joins smoothly and is exactly a sinusoid at a bin frequency, so it lands in one bin. If not, the repetition has a jump at every record boundary, and a jump needs energy at all frequencies. Equivalently, the record is the infinite tone multiplied by a rectangular window, which convolves the tone's line with the window's sinc-shaped spectrum; sampling that sinc anywhere but its nulls picks up the side lobes. A tapered window removes the jump, so its spectrum has much lower side lobes and the leakage falls off quickly away from the tone.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.