Leakage and windowing
About this lesson
The DFT only ever sees one block of samples, and it treats that block as if it repeated forever. When a tone fits a whole number of cycles in the block, it lands on one bin. When it does not, the repeated block has a seam, and the tone's energy leaks into every bin, enough to bury a quiet tone. Follow a 750 Hz and a 875 Hz tone through a 32-sample FFT, then taper the block with a Hann window and watch the quiet 2500 Hz tone come back, at the price of a wider peak. It builds on the lessons on the DTFT, the DFT, zero-padding and the FFT, and on decibels.
One block of 32 samples
A loud 750 Hz tone plus a quiet 2500 Hz tone, 100 times smaller, are sampled 8000 times a second. We keep one block of N = 32 samples (4 ms). Its DFT has a bin every fs/N = 250 Hz.
fs = 8000 samples per second N = 32 samples -> 32 / 8000 s = 4 ms x[n] = cos(2π·750·n/fs) + 0.01·cos(2π·2500·n/fs) bin spacing fs / N = 8000 / 32 = 250 Hz bins 0 .. 16: 0 Hz .. 4000 Hz (fs/2) buffer: x[0] x[1] ... x[30] x[31]
Read the lesson step by step
One block of 32 samples
A loud 750 Hz tone plus a quiet 2500 Hz tone, 100 times smaller, are sampled 8000 times a second. We keep one block of N = 32 samples (4 ms). Its DFT has a bin every fs/N = 250 Hz.
fs = 8000 samples per second N = 32 samples -> 32 / 8000 s = 4 ms x[n] = cos(2π·750·n/fs) + 0.01·cos(2π·2500·n/fs) bin spacing fs / N = 8000 / 32 = 250 Hz bins 0 .. 16: 0 Hz .. 4000 Hz (fs/2) buffer: x[0] x[1] ... x[30] x[31]The DFT sees a loop
The DFT treats the block as one period of a signal that repeats forever. 750 Hz fits exactly 3 whole cycles in 4 ms, so the end of the block flows smoothly into its start.
750 Hz × 4 ms = 3 cycles (a whole number) 750 / 250 = bin 3 the DFT's view: x[n + 32] = x[n] ... | block | block | block | ... plot: the block (n 0 .. 31) and the start of its repeat (n 32 .. 47), shadedOn a bin: two clean lines
750 Hz is bin 3 and 2500 Hz is bin 10. The FFT shows the loud tone at 0 dB and the quiet one at −40 dB (100 times smaller). Every other bin is zero, so both tones are easy to see.
level of bin k: A = 2·|X[k]| / Σw (Σw = 32) in dB: 20·log10(A) bin 3 (750 Hz): 0.0 dB loud tone bin 10 (2500 Hz): −40.0 dB quiet tone every other bin: 0 (below the plot floor) thin curve: the DTFT of the block; the bins are its samples every 250 HzBetween bins: a seam
Retune the loud tone to 875 Hz, bin 3.5, halfway between two bins. Now 3.5 cycles fit in the block. Where the loop joins end to start, the wave jumps from −1 back to +1: a seam.
875 Hz × 4 ms = 3.5 cycles 875 / 250 = bin 3.5 where the wave would be at n = 32: cos(2π·3.5) = −1.00 start of the next block (n = 0): cos(0) = 1 the loop has a jump of about 2 at every block boundaryLeakage: energy in every bin
A jump needs many frequencies to build it, so the tone's energy leaks into every bin. Its biggest bin reads only −3.4 dB, and bins far away still hold −24.1 dB. This spreading is spectral leakage.
rectangle window (all ones), 875 Hz: bin 3: −4.5 dB bin 4: −3.4 dB (the tone sits between them) bin 0 and bin 16: −24.1 dB rectangle side lobes: first one ≈13 dB down, falling about 6 dB per octave (octave = each doubling of distance from the peak) more samples do not lower the peak leakageLeakage hides the quiet tone
Bin 10 should read −40 dB, the quiet tone. But leakage from the loud tone alone already puts −21.9 dB there, about 8 times more. Bin 10 reads −21.1 dB, and the quiet tone is buried.
at bin 10 (2500 Hz): quiet tone alone −40.0 dB loud tone's leakage −21.9 dB both together −21.1 dB −21.9 dB − (−40 dB) = 18.1 dB -> 8 times bigger in amplitude no peak at bin 10: the quiet tone is lostThe Hann window tapers the ends
Cutting out a block already used a rectangle window: all ones. Instead, multiply each sample by the Hann window w[n] = 0.5 − 0.5·cos(2πn/N): near 0 at both ends, 1 in the middle. The seam is gone.
w[n] = 0.5 − 0.5·cos(2π·n/32), n = 0 .. 31 w[0] = 0 w[16] = 1 w[31] = 0.0096 windowed sample: x[n]·w[n] x[0]·w[0] = 0 x[31]·w[31] = −0.0075 both ends of the loop sit near 0: no jump Σw = 16 (the rectangle's is 32)Leakage falls away
Away from the peak, leakage now drops fast. The Hann side lobes (small humps beside the peak) start 31.5 dB down and fall 18 dB per octave (each doubling of distance from the peak). Bin 10 reads −39.9 dB: the quiet tone is back.
Hann window, 875 Hz: bin 3: −1.4 dB bin 4: −1.4 dB bin 9: −45.3 dB bin 10: −39.9 dB bin 11: −45.9 dB bin 14: −73.9 dB loud tone's leakage at bin 10: −58.0 dB (was −21.9 dB with the rectangle) bin 10 is a peak againThe price: a wider peak
Nothing is free. The Hann main lobe (the central peak) is 4 bins wide at its base, twice the rectangle's 2 bins, so tones close together blur sooner. Pick the window for the job: sharp peaks or low leakage.
main lobe width at the base (first zeros): rectangle 2 bins = 500 Hz Hann 4 bins = 1000 Hz first side lobe: rectangle ≈−13 dB, Hann −31.5 dB roll-off: 6 vs 18 dB per octave longer blocks (bigger N) narrow both lobesWindowing in Python
With numpy, windowing the block is one line and the spectrum another. Dividing by w.sum() = 16, not 32, makes up for the window's halved gain, so a tone on a bin still reads 0 dB.
import numpy as np n = np.arange(32) x = np.cos(2*np.pi*875*n/8000) x += 0.01*np.cos(2*np.pi*2500*n/8000) w = 0.5 - 0.5*np.cos(2*np.pi*n/32) A = 2*abs(np.fft.rfft(x*w))/w.sum() >>> np.round(20*np.log10(A[[3, 4, 10]]), 1) array([-1.4, -1.4, -39.9]) >>> len(A) 17 same w: scipy.signal.windows.hann(32, sym=False) (np.hanning(32) is the symmetric form: n/31)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A block holds N = 32 samples taken at 8000 samples per second. Which tone lands exactly on a DFT bin?
Bins sit every fs/N = 250 Hz. 1000 Hz = 4 × 250 Hz is bin 4, so exactly 4 cycles fit in the block. The others fall between bins.
02Why does a tone between two bins leak into every bin?
The DFT assumes the block is one period of a periodic signal. With a fractional number of cycles the end does not meet the start, and building that jump takes energy at every frequency.
03In the example, the 875 Hz tone's leakage reaches −21.9 dB at bin 10 with the rectangle window. Why can't you see the −40 dB quiet tone there?
−21.9 dB is 18.1 dB above −40 dB, about 8 times in amplitude. The bin reads −21.1 dB and shows no peak, so the weak tone is buried.
04What does the Hann window w[n] = 0.5 − 0.5·cos(2πn/N) do to the spectrum, compared with the rectangle?
Tapering the ends toward zero removes the seam, so leakage far from the peak drops fast. The price is a main lobe 4 bins wide at its base instead of 2, so close tones blur sooner.
05You window a 32-sample block with Hann before the FFT. What should you divide 2·|X[k]| by so that a sine of amplitude 1 on a bin reads 1 (0 dB)?
The window halves the samples' total weight: Σw[n] = 16 for N = 32. Dividing by Σw instead of N undoes that coherent-gain loss.
Glossary
- Bin
- One output of the DFT, X[k], at frequency k·fs/N. Here the bins sit every 250 Hz.
- Spectral leakage
- Energy of a tone that is not on a bin spreading into every other bin, because the DFT treats the block as repeating and the loop has a seam.
- Window
- A sequence w[n] that multiplies the block before the DFT. Just cutting out a block is the rectangle window (all ones).
- Hann window
- w[n] = 0.5 − 0.5·cos(2πn/N): 0 at the start, nearly 0 at the end and 1 in the middle (periodic form, used for spectral analysis).
- Main lobe
- The central peak of a tone in the spectrum. Rectangle: 2 bins wide at its base; Hann: 4 bins.
- Side lobes
- The smaller humps on either side of the main lobe. They carry the leakage far from the tone.
- Decibel (dB)
- 20·log10 of an amplitude ratio: −40 dB is 100 times smaller, −20 dB is 10 times smaller.
Notes and sources
- The two tones, 8000 samples a second and the 32-sample block are a chosen example; every level in the figure is computed from this model with the DFT sum, and the Python card was checked with numpy. Levels are 2·|X[k]|/Σw in dB, so a sine of amplitude 1 on a bin reads 0 dB with either window.
- The window figures (rectangle: first side lobe ≈13 dB down (13.3 dB in Harris), 6 dB per octave; Hann: −31.5 dB, 18 dB per octave, main lobe twice as wide) are from J. O. Smith III, Spectral Audio Signal Processing (CCRMA), and F. J. Harris, Proc. IEEE 1978.
- For spectral analysis the window is the periodic Hann, cos(2πn/N); numpy.hanning(N) is the symmetric one, cos(2πn/(N−1)), meant for filter design. With N = 32 the two differ only slightly.
- J. O. Smith III, Mathematics of the DFT, CCRMA Stanford: "Frequencies in the Cracks" (spectral leakage)
- J. O. Smith III, Spectral Audio Signal Processing, CCRMA Stanford: Rectangular Window Side Lobes
- J. O. Smith III, Spectral Audio Signal Processing, CCRMA Stanford: Hann or Hanning or Raised Cosine
- F. J. Harris, "On the use of windows for harmonic analysis with the discrete Fourier transform", Proceedings of the IEEE 66(1), 1978, pp. 51-83
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed. (course text listed on MIT OCW 6.341 readings)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim)
- Window function (Wikipedia): spectral leakage, Hann side lobes roll off about 18 dB per octave
- NumPy documentation: numpy.fft.rfft (n/2 + 1 outputs for even n)
- NumPy documentation: numpy.hanning (symmetric, M − 1 in the denominator)
- SciPy documentation: scipy.signal.windows.hann (sym=False gives a periodic window for spectral analysis)
- SciPy documentation: scipy.signal.get_window (fftbins=True: periodic window)
Quiz Unlocks after the last step.