Spectral features inherit the frame, window, FFT, and normalization contract
Calculate STFT dimensions and timing, distinguish bin spacing from resolving power, and normalize amplitudes without confusing spectra, power, and density.
On this page
The question for today
What you’ll understand
- Calculate frame count, hop duration, one-sided bins, and FFT frequency spacing.
- Explain leakage, window gain, and why zero padding adds grid points rather than new observations.
- Distinguish causal framing and raw-transform scaling from centered offline API conventions.
A spectrogram is not a parameter-free picture of speech. It is an array produced by a window length, hop, transform length, boundary policy, and scaling convention. Those choices determine feature dimensions, timestamps, latency, and the interpretation of magnitudes. A recognizer trained on one convention can receive systematically different inputs from another even when the waveform is unchanged. Write the contract in samples and units before treating a familiar shape or a visually plausible heat map as evidence that preprocessing matches the model.
Name the window length, hop, transform length, and time origin
The analysis window chooses how many observed samples contribute to one transform. The hop chooses how far consecutive frames advance, while the FFT length can include zero padding beyond the observed window. For real audio, an even transform length commonly yields half that length plus one one-sided complex bins. Count frames under an explicit boundary rule: full frames only, centered frames with padding, and a flushed partial final frame produce different totals. The SciPy 1.16.0 ShortTimeFFT API documents centered columns and its own border conventions; the simpler left-aligned formula here is deliberately stated separately.
Window duration and hop duration are different latencies. A left-aligned frame cannot be fully available until its last required sample arrives; a centered feature referenced to its midpoint needs future samples relative to that timestamp. Later encoder lookahead adds another requirement. Keep feature time, capture time, availability time, and recognition emission time separate. Merely shifting reported timestamps does not turn a future-dependent feature into a causal one, and inserting zeros at a chunk edge is not the same as observing the missing future waveform.
Frequency-grid density does not equal independent resolution
The bin spacing is sample rate divided by FFT length. Increasing the FFT length with zeros samples the same finite-window spectrum more densely; it does not extend the observation or create the ability to separate arbitrarily close tones. Window shape controls leakage and main-lobe width, trading different spectral properties. A short window can localize rapid changes while providing less frequency discrimination. Choose it for the task and training recipe rather than maximizing the number of bins and interpreting that larger matrix as strictly more measured information.
Raw FFT magnitudes scale with window gain and transform convention. For an isolated coherent real sinusoid at an interior bin, a suitable amplitude estimate doubles the positive-frequency magnitude and divides by window sum, provided the negative-frequency image does not contaminate that bin. DC and Nyquist bins use different doubling rules. Power and power spectral density require other normalizations, involving squared gain or window energy and frequency units. Log features also need a declared floor and reference; a magnitude in arbitrary units is not automatically calibrated sound pressure or loudness.
Verify feature equality before investigating the recognizer
Test impulses, silence, bin-centered and off-bin tones, integer and floating representations, and signals split at awkward chunk sizes. Compare the actual feature tensors, not only final transcriptions. Check window samples, pre-emphasis if used, log base, floors, mel mapping, normalization statistics, and channel mixing under pinned versions. A model can tolerate some differences while failing badly on others; tolerance is an empirical property, not permission to omit preprocessing details. Preserve the mapping from feature frames back to source sample indices so later latency and alignment measurements remain interpretable.
The spectral lab demonstrates a finite harmonic expansion rather than an audio STFT. Reset, explicitly set BOTH mode count N=4 and decay p=1, predict four modeled modes, then Play or Step. Reset and re-set BOTH N=8 and p=1; predict eight. Edits retain t. The analytic reconstruction adds harmonics under its fixed rule; it does not window speech, perform an FFT, or model zero padding. Adding actual modeled harmonics here changes the signal representation, whereas increasing a real FFT's zero-padding length merely samples one windowed spectrum more densely.
Work it through
Compute dimensions and calibrate a coherent tone
A left-aligned full-frame-only analysis takes one second of 16000-Hz audio with N=400, H=160, and K=512. Separately use a rectangular 400-sample window on a coherent 1000-Hz cosine whose raw FFT bin magnitude is one hundred; assume no image leakage at that bin.
The window is 400/16000=25 milliseconds and hop is ten milliseconds. Full-frame count is floor((16000−400)/160)+1=98; this deliberately excludes padded boundary frames.
The real one-sided transform has 512/2+1=257 bins with spacing 16000/512=31.25 Hz. That grid spacing is not the window's independent frequency resolving power.
The rectangular window sum is 400. The tone amplitude estimate is 2×100/400=0.5 in input amplitude units, using the stated coherent interior-bin assumptions rather than a PSD convention.
Feature shape, timing, and magnitude interpretation follow explicit conventions that must match the recognition pipeline.
The living notebook
Explore the model
Interactive lab · spectral
More modeled harmonics differ from more FFT grid points
For EACH run Reset, set BOTH N and p, predict, then Play or Step. Compare N=4,p=1 with N=8,p=1: four versus eight terms in the fixed harmonic model. Edits retain t. This analytic expansion is not an STFT, FFT, speech feature extractor, zero-padding demonstration, or amplitude calibration tool.
- Sum at x = π/2
- 0.867
- Smallest wavelength
- 2π / 5
- Highest-mode coefficient
- 0.2
A finite sum of sin(nx − t)/nᵖ. Playback changes t; increasing the cutoff includes finer spatial scales. This is not a proof about a continuum limit or renormalization. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forDistinguish added signal components from denser sampling of an unchanged finite-window spectrum.
Pause & reason
Check your understanding
Your turn
Try it yourself
For 8000-Hz real audio, use a full-frame-only window N=200, hop H=80, and even FFT length K=256. Calculate window duration, hop duration, one-sided bins, and bin spacing. What extra choice is needed for a frame count?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The signal's total sample count and boundary rule determine how many frames fit.
Compare with the solution
Window duration is 25 milliseconds, hop ten milliseconds, one-sided bins 129, and bin spacing 8000/256=31.25 Hz. Counting frames additionally needs the total signal length and the specified rule for padding or incomplete final frames; these dimensions alone do not determine that count.
Keep the idea
Take it with you
- Window, hop, FFT length, boundary handling, and time origin jointly define feature tensors.
- Zero padding changes grid density rather than measured duration or automatic resolving power.
- Amplitude, power, PSD, and log features have distinct normalization contracts.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.