Window Functions
A window is a weighting that tapers a frame smoothly toward zero at its ends before the FFT. It removes the boundary jump that causes spectral leakage, at the price of a wider main peak. Choosing a window is choosing a trade-off between seeing weak tones far away (low sidelobes) and separating close tones (narrow main lobe).
The common windows
For :
| Window | Highest sidelobe | Main lobe width | |
|---|---|---|---|
| rectangular | 1 | dB | 2 bins |
| Hann | dB | 4 bins | |
| Hamming | dB | 4 bins | |
| Blackman | dB | 6 bins |
(Main-lobe widths are measured between the first zeros. NumPy's np.hanning(N) etc. use in the denominator, the "symmetric" form for filter design; spectral analysis usually prefers the "periodic" form above, scipy.signal.get_window("hann", N).)
import numpy as np
N, pad = 64, 64 * 64
n = np.arange(N)
windows = {
"rectangular": np.ones(N),
"hann": 0.5 - 0.5 * np.cos(2 * np.pi * n / N),
"hamming": 0.54 - 0.46 * np.cos(2 * np.pi * n / N),
"blackman": 0.42 - 0.5 * np.cos(2 * np.pi * n / N) + 0.08 * np.cos(4 * np.pi * n / N),
}
for name, w in windows.items():
W = np.abs(np.fft.rfft(w, pad))
W_db = 20 * np.log10(W / W[0] + 1e-15)
first_null = np.argmax(np.diff(W) > 0) # main lobe ends where |W| starts rising again
print(f"{name:12s} main lobe half-width {first_null * N / pad:.2f} bins highest sidelobe {W_db[first_null:].max():6.1f} dB"
f" coherent gain {w.mean():.2f}")rectangular main lobe half-width 1.00 bins highest sidelobe -13.3 dB coherent gain 1.00
hann main lobe half-width 2.00 bins highest sidelobe -31.5 dB coherent gain 0.50
hamming main lobe half-width 2.00 bins highest sidelobe -42.4 dB coherent gain 0.54
blackman main lobe half-width 3.00 bins highest sidelobe -58.1 dB coherent gain 0.42
The coherent gain (the window's mean) scales every amplitude reading; divide by it to correct amplitudes.
Revealing a weak tone
The previous lesson's weak tone, 60 dB below a strong neighbour 20 Hz away, was buried by leakage. With a Blackman window it appears:
import numpy as np
N, fs = 256, 256.0
n = np.arange(N)
t = n / fs
x = np.sin(2 * np.pi * 40.5 * t) + 0.001 * np.sin(2 * np.pi * 60 * t)
for name, w in (("rectangular", np.ones(N)),
("blackman", 0.42 - 0.5 * np.cos(2 * np.pi * n / N) + 0.08 * np.cos(4 * np.pi * n / N))):
A = 20 * np.log10(np.abs(np.fft.rfft(x * w)) / (N / 2 * w.mean()) + 1e-12)
print(f"{name:12s} level at 56, 58, 60, 62, 64 Hz:", np.round(A[[56, 58, 60, 62, 64]], 1).tolist())rectangular level at 56, 58, 60, 62, 64 Hz: [-34.6, -35.7, -36.7, -37.6, -38.4]
blackman level at 56, 58, 60, 62, 64 Hz: [-88.9, -80.1, -60.0, -80.3, -99.7]
With the Blackman window the surroundings drop far below dB and the weak tone stands out as a clear peak at 60 Hz.
The trade-off
Wider main lobes blur close tones together. Two equal tones 2 bins apart are separated by the rectangular window but merge under Blackman. There is no best window, only the right one for the question:
- Rectangular: transients, or tones known to be on bins; best resolution.
- Hann: the default for general spectrum analysis and STFTs.
- Blackman, Blackman-Harris, Kaiser (adjustable): large dynamic range.
- Flat-top: accurate amplitude readings (almost no scalloping loss), poor resolution.
In software
scipy.signal.get_window(name, N)provides dozens of windows;np.hanning,np.hamming,np.blackman,np.kaisergive the symmetric versions.- Windows reappear in FIR filter design (windowed-sinc filters) and in the STFT.
Exercises
Exercise 1: Coherent gain
A tone of amplitude 1 on a bin is analysed with a Hann window of length . What is ?
Answer
.
Solution
Without a window it would be . The Hann window's mean is , so the bin is scaled to . Divide by the coherent gain to recover the amplitude.
Exercise 2: Choose a window
You need to detect a tone 80 dB below a strong interferer that is 50 bins away. Rectangular or Blackman-Harris?
Answer
Blackman-Harris (or another low-sidelobe window).
Solution
The rectangular window's sidelobes are still around dB 50 bins away (they decay only 6 dB per octave). A 4-term Blackman-Harris has sidelobes below dB, and resolution is not an issue at 50 bins' distance.
Exercise 3: Resolution loss (code)
Two equal tones at 20 and 22 Hz (, , so 2 bins apart). Count the peaks between 15 and 27 Hz after zero padding to 4096, with rectangular and Blackman windows.
Answer
Rectangular: 2 peaks; Blackman: 1 peak.
Solution
import numpy as np
N, fs = 64, 64.0
n = np.arange(N)
x = np.sin(2 * np.pi * 20 * n / fs) + np.sin(2 * np.pi * 22 * n / fs)
for name, w in (("rect", np.ones(N)), ("blackman", 0.42 - 0.5 * np.cos(2 * np.pi * n / N) + 0.08 * np.cos(4 * np.pi * n / N))):
A = np.abs(np.fft.rfft(x * w, 4096))
f = np.fft.rfftfreq(4096, 1 / fs)
band = np.where((f > 15) & (f < 27))[0]
peaks = [i for i in band if A[i] > A[i - 1] and A[i] > A[i + 1] and A[i] > 0.5 * A[band].max()]
print(name, len(peaks))rect 2
blackman 1
Test yourself
Compared with the rectangular window, a Hann window has…
- lower sidelobes and a wider main lobe
- higher sidelobes
- a narrower main lobe
- no effect
Which window is best for accurate amplitude readings of off-bin tones?
- rectangular
- flat-top
- Hann
- none
Why divide by the window's mean when reading amplitudes?
- to remove noise
- the window scales every coherent component by its mean (coherent gain)
- to normalize the FFT size
- to remove DC