4. Windowing & Spectral Leakage

Why your spectrum looks wrong — and how to fix it

You take the FFT of a sine wave and expect one clean spike at the right frequency. Instead you get something that looks like an explosion — energy smeared across dozens of bins, with the true peak barely distinguishable from the noise. Welcome to spectral leakage: the most common trap in frequency analysis, and one that has a surprisingly elegant fix.

Why Leakage Happens

The DFT does not analyse an infinite continuous signal. It analyses a finite block of N samples — and to make the mathematics work out neatly, it implicitly assumes that those N samples represent one complete period of an infinitely repeating signal. In other words, the DFT assumes the signal is periodic with period N.

If the signal happens to contain exactly an integer number of cycles within the N-sample window, this assumption is fine. The end of the block connects smoothly to the beginning of the next imaginary repetition. But if the frequency is not an exact multiple of the bin spacing fs / N, then the end of the block is at a different amplitude than the beginning — and when the DFT mentally joins them up end-to-end, it sees a sharp discontinuity.

Diagram

Spectral leakage

N samples (analysis window) discontinuity! start end
A sine wave with 3.3 cycles in the window. The signal ends at a different value than it starts, so the DFT's implicit periodic extension has a sharp jump — and sharp jumps contain energy at all frequencies.

A sharp discontinuity in the time domain produces energy at all frequencies in the DFT. That energy leaks from the true bin into every other bin in the spectrum — hence the name.

The Rectangular Window

Here is the key insight most introductions skip: not windowing your data is itself a windowing decision. When you hand N samples to the FFT without any pre-processing, you are implicitly multiplying the signal by a function that is 1 for every sample in the window and 0 outside it. This is the rectangular window (also called the boxcar window).

Multiplying by a rectangle in the time domain is equivalent to convolving with a sinc function in the frequency domain. The sinc function has a sharp main lobe, but its sidelobes decay very slowly — only at 13 dB below the main lobe for the first sidelobe. Every bin's content bleeds into its neighbours at that level. If one frequency is much louder than another nearby, the loud one's sidelobes can completely bury the quiet one.

Windowing

The solution is to choose a window function that tapers smoothly to zero at both ends of the block. When the signal is multiplied by such a taper, the discontinuity at the edge disappears — the signal goes to zero at both endpoints, so the periodic extension is smooth. No discontinuity means no leakage.

Diagram

Window function shapes

n w(n) 1 0.5 Rectangular Hann Hamming Blackman
Window function shapes. The rectangular window (dashed) is flat — no tapering. Hann and Blackman go smoothly to zero at both edges. Hamming dips to about 0.08 rather than zero, giving it slightly different sidelobe characteristics.

The procedure is simple: before computing the FFT, multiply each sample x[n] by the window value w[n], producing the windowed signal xw[n] = x[n] · w[n].

Common Window Functions

Four windows cover the vast majority of practical needs:

Hann window w[n] = 0.5 · (1 − cos(2πn / N))

Also (incorrectly) called the Hanning window. Goes smoothly to zero at both ends. First sidelobe at −31 dB, sidelobes roll off at 18 dB per octave. A reliable all-purpose choice — when in doubt, use Hann.

Hamming window w[n] = 0.54 − 0.46 · cos(2πn / N)

Deliberately does not go all the way to zero at the edges. This choice was optimised to cancel the first sidelobe, pushing the first sidelobe to −43 dB — lower than Hann's −31 dB. However, the distant sidelobes decay more slowly. Better than Hann when you care most about the nearest-frequency leakage.

Blackman window w[n] = 0.42 − 0.5 · cos(2πn / N) + 0.08 · cos(4πn / N)

Uses a second cosine term to push all sidelobes lower. First sidelobe at −58 dB, and they continue to fall off rapidly. The price: the main lobe is wider than Hann or Hamming, meaning slightly worse frequency resolution. Good when you need high dynamic range — detecting a weak signal alongside a much stronger one at a nearby frequency.

