TExcelTAD
Home › Fourier Analysis & Signals › Window Functions

Window Functions

A window is a weighting w[n]w[n] 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 n=0,…,N−1n = 0, \ldots, N-1:

Window w[n]w[n] Highest sidelobe Main lobe width
rectangular 1 −13-13 dB 2 bins
Hann 0.5−0.5cos⁡2πnN0.5 - 0.5\cos\frac{2\pi n}{N} −31-31 dB 4 bins
Hamming 0.54−0.46cos⁡2πnN0.54 - 0.46\cos\frac{2\pi n}{N} −42-42 dB 4 bins
Blackman 0.42−0.5cos⁡2πnN+0.08cos⁡4πnN0.42 - 0.5\cos\frac{2\pi n}{N} + 0.08\cos\frac{4\pi n}{N} −58-58 dB 6 bins

(Main-lobe widths are measured between the first zeros. NumPy's np.hanning(N) etc. use N−1N - 1 in the denominator, the "symmetric" form for filter design; spectral analysis usually prefers the "periodic" form above, scipy.signal.get_window("hann", N).)

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

python · try it yourself
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 −60-60 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.kaiser give 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 NN. What is ∣X[k]∣\lvert X[k] \rvert?

Answer

N/4N/4.

Solution

Without a window it would be N/2N/2. The Hann window's mean is 0.50.5, so the bin is scaled to N/2⋅0.5=N/4N/2 \cdot 0.5 = N/4. 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 −50-50 dB 50 bins away (they decay only 6 dB per octave). A 4-term Blackman-Harris has sidelobes below −90-90 dB, and resolution is not an issue at 50 bins' distance.

Exercise 3: Resolution loss (code)

Two equal tones at 20 and 22 Hz (N=64N = 64, fs=64f_s = 64, 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
python · try it yourself
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…

  1. lower sidelobes and a wider main lobe
  2. higher sidelobes
  3. a narrower main lobe
  4. no effect

Which window is best for accurate amplitude readings of off-bin tones?

  1. rectangular
  2. flat-top
  3. Hann
  4. none

Why divide by the window's mean when reading amplitudes?

  1. to remove noise
  2. the window scales every coherent component by its mean (coherent gain)
  3. to normalize the FFT size
  4. to remove DC