Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Spectral Analysis

The STFT and Spectrograms

Build a spectrogram from scratch with the short-time Fourier transform, track a chirp over time, and understand the time-frequency resolution trade-off.

A single FFT tells you which frequencies are in a recording but not when. A siren, a spoken word, a bird call or a radar chirp changes its frequency over time. The short-time Fourier transform (STFT) handles this by cutting the signal into short, overlapping frames, windowing each one and taking an FFT per frame. Stacking the magnitude spectra side by side gives the spectrogram: frequency on one axis, time on the other.

Building an STFT by hand

For frame length L and hop size H, frame m covers samples mH to mH + L. Each frame is multiplied by a window and transformed. This is simple enough to write in a few lines.

Python STFT from scratch

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.

Early frames show only 50 Hz; later frames show 50 Hz and 200 Hz. The time axis of the STFT is m * H / fs for the start of frame m (or + L/(2 fs) for its center).

Tracking a chirp

A chirp is a sinusoid whose frequency sweeps. Its spectrogram is a ridge. scipy.signal.chirp generates one; scipy.signal.spectrogram computes the STFT power and returns the time and frequency axes.

Python Following a linear chirp

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 ridge follows the expected instantaneous frequency 100 + 250 t within one bin width (fs / nperseg, about 7.8 Hz).

Time versus frequency resolution

You cannot have both. A frame of length L samples has frequency resolution about fs / L and time resolution about L / fs. Their product is fixed, a version of the uncertainty principle.

Choose the frame length

Short frame Sharp in time, blurry in pitch
Long frame Sharp in pitch, blurry in time
Compromise Match frame to the feature size

Speech often uses 20-30 ms frames.

1 / 2
Python Two tones close in time

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 L = 64 the bins are 62.5 Hz wide, so the ridge only takes coarse values (375 and 438 Hz here) and the 40 Hz difference is measured poorly, but the activity lasts 40 ms, which is the true length of the two bursts. With L = 512 the ridge is read to within about 8 Hz, yet the activity smears to about 116 ms. Pick the frame to suit the feature you care about.

Overlap and reconstruction

With a Hann window, 50% overlap sums to a constant (the constant overlap-add or COLA condition). That permits inverse STFT: IFFT each frame and overlap-add them. This is how spectral audio effects and denoisers work: change the STFT, then resynthesize. scipy.signal.ShortTimeFFT implements both directions.

Reading a spectrogram

  • Use dB (10*log10(S)) with a dynamic range of 60-80 dB; linear scaling hides weak components.
  • Horizontal lines are steady tones, vertical lines are clicks, slanted lines are chirps, blobs are noise bursts.
  • Harmonics appear as parallel lines at integer multiples of a fundamental, characteristic of voiced speech and instruments.
  • Mind the axes: time is the frame center, and a window of length L blurs events by about L/fs.

The spectrogram connects naturally to pitch detection and to the filter banks you use in applications.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three steps of an STFT frame?

    Cut a frame, multiply it by a window and take its FFT; repeating this over hops gives the time-frequency picture.
  2. 2. What happens to resolution when the frame length L is doubled?

    Frequency bins get twice as narrow (fs/L) while each frame spans twice as much time, so events are smeared more.
  3. 3. What does a slanted straight line in a spectrogram represent?

    A frequency that increases or decreases steadily over time draws a diagonal ridge.
  4. 4. Why use 50% overlap with a Hann window?

    Hann windows at 50% overlap satisfy the COLA condition, so the inverse STFT by overlap-add reproduces the signal.

Summary

  • The STFT slices a signal into windowed frames and takes an FFT of each; a spectrogram is its magnitude (or power) image.
  • Frequency resolution is about fs/L, time resolution about L/fs; you must trade one for the other.
  • Chirps appear as ridges, harmonics as parallel lines.
  • Overlapped frames allow inverse STFT, the base of spectral processing.

Next: Zero Padding, Resolution and Peak Interpolation