AI Glossary / Time series and signal processing / Advanced
Spectrogram
Definition
A spectrogram represents how a signal's frequency content changes over time. Formally, it is the squared magnitude of a short-time Fourier transform: each short section of a signal contributes a column of frequency values. It is often displayed as a colored image, with time on one axis, frequency on the other and intensity indicating the chosen spectral measure.
What it means
A whole-record spectrum can show that two tones occurred without showing whether they occurred together or at different times. A spectrogram adds that time dimension. Its colors are calculated values, not a direct photograph of sound or machine activity. Reading the image requires knowing the sampling rate, window, time step, scaling and color reference used to construct it.
How it works
Select short, usually overlapping windows of samples, multiply each by a window function and calculate its Fourier transform. Square the complex magnitude to obtain nonnegative values for an ordinary spectrogram. Arrange these values by frequency and window time. Display software may then apply logarithmic scaling or combine frequencies into bands.
Window length affects the balance between localizing changes in time and separating nearby frequencies. Hop size controls spacing between columns, while overlap makes neighboring columns share observations. Padding, detrending and normalization also change the numerical representation. A power spectral density display requires appropriate normalization; an unscaled squared transform should not be labeled automatically as physical power per hertz.
Three practical examples
- A speech analyst examines how resonant bands change during an utterance. The image helps locate transitions, while the original audio remains necessary for interpretation and checking artifacts.
- A maintenance team compares vibration patterns before and after a change in operating speed. A moving frequency ridge may be relevant, but the team still needs equipment context before assigning a fault label.
- An audio classifier takes spectrogram values as inputs. Consistent preprocessing matters because changing the window or intensity scale can alter the representation even when the underlying recording is unchanged.
Think of it this way
Imagine taking a sequence of short musical snapshots, each listing how strongly different notes are represented. Laying those lists side by side reveals when the pattern changes. Each snapshot covers an interval, so a sharp event can affect several neighboring snapshots rather than appearing at one perfectly precise instant.
Worked example
Construct a 16-sample signal recorded at eight samples per second. Its first eight samples follow 2 cos(2πn/8), for local index n from zero to seven: a 1 Hz cosine of amplitude two. Its final eight samples follow cos(2π2n/8), restarting the local index: a 2 Hz cosine of amplitude one.
Use eight-sample rectangular windows, a four-sample hop, no padding and no detrending. Complete windows start at samples zero, four and eight. Label columns by their start times: zero, 0.5 and one second. Each window has a nominal duration of one second and a frequency-bin spacing of 8/8 = 1 Hz. Software may instead label window centers.
The first window contains only the constructed 1 Hz tone. With an unnormalized forward DFT, its positive-frequency magnitude at 1 Hz is eight, giving squared magnitude 64. The final window contains only the 2 Hz tone; its positive-frequency magnitude at 2 Hz is four, giving squared magnitude 16. Matching negative-frequency components are present in the full transform. We are displaying raw squared magnitudes, without one-sided doubling or physical power-density normalization.
The middle window contains four samples from each segment. Its spectrum describes that mixed interval, not an additional stable tone known to have existed at the transition. Overlapping columns share data, so three columns are not three independent measurements.
On a logarithmic display with reference value one in the same squared-magnitude units, the two pure-window peaks are 10 log10(64) = 18.06 dB and 10 log10(16) = 12.04 dB. Their difference is approximately 6.02 dB. This corresponds to a fourfold squared-magnitude ratio and a twofold amplitude ratio under the identical transform convention. It is not a calibrated sound-pressure level.
Illustrative case study
Fictional Cedar Acoustics uses the constructed signal to test a visualization. Its designer initially assigns each image a separate automatic color range. The 1 Hz and 2 Hz peaks then look equally bright in separately plotted windows, despite their different numerical intensities.
The team adds a shared reference and color scale, labels the start-time convention, and makes window settings available alongside the image. It also preserves the waveform so users can inspect the transition. This case demonstrates a display problem and its proposed correction; it does not show that a classifier can identify real events or that brightness alone measures operational importance.
Common misconceptions and mistakes
More columns do not automatically mean better temporal resolution: a smaller hop can merely create more overlapping views of the same long window. More FFT bins from zero-padding do not add observations. Strong color can reflect a plotting choice rather than a stronger physical signal. An ordinary squared-magnitude spectrogram also omits explicit phase, so it should not be treated as the full complex STFT or assumed to uniquely determine the original waveform.
Business use
Define a consistent representation before comparing recordings or training an AI model. Record sampling rate, window shape and length, hop, padding, scaling and logarithmic reference. Split evaluation data so overlapping excerpts from the same recording do not create misleadingly easy comparisons across training and test sets. Validate the downstream task separately from the visual appeal of the spectrogram; a readable image is not evidence of accurate classification.
People also ask
Is a spectrogram the same as a waveform?
No. A waveform shows sample values over time; a spectrogram summarizes localized frequency content.
Does every bright band identify a separate source?
No. Harmonics, leakage and processing choices can create multiple visible features from one source.
Is a mel spectrogram identical?
It applies frequency-band processing on a mel scale, so its numerical representation differs from a plain frequency-bin spectrogram.
Check your understanding
Why does the middle column not establish that both stable tones were present simultaneously throughout its interval?
Answer: Its window spans the switch and combines samples from both segments. The display summarizes that whole interval; it cannot make the underlying event timing sharper than the information and window permit.
Related terms
- Short-Time Fourier Transform
- Spectral Analysis
- window function
- hop size
- spectral leakage
- power spectral density
- mel spectrogram
- decibel
Learn next
Double both segments' amplitudes while leaving every setting unchanged. Each squared-magnitude peak becomes four times larger: 256 and 64. Both rise by approximately 6.02 dB relative to the same reference, while their mutual difference remains approximately 6.02 dB.
More in Time series and signal processing
- Source-filter model - The source-filter model explains speech as an excitation signal shaped by the acoustic response of the vocal tract.
- Specification error - Specification error occurs when a model's chosen structure inadequately represents the relationship or process it is intended to describe.
- Spectral decomposition - Spectral decomposition expresses a normal square matrix through an orthonormal basis of eigenvectors and their eigenvalues.
- State space - A state space is the set of possible states used to represent a system.
- State space modeling - State space modeling describes a changing system using an underlying state and equations connecting that state to observations.
- Stationarity - Stationarity is a property describing statistical stability over time.
Sources
Aalto Dictionary of Machine Learning, spectrogram: https://aaltodictionaryofml.github.io/ADictML_Main.pdf SciPy, ShortTimeFFT.spectrogram documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.ShortTimeFFT.spectrogram.html The switched-tone signal, numerical values and Cedar Acoustics scenario are original teaching constructions with explicitly stated transform and display conventions.
Previous: Spectral modeling synthesis Next: Spectrum bias