DSPRelated.com

Spectrogram

Category: Spectral

A spectrogram is a two-dimensional representation of how the frequency content of a signal evolves over time, typically displayed as a plot with time on the horizontal axis, frequency on the vertical axis, and signal power (or magnitude) encoded as color or intensity. It is computed by applying a sequence of short-time Fourier transforms (STFTs) to overlapping or adjacent time windows of the signal.

In practice

In embedded signal processing, spectrograms are most commonly generated as an intermediate or final step in audio and vibration analysis applications. On a microcontroller with sufficient memory and compute, the signal is divided into overlapping frames using a window function (Hann, Hamming, Blackman, etc.), each frame is passed through an FFT, and the resulting magnitude or power spectrum for each frame is stored or streamed. The collection of these per-frame spectra forms the spectrogram. On constrained targets such as Cortex-M4/M7 parts, fixed-point or CMSIS-DSP FFT routines are commonly used to keep per-frame latency manageable.

The choice of window length and overlap directly trades off time resolution against frequency resolution. A short window captures fast transients well but smears frequency detail; a long window resolves closely spaced tones but blurs rapid changes. This is a manifestation of the time-frequency uncertainty principle. Typical embedded audio applications (keyword spotting, fault detection in motors) use window lengths in the range of 20 to 40 ms with 50% overlap, producing spectrogram frames that feed a classifier. The blog post "The Power Spectrum" covers how per-frame power spectra are computed, which is the underlying operation for each column of a spectrogram.

A common pitfall is underestimating memory requirements. Storing a full floating-point spectrogram for even a few seconds of audio at reasonable resolution can exceed the RAM of many MCU-class targets. Common mitigations include processing frames as they arrive and feeding them directly to a downstream algorithm, quantizing the output to 8 or 16 bits, or using a Mel-scale frequency axis (log-spaced filterbank) to reduce the number of frequency bins before storage, as is standard in MFCC-based audio pipelines.

Spectral leakage affects every column of a spectrogram individually; a strong tone that is not bin-aligned will spread energy into neighboring frequency bins, which can mask weaker nearby features. Applying an appropriate window function before each FFT reduces but does not eliminate leakage. The blog post "Demonstrating the Periodic Spectrum of a Sampled Signal Using the DFT" provides useful background on how the DFT treats each frame as a periodic signal, which is the root cause of leakage when that periodicity assumption is violated.

Discussed on DSPRelated

Frequently asked

What is the difference between a spectrogram and a power spectrum?
A power spectrum describes the power distribution across frequencies for a single, fixed block of samples. A spectrogram is a sequence of power (or magnitude) spectra computed from successive short time windows, showing how that distribution changes over time. Each vertical slice of a spectrogram is essentially one power spectrum.
How do I choose window length and overlap for an embedded application?
Window length sets the frequency resolution (resolution in Hz = sample rate / window length) and the minimum time event you can detect. Overlap controls how often you produce a new output frame. A 50% overlap is a common starting point. For audio keyword spotting on MCUs, 20 to 40 ms windows with 50% overlap are typical. For vibration diagnostics at lower sample rates, longer windows are often needed to resolve closely spaced mechanical frequencies.
Can a spectrogram be computed in real time on a microcontroller?
Yes, on many modern MCUs. A Cortex-M4 or M7 running a 256-point fixed-point FFT via CMSIS-DSP can process a frame in well under 1 ms, leaving ample headroom for 50% overlap at typical audio sample rates. Lower-end 8-bit or 16-bit MCUs may struggle with the arithmetic throughput required, particularly for longer FFT sizes or high sample rates.
What window function should I use?
The Hann window is a common default: it provides good sidelobe suppression with moderate main-lobe widening. Hamming offers slightly lower first-sidelobe amplitude. Blackman gives better sidelobe rejection at the cost of a wider main lobe. Rectangular (no window) maximizes frequency resolution but produces significant leakage for non-bin-aligned tones. The best choice depends on whether your application prioritizes resolving closely spaced tones or detecting weak signals near strong ones.
What is a Mel spectrogram and why is it used in embedded audio applications?
A Mel spectrogram applies a bank of triangular filters spaced on the Mel scale (approximately logarithmic in frequency) to the magnitude spectrogram, compressing the output to a smaller number of frequency bands that better match human auditory perception. This reduces the feature vector size significantly, making downstream classifiers cheaper to run. It is the standard front end for MFCC computation and for neural network keyword spotting models deployed on MCU targets.

Differentiators vs similar concepts

A spectrogram is often confused with a plain power spectrum or FFT output. The key distinction is the time axis: a single FFT or power spectrum gives a static snapshot of frequency content, while a spectrogram stacks many such snapshots to show temporal evolution. A spectrogram is also distinct from a scalogram, which uses wavelet transforms instead of the STFT and achieves variable time-frequency resolution across scales. A Mel spectrogram is a spectrogram whose frequency axis has been warped and compressed using a Mel-scale filterbank, reducing bin count and emphasizing perceptually relevant frequency bands.