Time-Frequency Analysis (STFT and Spectrogram)

The short-time Fourier transform (STFT) computes a frequency snapshot for each time by sliding a length- window function across the data and taking a DFT of each windowed segment:

where slides across the record (typically in steps smaller than so that windows overlap). The squared modulus is the spectrogram: a 2-D function of time and frequency, conventionally displayed as a heat map with time on the horizontal axis and frequency on the vertical axis.

The motivation is that the standard DFT and spectral density assume the spectrum is constant in time. For non-stationary signals (speech, music, EEG, seismic events, financial volatility regimes) the analyst’s question is not “what frequencies are present” but “when is each frequency present.” The STFT trades a single global spectrum for a sequence of local spectra, one per window, recovering the time information that the global DFT discards.

The trade-off is governed by the time-frequency uncertainty principle (Heisenberg-Gabor): a window of length samples gives frequency resolution and time resolution , so the product is bounded below by a constant. Sharper time localization forces blurrier frequency resolution and vice versa, and the choice of is the central design knob.

Window Choice Matters scipy.signal.spectrogram) is a Tukey or Hanning window, never a rectangle.

A rectangular window has sharp edges that produce strong spectral leakage (its Fourier transform is a sinc, with large side-lobes). Tapered windows like Hanning, Hamming, and Gaussian smooth the edges and dramatically reduce leakage at the cost of slightly broader main lobes. The default in production tools (

Intuition

The DFT of an entire record asks “how much of frequency is in the data, summed over the whole history?” If a Hz tone plays for the first second of a -second clip and silence follows, the DFT registers Hz as moderately strong but loses the fact that it was confined to the start. The STFT fixes this by chopping the record into overlapping windows, taking a DFT of each, and arranging the results into a 2-D image: time on one axis, frequency on the other, color showing power. The Hz tone now appears as a bright horizontal stripe confined to the first second.

The unavoidable trade-off is that a window narrow enough to localize the tone in time is too short to nail down its frequency precisely. This is not a defect of the algorithm; it is a mathematical theorem (Heisenberg-Gabor) that any time-frequency representation built from a single window obeys . For signals where the frequency content evolves at very different rates in different bands (low frequencies change slowly, high frequencies change quickly), wavelet transforms replace the fixed window with a frequency-dependent window, but that is a different note.

Properties

(Inheriting from the DFT) Each fixed-time slice of the spectrogram is the periodogram of the windowed segment.

So everything that’s true of the periodogram holds within each STFT column: the same noise distribution at each frequency bin, the same need for smoothing across nearby bins, the same sensitivity to mean and trend within the window. Welch’s method (averaging periodograms across multiple windows) is essentially an STFT with no time axis displayed.

(Window Length Trade-Off) Frequency resolution ; time resolution .

A window of samples at sampling rate Hz spans s of time and resolves frequencies down to Hz. Halving doubles the time resolution but doubles the frequency bin width. The right depends entirely on the time scale at which the spectral content is changing: short windows for fast transients, long windows for slowly varying tones.

(Hop Size and Overlap) Adjacent windows are typically separated by a hop , giving overlap .

The default overlap is (). Smaller hops give a smoother spectrogram in the time axis at higher computational cost; hops leave gaps the window’s smoothing cannot fill. The combination of overlap and a tapered window provides time-resolution finer than the window length itself by interpolating between adjacent windowed estimates.

(Window Choice) Tapered windows suppress leakage at the cost of broader main lobes.

The Hanning window is the default-of-defaults: dB side-lobes versus dB for a rectangular window, with only modest main-lobe broadening. The Kaiser window has a tunable parameter that lets the user dial the side-lobe / main-lobe trade-off. Choice of window matters most when looking for weak frequency components near strong ones, since side-lobes from the strong component can mask the weak one entirely.

(Reconstruction) Overlapping windows that satisfy the constant-overlap-add condition allow exact inverse-STFT.

If for some constant , the original signal can be exactly recovered from the STFT by inverting each windowed segment and summing. This makes the STFT a basis for filtering operations performed in the time-frequency plane: zero out spectrogram cells that contain noise, then inverse-STFT to reconstruct a cleaned signal. The technique underlies modern noise reduction (spectral subtraction) and time-stretching (phase vocoder).

(Multitaper Extension) Replacing one window with several orthogonal Slepian tapers reduces variance at fixed time-frequency resolution.

The multitaper method uses orthogonal Slepian tapers, computes the spectrogram with each, and averages the results. This reduces variance by a factor of at fixed time-frequency resolution, a significant variance reduction at no cost in resolving power. Implemented in MNE-Python’s tfr_multitaper and nitime.algorithms.multi_taper_psd. Standard in neuroscience and seismology where the signal-to-noise per spectrogram bin is low.

Examples

Chirp signal

For for (a sinusoid whose frequency rises linearly from to over the record), what does the spectrogram look like, and how does window length change it?

Detecting a transient burst

An EEG channel contains a brief Hz “gamma burst” in a single -ms window of an otherwise quiet -second recording, sampled at Hz. What window length should you use?