TExcelTAD
Home › Fourier Analysis & Signals › Spectral Leakage

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 NN 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.

python · try it yourself
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 2/π≈0.642/\pi \approx 0.64 for a long frame: the scalloping loss of about −3.9-3.9 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:

python · try it yourself
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 −60-60 dB sits more than 20 dB below the strong tone's leakage around it (about −37-37 dB), so it is completely invisible. A window function lowers that floor (Window Functions).

Remedies

  • Choose NN 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 (default window="hann") apply a window for this reason; scipy.signal.periodogram defaults to window="boxcar", no window at all, so pass window="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 fs=1000f_s = 1000 Hz with N=500N = 500, 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 fs/N=2f_s/N = 2 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 sinc⁡(1/2)=2/π\operatorname{sinc}(1/2) = 2/\pi.

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 sin⁡(π/2)/(π/2)=2/π≈0.637\sin(\pi/2)/(\pi/2) = 2/\pi \approx 0.637.

Exercise 3: Make it periodic (code)

Change NN in the first example so that 8.5 Hz is on a bin, and show the leakage disappears.

Answer

With N=128N = 128 (2 seconds), 8.5 Hz is bin 17 and only one bin is nonzero.

Solution
python · try it yourself
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…

  1. the signal is noisy
  2. the frame does not contain a whole number of cycles
  3. the sampling rate is too high
  4. the FFT size is a power of two

Cutting a signal to NN samples multiplies it by a rectangle, which in frequency…

  1. shifts the spectrum
  2. convolves the spectrum with a (periodic) sinc
  3. removes high frequencies
  4. does nothing

Which remedy reduces leakage for arbitrary frequencies?

  1. zero padding
  2. applying a tapering window
  3. using rfft
  4. rounding the samples