Kaiser window Parameterised by a shape parameter β that lets you continuously trade main-lobe width against sidelobe level. The professional's tool when precise control over the resolution/leakage trade-off is needed. Most audio and communications toolkits provide it as scipy.signal.windows.kaiser(N, beta) or equivalent.

Window Properties: The Fundamental Trade-Off

Every window function sits on a curve in the trade-off between two competing properties:

  • Main lobe width: how wide the DFT spike for a single pure tone appears. Narrower = better frequency resolution — you can distinguish two close-together frequencies.
  • Sidelobe level: how much energy from one frequency contaminates adjacent bins. Lower = better dynamic range — you can detect a weak frequency next to a strong one.
Diagram

Resolution vs. leakage trade-off

f dB 0 −20 −40 −55 f₀ −13 dB sidelobe Rectangular (narrow lobe, high sidelobes) Hann Blackman (wide lobe, low sidelobes)
Frequency domain magnitude of each window. The rectangular window has the narrowest main lobe (best resolution) but the highest sidelobes (worst leakage). Blackman has the lowest sidelobes but the widest main lobe. Every window sits on this trade-off curve.
The Fundamental Trade-Off: You cannot simultaneously have perfect frequency resolution and perfect leakage suppression. Narrowing the main lobe widens the sidelobes; pushing sidelobes down widens the main lobe. Window selection is choosing where on this curve your application sits.

How to Choose a Window

The right window depends on what you are trying to measure:

Use rectangular when: all the frequencies you care about fall exactly on DFT bin centers (e.g. you are generating test signals digitally and know the exact frequencies). You get the narrowest main lobe and thus the best resolution. Any other case — avoid it.

Use Hann when: you need a general-purpose window. It is the default choice in audio analysis, vibration monitoring, and communications. Good sidelobe suppression with acceptable main-lobe width.

Use Hamming when: you are particularly concerned about leakage from immediately adjacent bins. The Hamming window's optimised first sidelobe makes it slightly better than Hann for resolving nearby equal-amplitude tones.

Use Blackman when: you need high dynamic range — for example, detecting a weak tone at −50 dB alongside a strong one. The extra sidelobe suppression is worth the slightly reduced frequency resolution.

Audio analysis rule of thumb: For real-time spectrum displays (the kind you see in audio editors), Hann is almost always correct. For detecting low-level distortion harmonics in high-fidelity audio measurements, use Blackman. For DTMF or other tone-detection systems where you know the exact bin frequencies, rectangular may be appropriate.

Note that windowing scales down the signal amplitude — a Hann window reduces the average signal power. If you need accurate amplitude measurements, you must apply a coherent gain correction by dividing the FFT output by the sum of the window coefficients (∑w[n] / N). Most spectrum analyser libraries do this automatically.

Key Takeaways

  • Spectral leakage occurs because the DFT assumes the N-sample block is one period of an infinitely periodic signal. A non-integer number of cycles creates a discontinuity at the boundary.
  • Not applying a window is the same as applying a rectangular window — and the rectangular window has poor sidelobe behaviour (−13 dB first sidelobe).
  • Windowing multiplies the signal by a taper function that goes to zero at both ends, eliminating the edge discontinuity.
  • Hann: 0.5(1 − cos(2πn/N)) — good all-purpose window, −31 dB first sidelobe.
  • Hamming: 0.54 − 0.46·cos(2πn/N) — optimised first sidelobe at −43 dB, but slower far-sidelobe rolloff.
  • Blackman: 0.42 − 0.5·cos(2πn/N) + 0.08·cos(4πn/N) — lowest sidelobes at −58 dB, widest main lobe.
  • Every window trades resolution (main-lobe width) against dynamic range (sidelobe level). There is no free lunch.
  • For most audio and signal work, Hann is the default choice. Use Blackman when high dynamic range matters more than resolution.

Try It — Windowing Visualizer

Set the signal frequency to a non-integer value (like 7.3) to see leakage in action. Switch between window functions to watch the smearing disappear. Then move the frequency to a whole number and observe that rectangular has zero leakage for integer-bin frequencies.

7.3
64