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
Window Functions
A window function is a finite-length sequence that is multiplied with a signal before computing the DFT to reduce spectral leakage. The windowed DFT is:
Common windows in scipy.signal.windows:
| Window | Main-Lobe Width | Sidelobe Level | Use Case |
|---|---|---|---|
| Rectangular | Narrowest () | dB | Maximum resolution |
| Hann | dB | General-purpose | |
| Hamming | dB | Speech/audio | |
| Blackman | dB | High dynamic range | |
| Kaiser () | 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 Family
The Kaiser window provides a continuous trade-off between main-lobe width and sidelobe attenuation via the parameter :
where is the zeroth-order modified Bessel function.
| Sidelobe Attenuation | Equivalent Window | |
|---|---|---|
| 0 | dB | Rectangular |
| 5 | dB | Hamming-like |
| 8.6 | dB | Blackman-like |
w = windows.kaiser(N, beta=8.6)
Definition: Periodogram Power Spectral Density Estimate
Periodogram Power Spectral Density Estimate
The periodogram estimates the power spectral density (PSD) as:
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 does not reduce its variance. Use Welch's method for a smoother estimate.
Definition: Welch's Method for PSD Estimation
Welch's Method for PSD Estimation
Welch's method reduces the variance of the periodogram by:
- Dividing the signal into overlapping segments
- Windowing each segment
- Computing the periodogram of each segment
- Averaging the periodograms
from scipy.signal import welch
f, Pxx = welch(x, fs=1000, nperseg=256, noverlap=128, window='hann')
With segments and 50% overlap, the variance is reduced by approximately a factor of . The trade-off: averaging reduces variance but also reduces frequency resolution to where is the segment length.
Definition: Short-Time Fourier Transform (STFT)
Short-Time Fourier Transform (STFT)
The STFT applies the DFT to successive windowed segments of a signal, producing a time-frequency representation:
where is the window length and 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 , 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 , the time spread and frequency spread satisfy:
Equality holds only for the Gaussian pulse .
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.
Generate signal and apply windows
from scipy.signal import windows
import numpy as np
fs = 1000
t = np.arange(0, 1, 1/fs)
x = np.sin(2*np.pi*100*t) + 0.01*np.sin(2*np.pi*105*t)
for name, w_func in [("Rectangular", windows.boxcar),
("Hann", windows.hann),
("Blackman", windows.blackman)]:
w = w_func(len(x))
X = np.fft.fft(w * x, n=8192)
mag_db = 20 * np.log10(np.abs(X[:4096]) / np.max(np.abs(X)))
The Blackman window's dB sidelobes reveal the weak 105 Hz tone, while the rectangular window's dB sidelobes mask it.
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.
Compute both estimates
from scipy.signal import periodogram, welch
import numpy as np
fs = 1000
t = np.arange(0, 10, 1/fs)
x = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t) + np.random.randn(len(t))
f_per, P_per = periodogram(x, fs=fs, window='hann')
f_welch, P_welch = welch(x, fs=fs, nperseg=1024, noverlap=512)
The Welch estimate is much smoother, clearly showing the two spectral peaks above the noise floor, while the periodogram is noisy and harder to interpret.
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.
Generate and analyze
from scipy.signal import chirp, spectrogram
import numpy as np
fs = 2000
t = np.arange(0, 2, 1/fs)
x = chirp(t, f0=100, f1=400, t1=2, method='linear')
f, t_spec, Sxx = spectrogram(x, fs=fs, nperseg=256, noverlap=240)
The spectrogram shows a bright diagonal line from (0 s, 100 Hz) to (2 s, 400 Hz), clearly revealing the linear frequency sweep.
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
# Code from: ch07/python/spectral_analysis.py
# Load from backend supplements endpointQuick 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
Averaging independent (or partially overlapping) periodograms reduces variance by approximately .
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 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., V/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
| Window | Main-Lobe Width | Peak Sidelobe | Best For |
|---|---|---|---|
| Rectangular | dB | Maximum resolution, no dynamic range needed | |
| Hann | dB | General-purpose spectral analysis | |
| Hamming | dB | Speech processing, moderate dynamic range | |
| Blackman | dB | High dynamic range, weak signal detection | |
| Kaiser () | dB | Flexible trade-off via |