Spectral Leakage
Take the FFT of a pure tone and you expect one spike. Usually you get a spike plus a skirt of smaller values spreading across the whole spectrum. That skirt is spectral leakage. It is not noise or a bug: it comes from cutting the signal to a finite frame, and it can hide weak components next to strong ones.
Where it comes from
The DFT treats the samples as one period of a periodic signal. If the tone completes a whole number of cycles in the frame, the periodic extension is a perfect sinusoid and the DFT has one nonzero bin. If not, the extension has a jump at the frame boundary, and jumps have spectra that decay slowly.
Equivalently, by the convolution theorem, cutting the signal multiplies it by a rectangular window, which convolves its spectral line with the window's transform, a periodic sinc (the Dirichlet kernel). On-bin tones land exactly on the sinc's zeros; off-bin tones do not.
import numpy as np
N, fs = 64, 64.0
t = np.arange(N) / fs
for f in (8.0, 8.5):
A = np.abs(np.fft.rfft(np.sin(2 * np.pi * f * t))) / (N / 2)
print(f"tone at {f} Hz: bins 4..12 =", np.round(A[4:13], 3).tolist())
print(f" bins above 0.01: {int(np.sum(A > 0.01))} of {A.size}, peak {A.max():.3f}")tone at 8.0 Hz: bins 4..12 = [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]
bins above 0.01: 1 of 33, peak 1.000
tone at 8.5 Hz: bins 4..12 = [0.092, 0.11, 0.145, 0.228, 0.651, 0.623, 0.2, 0.116, 0.08]
bins above 0.01: 33 of 33, peak 0.651
The on-bin tone gives one bin of height 1. The half-bin tone spreads over many bins, and its peak drops to about 0.65, close to the theoretical for a long frame: the scalloping loss of about dB.
Leakage hides weak signals
A tone 60 dB weaker than its neighbour (amplitude 0.001) is invisible under the leakage skirt of the strong one:
import numpy as np
N, fs = 256, 256.0
t = np.arange(N) / fs
x = np.sin(2 * np.pi * 40.5 * t) + 0.001 * np.sin(2 * np.pi * 60 * t)
A = 20 * np.log10(np.abs(np.fft.rfft(x)) / (N / 2) + 1e-12)
print("level at 60 Hz (weak tone): ", round(A[60], 1), "dB")
print("leakage level at 59 and 61 Hz: ", round(A[59], 1), round(A[61], 1), "dB")
print("leakage level of strong tone at 80 Hz:", round(A[80], 1), "dB")level at 60 Hz (weak tone): -36.7 dB
leakage level at 59 and 61 Hz: -36.2 -37.2 dB
leakage level of strong tone at 80 Hz: -43.0 dB
The weak tone at dB sits more than 20 dB below the strong tone's leakage around it (about dB), so it is completely invisible. A window function lowers that floor (Window Functions).
Remedies
- Choose so that tones complete whole cycles (possible only when you control the frequencies, as in test equipment).
- Apply a window that tapers smoothly to zero at both ends, removing the jump.
- Record longer: the skirt is fixed in bins, so more bins per Hz pushes it closer to the peak.
In software
- Spectrum analyzers and
scipy.signal.welch(defaultwindow="hann") apply a window for this reason;scipy.signal.periodogramdefaults towindow="boxcar", no window at all, so passwindow="hann"yourself. - Leakage also appears in 2-D: the FFT of an image shows a bright cross because the left and right (and top and bottom) edges do not match.
Exercises
Exercise 1: On-bin or not?
At Hz with , which of 50 Hz, 51 Hz and 50.5 Hz leak?
Answer
51 Hz and 50.5 Hz leak; only 50 Hz is on a bin.
Solution
The bin spacing is Hz, so the bins are 0, 2, 4, ... Hz. 50 Hz is on bin 25, but 51 Hz lies halfway between bins 25 and 26, so it leaks, and so does 50.5 Hz. Only 50 Hz is leak-free.
Exercise 2: Scalloping loss
Why is the measured amplitude of a tone halfway between two bins only about 64 % of the truth?
Answer
The rectangular window's sinc response at half a bin is .
Solution
The tone's spectral line is convolved with the window transform, and the nearest bins sample it half a bin from its peak, where the sinc equals .
Exercise 3: Make it periodic (code)
Change in the first example so that 8.5 Hz is on a bin, and show the leakage disappears.
Answer
With (2 seconds), 8.5 Hz is bin 17 and only one bin is nonzero.
Solution
import numpy as np
N, fs = 128, 64.0
t = np.arange(N) / fs
A = np.abs(np.fft.rfft(np.sin(2 * np.pi * 8.5 * t))) / (N / 2)
print(int(np.sum(A > 0.01)), int(np.argmax(A)), round(float(A.max()), 3))1 17 1.0
Two seconds of an 8.5 Hz tone contain exactly 17 cycles.
Test yourself
Spectral leakage occurs when…
- the signal is noisy
- the frame does not contain a whole number of cycles
- the sampling rate is too high
- the FFT size is a power of two
Cutting a signal to samples multiplies it by a rectangle, which in frequency…
- shifts the spectrum
- convolves the spectrum with a (periodic) sinc
- removes high frequencies
- does nothing
Which remedy reduces leakage for arbitrary frequencies?
- zero padding
- applying a tapering window
- using rfft
- rounding the samples