Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Applications

Spectral Analysis, Leakage and Windowing

Understand spectral leakage in the DFT, the trade-off between main-lobe width and side-lobe level, window functions, resolution, and Welch's method for noisy data.

The DFT assumes that the N samples you give it are one period of a periodic signal. A real record is a slice cut out of something longer. If the slice does not contain a whole number of periods, the "periodic extension" has a jump at the seam, and that jump spreads energy across the whole spectrum. This is spectral leakage, and it is the most common reason that FFT plots look messier than expected.

Leakage in numbers

Take fs = 1000 Hz and N = 1000 samples, so each bin is 1 Hz wide. A 50 Hz sine fits exactly 50 periods and lands in one bin. A 50.5 Hz sine fits 50.5 periods: its energy is smeared over many bins, with side lobes that fall off slowly (like 1/distance).

Cutting a record is the same as multiplying the infinite signal by a rectangular window. In frequency that is a convolution with the transform of a rectangle, the sinc from earlier lessons. The sinc has a main lobe and side lobes; the sine's single line is replaced by that shape.

Python Exact bin versus in-between frequency

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The exact-bin tone has essentially nothing away from its bin (the tiny numbers are rounding noise), while the off-bin tone loses about 3.9 dB at its peak and leaves a tail of leakage around -55 to -70 dB. That is small, but a weak signal near a strong one could vanish under it.

Windows

Multiplying the record by a tapered window that goes smoothly to zero at both ends removes the jump at the seam. The price is a wider main lobe, which costs resolution.

Window Side-lobe level Main-lobe width Typical use
Rectangular about -13 dB narrowest exact-bin tones, transients
Hann about -31 dB 2x wider general purpose
Hamming about -43 dB 2x wider speech, narrowband
Blackman about -58 dB 3x wider high dynamic range
Flat-top very low widest accurate amplitude readings

Because a window reduces the signal's amplitude, correct for the coherent gain (the window's average) when you need peak amplitudes.

Python Windows suppress far-away leakage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Dividing by the sum of the window removes the coherent-gain loss, but the peak still reads below 1 because 50.5 Hz falls between two bins (the scalloping loss; a flat-top window minimizes it). The leakage far from the tone drops by tens of decibels for Hann and Blackman.

Resolution and the uncertainty trade-off

Two tones can be told apart only if they are separated by more than the main-lobe width, roughly fs/N for a rectangular window and about twice that for Hann. The only way to improve resolution is a longer observation (a larger N), not zero-padding. The time-frequency uncertainty principle explains it: sharper frequency knowledge needs a longer time span.

Python Resolving two close tones

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a 3 Hz separation, N = 200 (5 Hz bins) merges the tones into one peak, while N = 400 and N = 1000 show two. Longer records also put the peaks closer to the true 100 and 103 Hz. A Hann window needs a longer record than a rectangular one when the tones are closer still, because its main lobe is wider.

Welch's method for noisy signals

A single FFT of noisy data is itself noisy: its variance does not shrink as N grows. Welch's method splits the record into overlapping segments, windows each, computes the squared magnitude and averages. The result is a smooth power spectral density estimate at the cost of lower resolution. In SciPy, use scipy.signal.welch.

Use scipy.signal.welch(x, fs, nperseg=...) for noise and random signals, and a windowed single FFT for deterministic tones. Larger nperseg gives finer resolution; more segments give lower variance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What causes spectral leakage?

    The DFT treats the record as periodic; a partial period causes a jump at the boundary and spreads energy across bins.
  2. 2. What is the main trade-off when you apply a Hann window instead of a rectangular one?

    Tapering reduces side lobes (less leakage) but widens the main lobe (less resolution).
  3. 3. How do you improve the frequency resolution of a spectrum?

    Resolution is about fs/N, which equals 1 over the observation time. Only longer observation improves it.
  4. 4. What does Welch's method do?

    Averaging segment periodograms gives a smoother PSD estimate, at the cost of lower resolution.

Summary

  • A finite record multiplies the signal by a rectangular window; its sinc-shaped spectrum causes leakage.
  • Tapered windows (Hann, Hamming, Blackman) cut side lobes but widen the main lobe.
  • Resolution is fs/N: longer observation, not padding, separates close tones.
  • Welch averaging gives a stable PSD for noisy signals.

Next: Project: A Tone Detector with the Goertzel Algorithm