The DFT: one spectrum for the whole signal
The Discrete Fourier Transform takes N samples of a signal and rewrites them as a sum of N sinusoids at evenly spaced frequencies, each with its own amplitude and phase. It answers which frequencies are present, on average, across the whole recording:
X[k] = Σ_{n=0}^{N-1} x[n] · e^(−2πi·kn/N), k = 0 … N−1
magnitude spectrum: |X[k]| → "how much energy at frequency k·(fs/N)"
computed via the FFT in O(N log N) instead of O(N²)
What a single spectrum hides
A DFT magnitude spectrum has no time axis at all — it is a snapshot of the entire signal collapsed into frequency. Feed it a chirp that sweeps from 200 Hz to 2000 Hz over five seconds and the spectrum shows energy smeared across that whole range, indistinguishable from a signal that contained all of those frequencies simultaneously the entire time. Any signal whose frequency content changes over time — speech, music, a chirp, an AM-modulated carrier — loses exactly the information you usually care about: when each frequency happened.
STFT: chop the signal into overlapping windows
The Short-Time Fourier Transform fixes this with a simple idea: slide a short window (a few hundred samples, tapered at the edges with a Hann or Hamming function to avoid sharp cutoffs) along the signal, take a DFT of each windowed chunk separately, and stack the resulting spectra side by side. The result is a spectrogram — a 2D image with time on one axis, frequency on the other, and colour or brightness showing magnitude — which is exactly how you read a chirp's frequency rising over time, or see the vertical bands of energy that mark consonants in speech.
for each window position t:
segment = x[t : t+W] * hann(W) # taper to reduce spectral leakage
spectrogram[:, t] = |FFT(segment)|
window length W trades time resolution for frequency resolution
The uncertainty trade-off you cannot avoid
A short window pins down when something happened precisely but smears out which frequency it was (few samples means coarse frequency bins); a long window does the opposite. This is not an implementation flaw — it is a direct consequence of the time-frequency uncertainty principle, the same mathematics behind the Heisenberg uncertainty relation in quantum mechanics, applied to signals instead of particles. There is no single window length that is simultaneously optimal for both a sharp instant (like a drum hit) and a pure tone (like a sustained note); every spectrogram is a compromise chosen for the signal at hand.
Reading the modes on this simulator
A pure sine shows one horizontal band; a chord shows several stacked horizontal bands, one per note; a chirp shows a rising or falling diagonal streak, the DFT's smeared blur resolved cleanly into a trajectory; AM modulation shows a strong carrier line flanked by two symmetric sidebands spaced at the modulation frequency, exactly as amplitude-modulation theory predicts; and broadband noise fills the whole frequency axis with no structure at any single time, because by definition it has no dominant frequency to track.
Frequently asked questions
Why does a plain DFT fail to show a chirp's rising pitch?
The DFT collapses the entire signal duration into one spectrum with no time axis, so a frequency that sweeps upward over time looks identical to many frequencies all present at once for the whole recording. Only slicing the signal in time, as the STFT does, can recover when each frequency occurred.
Why can't the STFT have both perfect time and frequency resolution?
Because resolution in each domain depends on window length in opposite ways: a short window localises time precisely but only samples a few cycles, blurring frequency; a long window resolves frequency finely but averages over a long time span. This time-frequency trade-off is a mathematical limit, not a bug to fix by choosing a better algorithm.
What do the sidebands next to the carrier in AM mode represent?
Amplitude-modulating a carrier at frequency fc with a tone at frequency fm mathematically produces three components: the carrier fc itself plus two sidebands at fc − fm and fc + fm. The STFT display shows all three as separate horizontal lines because it is genuinely a three-frequency signal, not an illustration artifact.
Try it live
Everything above runs in your browser — open DFT & STFT Visualiser and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open DFT & STFT Visualiser simulation