DSPRelated.com

STFT

Category: Transforms | Also known as: Short-Time Fourier Transform

The Short-Time Fourier Transform (STFT) is a technique that applies the Discrete Fourier Transform (DFT) repeatedly to short, overlapping segments of a signal, producing a time-varying frequency representation sometimes called a spectrogram. It reveals how the spectral content of a signal changes over time, at the cost of a time-frequency resolution trade-off governed by the chosen window length.

In practice

In embedded work, the STFT appears most often in audio and vibration analysis applications: keyword detection, motor fault detection, ultrasonic sensing, and speech feature extraction for edge-ML pipelines. The core computation is a series of windowed FFTs, so any platform with an FFT routine (CMSIS-DSP on Cortex-M, for example) can implement one. Memory is the primary constraint: you need to buffer at least one full window of samples and store the complex output bins for each frame.

Window choice and hop size are the two main design knobs. A longer window (e.g., 1024 points at 16 kHz) gives finer frequency resolution but coarser time resolution and higher per-frame compute cost. A shorter window (e.g., 64 or 128 points) suits fast transients but smears closely spaced frequencies together. Overlap between consecutive windows (commonly 50-75%) smooths the output and is required by synthesis applications (overlap-add), but it multiplies the number of FFT calls proportionally.

A common pitfall on resource-constrained targets is underestimating the throughput requirement. At 50% overlap with a 256-point window and a 16 kHz sample rate, you need roughly 125 FFTs per second. On an M4F running CMSIS-DSP, a 256-point complex FFT takes on the order of a few hundred microseconds, which is manageable, but chaining windowing, FFT, and magnitude computation inside an interrupt or a tight RTOS task requires careful profiling. The EmbeddedRelated post "A Fast Guaranteed-Stable Sliding DFT Algorithm" covers a recursive DFT approach that can reduce per-sample cost compared to recomputing a full FFT each hop.

For RF and communications work, the STFT (or its spectrogram output) provides a visual and algorithmic tool for inspecting modulated signals across time, as explored in the post "RF in Slow Motion: Sonifying a Wi-Fi 7 Packet." Spectral leakage from discontinuities at window edges is a persistent issue; applying a Hann, Hamming, or Blackman-Harris window before each FFT call is standard practice and largely eliminates the worst leakage artifacts.

Frequently asked

How do I choose the right window length for my application?
The window length sets the time-frequency resolution trade-off: a longer window resolves closely spaced frequencies better but cannot track rapid spectral changes, while a shorter window tracks transients but blurs frequency detail. A useful starting point is to ask what the shortest event you need to detect is, and what the closest frequency spacing you need to resolve is -- these two requirements will pull in opposite directions and force a practical compromise. For audio at 16 kHz, windows of 256-512 samples (16-32 ms) are common in speech applications.
What is the difference between an STFT and a plain FFT?
A plain FFT assumes the input signal is stationary over the entire analysis block and produces a single spectrum. The STFT applies a windowed FFT repeatedly to short, successive segments of the signal, yielding a sequence of spectra that show how frequency content evolves over time. The STFT output is therefore two-dimensional: time (frame index) by frequency (bin index).
Can I implement an STFT on a low-end microcontroller without an FPU?
Yes, with fixed-point arithmetic. Libraries such as CMSIS-DSP provide fixed-point FFT variants (Q15, Q31) suited to Cortex-M0/M0+ and other integer-only cores. You trade dynamic range and ease of implementation for hardware compatibility. On 8-bit or 16-bit MCUs (PIC, AVR, MSP430), real-time STFT is feasible only for short windows and low sample rates; offline or block-buffered processing is more practical in those cases.
What is a spectrogram, and how does it relate to the STFT?
A spectrogram is typically the magnitude (or magnitude-squared, i.e., power) of the STFT output, arranged as a 2-D array with time on one axis and frequency on the other. It discards phase information. Spectrograms are the standard input format for many edge-ML audio models (MobileNet-based keyword spotters, for example) because they are compact and capture perceptually relevant features.
Is there a more compute-efficient alternative to the STFT for embedded targets?
The sliding DFT (SDFT) updates a single DFT bin incrementally as each new sample arrives, avoiding the full FFT recompute on every hop. For applications that need only a few bins or require sample-by-sample output, it can be significantly cheaper. The EmbeddedRelated post 'A Fast Guaranteed-Stable Sliding DFT Algorithm' describes a numerically stable variant. For full spectrograms or when many bins are needed, the FFT-based STFT is usually more efficient overall.

Differentiators vs similar concepts

The STFT is sometimes conflated with the wavelet transform. Both provide time-frequency representations, but the STFT uses a fixed window length, giving uniform time and frequency resolution across all frequencies. Wavelet transforms use scaled basis functions, yielding finer time resolution at high frequencies and finer frequency resolution at low frequencies -- a property called multiresolution analysis. The STFT is also distinct from a plain FFT: the FFT produces a single static spectrum for the entire input block, while the STFT tracks how that spectrum changes over time by applying the FFT to a sliding window.