What Is the Short-Time Fourier Transform (STFT)? Reading Frequency Changes Over Time
Learn how STFT divides audio into short frames to calculate their frequency components, and how frame and hop lengths affect time and frequency detail.
Short answer
The short-time Fourier transform (STFT) divides audio into short frames, applies a window to each frame, and computes a Fourier transform to show how frequencies change over time. Its output is a complex matrix indexed by time frames and frequency bins: magnitude describes component strength, while phase carries phase information. SciPy ShortTimeFFT librosa STFT
How it works
A window moves across the signal in steps. At each position, the transform analyzes the samples inside the window. The step between adjacent frames is the hop length; a shorter hop creates more closely spaced time frames. Applying a window reduces discontinuities at frame edges and spectral leakage.
At 16 kHz, a 400-sample frame spans 25 ms and a 160-sample hop advances 10 ms. These are illustrative speech-analysis values, not universal settings.
The window and hop trade-off
A longer window can distinguish nearby frequencies better, but can blur the timing of brief events.
A shorter window can locate rapid events more precisely, but may separate nearby frequencies less well.
A shorter hop produces more time frames and computation; by itself it does not improve frequency resolution.
This time-frequency trade-off comes from the duration of signal analyzed. Zero-padding a shorter window to a larger FFT can make frequency-bin spacing finer, but it adds no signal information and does not automatically improve true frequency separation. librosa STFT documentation
Python example
Requirements: Python, librosa, NumPy. Input y is mono audio and sr is its actual sample rate in Hz. The example analyzes 16 kHz audio with a 25 ms frame and 10 ms hop.
import numpy as np
import librosa
n_fft = 400
hop_length = 160
D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length, win_length=n_fft)
magnitude = np.abs(D)
decibels = librosa.amplitude_to_db(magnitude, ref=np.max)D is a complex STFT matrix. magnitude omits phase; decibels are relative to the maximum. To label a plot correctly, convert frame and bin indices to seconds and Hz using the actual sr, FFT size, and hop. This is an analysis example, not a model input specification.
Read and check the result
Record frame and hop lengths in samples and time; time equals samples divided by the actual sample rate.
Time spacing follows the hop; frequency-bin spacing depends on sample rate and FFT size. Padding and frame-centering options can change first-frame timestamps and edge values.
A magnitude spectrogram alone does not preserve phase or the full original waveform. Reconstruction also depends on phase and compatible window/hop conditions.
When comparing settings, hold the input and display scale constant and record changes to window and hop lengths.
Speech consonants and low musical frequencies may require different time and frequency detail. Choose settings for the signal and task, then check axis units.