Leakage, Windows and Scalloping Loss
In the previous lesson every tone sat exactly on a bin and the spectrum was a set of clean lines. Real tones do not cooperate. Record a tone for exactly one second and the FFT shows no line at (there is no such bin): the energy is smeared over the whole spectrum, the peak reads low, and a weak tone thirty bins away is buried under the skirt. This is spectral leakage. Windows trade a little resolution for much less leakage, and choosing one is a routine engineering decision with numbers behind it. The mathematics of leakage and the common window shapes are in Spectral Leakage and Windows; this lesson gives the figures of merit and how to correct readings.
Why leakage happens
The DFT treats the samples as one period of a periodic signal. If the record holds a whole number of cycles, the periodic extension is a seamless sine; otherwise it has a jump at the wrap-around, and a jump needs many frequencies. Equivalently, the record is the infinite signal multiplied by a rectangular window, so the spectrum is convolved with the window's transform, a Dirichlet kernel with sidelobes that decay only per octave. A tone exactly on a bin lands on the kernel's peak and its zeros (no leakage visible); a tone between bins samples the kernel elsewhere.
A window tapers the record to zero at both ends, removing the jump. Its transform has lower sidelobes but a wider main lobe.
Figures of merit
| Figure | Definition | Meaning |
|---|---|---|
| coherent gain (CG) | a tone's peak reads low by this factor: divide by it | |
| equivalent noise bandwidth (ENBW) | bins | how much noise each bin collects; noise readings scale by it |
| scalloping loss | worst-case amplitude error for a tone halfway between bins | |
| highest sidelobe | largest lobe outside the main lobe, dB | how far down a weak neighbour must be to stay hidden |
| main-lobe width | null to null, bins | two tones closer than about half this merge (Resolution) |
import numpy as np
from scipy.signal import windows
N, pad = 1024, 64
for name in ("boxcar", "hann", "hamming", "blackmanharris", "flattop"):
w = windows.get_window(name, N, fftbins=True)
cg = w.sum() / N
enbw = N * np.sum(w**2) / w.sum() ** 2
W = np.abs(np.fft.rfft(w, N * pad))
W /= W[0]
scallop = 20 * np.log10(W[pad // 2]) # half a bin = pad/2 fine bins
i0 = np.argmax(W < 0.5) # skip the (flat-top's rippled) top of the main lobe
first_null = i0 + np.argmax(np.diff(W[i0:]) > 0) # end of the main lobe: first local minimum
sidelobe = 20 * np.log10(W[first_null:].max())
print(f"{name:15s} CG {cg:.3f} ENBW {enbw:.2f} bins scalloping {scallop:6.2f} dB "
f"highest sidelobe {sidelobe:7.1f} dB main lobe +/-{first_null / pad:.1f} bins")boxcar CG 1.000 ENBW 1.00 bins scalloping -3.92 dB highest sidelobe -13.3 dB main lobe +/-1.0 bins
hann CG 0.500 ENBW 1.50 bins scalloping -1.42 dB highest sidelobe -31.5 dB main lobe +/-2.0 bins
hamming CG 0.540 ENBW 1.36 bins scalloping -1.75 dB highest sidelobe -42.7 dB main lobe +/-2.0 bins
blackmanharris CG 0.359 ENBW 2.00 bins scalloping -0.83 dB highest sidelobe -92.0 dB main lobe +/-4.0 bins
flattop CG 0.216 ENBW 3.77 bins scalloping -0.01 dB highest sidelobe -93.0 dB main lobe +/-5.0 bins
Read the table as a menu. The rectangular window (no window) has the narrowest main lobe but the worst sidelobes and the largest scalloping loss: use it only for tones known to be on bins, or for transients that fit inside the record. Hann is the general-purpose default. Blackman–Harris (4-term) pushes the sidelobes below for high-dynamic-range work at the cost of a wider lobe. Flat-top has almost no scalloping loss, so it is the choice for amplitude measurement of a single tone (calibrating a level), but its main lobe is wide. Hamming cancels the first sidelobe but its far sidelobes stay high.
Worked example: a strong tone between bins and a weak neighbour
Given: , (so ); a tone at (halfway between bins) and a tone () at .
Find: the amplitude read for the strong tone with rectangular, Hann and flat-top windows (after dividing by the coherent gain), and whether the weak tone stands out of the strong tone's leakage.
Solve: the scalloping losses predict readings of (rectangular), (Hann) and almost exactly (flat-top). The rectangular skirt bins away is roughly , about , far above : the weak tone is invisible. Hann's sidelobes fall at per octave and are below that far out, so the weak tone appears clearly.
Check: the program measures both.
import numpy as np
from scipy.signal import windows
import matplotlib.pyplot as plt
fs = N = 1024
t = np.arange(N) / fs
x = 1.0 * np.sin(2 * np.pi * 100.5 * t) + 1e-3 * np.sin(2 * np.pi * 130 * t)
f = np.fft.rfftfreq(N, 1 / fs)
plt.figure(figsize=(7, 3))
for name in ("boxcar", "hann", "flattop"):
w = windows.get_window(name, N)
A = 2 * np.abs(np.fft.rfft(x * w)) / w.sum() # amplitude scaling corrected for the window
k_weak = 130
skirt = np.median(A[[125, 126, 127, 133, 134, 135]]) # strong tone's leakage around the weak one
print(f"{name:8s}: strong tone reads {A[100:102].max():.3f} V; at 130 Hz {20 * np.log10(A[k_weak]):6.1f} dB, "
f"leakage floor nearby {20 * np.log10(skirt):6.1f} dB")
plt.plot(f, 20 * np.log10(A + 1e-12), lw=0.9, label=name)
plt.xlim(60, 160); plt.ylim(-140, 5); plt.xlabel("f (Hz)"); plt.ylabel("dBV (peak)")
plt.legend(); plt.tight_layout(); plt.show()boxcar : strong tone reads 0.638 V; at 130 Hz -40.3 dB, leakage floor nearby -40.2 dB
hann : strong tone reads 0.849 V; at 130 Hz -60.0 dB, leakage floor nearby -97.6 dB
flattop : strong tone reads 0.999 V; at 130 Hz -60.0 dB, leakage floor nearby -92.9 dB
Using windows correctly
- Amplitude of tones: divide by instead of (that is, by the coherent gain). For best accuracy use flat-top, or interpolate the peak from the bins around it.
- Noise and power spectral density: divide by the ENBW too, or use
scipy.signal.welch, which applies the right scaling (Spectral Estimation). - Coherent sampling (an integer number of cycles in the record) removes leakage without any window; converter testing uses it on purpose (Data Acquisition).
- Remove the mean before windowing: a large DC level leaks into the low bins through the window's main lobe and sidelobes.
- Transients shorter than the record (a hammer impact) should not be windowed with Hann, which would attenuate the part near the edges; use a rectangular window or an exponential window for decaying responses.
Guided lab: windows on a scope
Hand calculation. A , peak tone () analysed with a bin spacing that leaves it halfway between bins reads about with no window, with Hann and with flat-top.
Simulation. In the worked example, set the strong tone to : all three windows read and the rectangular skirt disappears.
What you would measure. On a scope or spectrum analyser with selectable FFT windows, change the generator frequency in small steps (for example , , with a bin spacing): the rectangular reading jumps by up to about , Hann by up to about and flat-top by less than . Expect differences of a few tenths of a dB from the instrument's own amplitude accuracy (Scope FFT).
Hazards and safe setup. Generator and scope only; no mains circuits.
Pitfalls
- Reading tone amplitudes with a Hann window but dividing by instead of ( low).
- Choosing flat-top to separate close tones (its main lobe is about bins wide).
- Using a rectangular window and reporting "harmonics" that are leakage skirts.
- Windowing a transient that is not centred in the record.
Practice
- With a Hann window, by how much does a tone halfway between bins read low, uncorrected for scalloping?
- What is the ENBW of a Hann window in bins, and what does it mean for a noise floor read with a 10 Hz bin spacing?
- You need the amplitude of a single tone to without knowing its exact frequency. Which window?
Answer
- ().
- bins: each bin collects the noise of , so the noise floor per bin is higher than with a rectangular window of the same spacing.
- Flat-top: its scalloping loss is below .
Test yourself
Spectral leakage occurs because
- the FFT is approximate
- the record does not hold a whole number of cycles, so its periodic extension has a jump
- the ADC has too few bits
- the sample rate is too low
The worst-case scalloping loss of the rectangular window is about
- 0.01 dB
- 1.42 dB
- 3.92 dB
- 13.3 dB
Which window is best for measuring the amplitude of a single tone?
- rectangular
- Hann
- Hamming
- flat-top
To read a tone's amplitude through a window , divide by
- the ENBW