Window Functions and Spectral Analysis

Why Windows Matter

When you compute the FFT of a finite signal, you implicitly multiply it by a rectangular window. This abrupt truncation causes spectral leakage: energy from a single frequency spreads across many bins. Window functions taper the signal's edges to zero, trading main-lobe width (resolution) for sidelobe suppression (dynamic range). Choosing the right window is the key design decision in spectral analysis.

Definition:

Window Functions

A window function w[n]w[n] is a finite-length sequence that is multiplied with a signal before computing the DFT to reduce spectral leakage. The windowed DFT is:

Xw[k]=βˆ‘n=0Nβˆ’1w[n] x[n] eβˆ’j2Ο€kn/NX_w[k] = \sum_{n=0}^{N-1} w[n]\, x[n]\, e^{-j2\pi kn/N}

Common windows in scipy.signal.windows:

Window Main-Lobe Width Sidelobe Level Use Case
Rectangular Narrowest (2/N2/N) βˆ’13-13 dB Maximum resolution
Hann 4/N4/N βˆ’31-31 dB General-purpose
Hamming 4/N4/N βˆ’43-43 dB Speech/audio
Blackman 6/N6/N βˆ’58-58 dB High dynamic range
Kaiser (Ξ²\beta) Adjustable Adjustable Flexible trade-off
from scipy.signal import windows
w = windows.hann(N)
X = np.fft.fft(w * x)

Definition:

The Kaiser Window Family

The Kaiser window provides a continuous trade-off between main-lobe width and sidelobe attenuation via the parameter Ξ²\beta:

w[n]=I0 ⁣(Ξ²1βˆ’(2nNβˆ’1βˆ’1)2)I0(Ξ²)w[n] = \frac{I_0\!\left(\beta\sqrt{1 - \left(\frac{2n}{N-1} - 1\right)^2}\right)}{I_0(\beta)}

where I0I_0 is the zeroth-order modified Bessel function.

Ξ²\beta Sidelobe Attenuation Equivalent Window
0 βˆ’13-13 dB Rectangular
5 βˆ’36-36 dB Hamming-like
8.6 βˆ’60-60 dB Blackman-like
w = windows.kaiser(N, beta=8.6)

Definition:

Periodogram Power Spectral Density Estimate

The periodogram estimates the power spectral density (PSD) as:

S^xx(fk)=1Nfs∣X[k]∣2\hat{S}_{xx}(f_k) = \frac{1}{N f_s} |X[k]|^2

In SciPy:

from scipy.signal import periodogram
f, Pxx = periodogram(x, fs=1000, window='hann')

The periodogram is the simplest PSD estimator but has high variance: its standard deviation equals its mean, regardless of signal length.

The periodogram is an inconsistent estimator β€” increasing NN does not reduce its variance. Use Welch's method for a smoother estimate.

Definition:

Welch's Method for PSD Estimation

Welch's method reduces the variance of the periodogram by:

  1. Dividing the signal into overlapping segments
  2. Windowing each segment
  3. Computing the periodogram of each segment
  4. Averaging the periodograms
from scipy.signal import welch
f, Pxx = welch(x, fs=1000, nperseg=256, noverlap=128, window='hann')

With KK segments and 50% overlap, the variance is reduced by approximately a factor of 9K/119K/11. The trade-off: averaging reduces variance but also reduces frequency resolution to Ξ”f=fs/L\Delta f = f_s / L where LL is the segment length.

Definition:

Short-Time Fourier Transform (STFT)

The STFT applies the DFT to successive windowed segments of a signal, producing a time-frequency representation:

STFT{x}(m,k)=βˆ‘n=0Lβˆ’1w[n] x[n+mH] eβˆ’j2Ο€kn/L\text{STFT}\{x\}(m, k) = \sum_{n=0}^{L-1} w[n]\, x[n + mH]\, e^{-j2\pi kn/L}

where LL is the window length and HH is the hop size.

from scipy.signal import stft, spectrogram
f, t, Zxx = stft(x, fs=1000, nperseg=256, noverlap=192)
f, t, Sxx = spectrogram(x, fs=1000, nperseg=256, noverlap=192)

spectrogram returns ∣Zxx∣2|Zxx|^2, the squared magnitude of the STFT.

The STFT has a fixed time-frequency resolution determined by the window length: long windows give good frequency resolution but poor time resolution, and vice versa. This is a fundamental limitation captured by the uncertainty principle.

Theorem: Time-Frequency Uncertainty Principle

No signal can be simultaneously localized in both time and frequency. For any signal x(t)x(t), the time spread Οƒt\sigma_t and frequency spread Οƒf\sigma_f satisfy:

Οƒtβ‹…Οƒfβ‰₯14Ο€\sigma_t \cdot \sigma_f \ge \frac{1}{4\pi}

Equality holds only for the Gaussian pulse x(t)=eβˆ’Ξ±t2x(t) = e^{-\alpha t^2}.

A short pulse (good time localization) must contain many frequencies (poor frequency localization). A pure sinusoid (perfect frequency localization) extends infinitely in time. The STFT window length controls where you sit on this trade-off.

Example: Effect of Window Choice on Spectral Analysis

A signal contains a strong tone at 100 Hz and a weak tone at 105 Hz that is 40 dB below. Compare the ability of different windows (rectangular, Hann, Blackman) to reveal the weak tone.

Example: Welch's Method vs Periodogram

Compare the periodogram and Welch's method for estimating the PSD of a signal consisting of two sinusoids in white noise.

Example: Spectrogram of a Linear Chirp

Generate a linear chirp signal from 100 Hz to 400 Hz over 2 seconds and compute its spectrogram to visualize the time-frequency content.

Window Function Comparison

Compare different window functions in both the time domain and frequency domain. Observe the trade-off between main-lobe width and sidelobe level.

Parameters

Spectral Analysis Recipes

python
Complete examples: window functions, periodogram, Welch PSD, spectrogram, and STFT with various windowing options.
# Code from: ch07/python/spectral_analysis.py
# Load from backend supplements endpoint

Quick Check

Welch's method reduces the variance of the PSD estimate by:

Using a longer FFT

Averaging periodograms of overlapping windowed segments

Applying zero-padding

Using a wider window

Common Mistake: Using Rectangular Window for Spectral Analysis

Mistake:

Computing the FFT without applying a window function first. This implicitly uses a rectangular window, which has βˆ’13-13 dB sidelobes that can mask weak spectral components near strong ones.

Correction:

Always apply a window before the FFT for spectral analysis:

from scipy.signal import windows
w = windows.hann(len(x))
X = np.fft.fft(w * x)

Key Takeaway

Window functions trade frequency resolution for sidelobe suppression. Use Welch's method instead of the periodogram for smoother PSD estimates. The spectrogram (STFT magnitude squared) reveals time-varying frequency content but has fixed time-frequency resolution governed by the uncertainty principle.

Power Spectral Density (PSD)

The distribution of signal power as a function of frequency, measured in units of power per Hz (e.g., V2^2/Hz).

Related: Periodogram Power Spectral Density Estimate, Welch's method

STFT (Short-Time Fourier Transform)

A time-frequency representation obtained by applying the DFT to successive windowed segments of a signal.

Related: Spectrogram, window function

Why This Matters: Spectrum Sensing in Cognitive Radio

Cognitive radios use spectral analysis to detect whether a frequency band is occupied by a primary user. The Welch periodogram is a standard approach: if the estimated PSD in a band exceeds a threshold, the band is declared occupied. The trade-off between detection probability and false alarm rate maps directly to the segment length and overlap parameters. Window choice affects the ability to detect weak signals near strong interferers.

See full treatment in Chapter 22

Window Functions at a Glance

WindowMain-Lobe WidthPeak SidelobeBest For
Rectangular2Ξ”f2\Delta fβˆ’13-13 dBMaximum resolution, no dynamic range needed
Hann4Ξ”f4\Delta fβˆ’31-31 dBGeneral-purpose spectral analysis
Hamming4Ξ”f4\Delta fβˆ’43-43 dBSpeech processing, moderate dynamic range
Blackman6Ξ”f6\Delta fβˆ’58-58 dBHigh dynamic range, weak signal detection
Kaiser (Ξ²=8.6\beta=8.6)∼5Ξ”f\sim 5\Delta fβˆ’60-60 dBFlexible trade-off via Ξ²\beta