04. STFT and spectrograms
Speech and music change over time: a vowel lasts tens of milliseconds, a plosive burst a few. One DFT of a whole utterance (chapter 03) combines all of these into one spectrum. The short-time Fourier transform (STFT) takes a DFT of short, overlapping, windowed frames instead, giving frequency content as a function of time: the spectrogram. This chapter defines exactly how the crate frames the signal, why fine time and fine frequency resolution cannot be had at once, how the transform is inverted, and what the dB scale of a spectrogram means.
Concept
Definition (as implemented). With frame length (and FFT size) , hop and window :
where is the frame index, the bin index,
the padding in samples and outside the signal. With
centre = true, , so frame is centred on sample
; with centre = false, and frame starts at sample . The
window is the symmetric Hann of chapter 03.
Number of frames for a signal of samples:
Resolution. Bins are Hz apart and frames seconds apart, with the sample rate in Hz. The actual time resolution, however, is set by the window length , and the actual frequency resolution by the window’s main lobe (about for Hann at dB). Their product is fixed by the window shape: halving one doubles the other. A smaller hop gives more frames, not sharper ones.
Inverse (weighted overlap-add). Inverse-DFT each frame to , multiply by the window again and overlap-add, dividing by the summed squared windows:
This is exact wherever the denominator is non-zero, for any window and hop, provided the STFT has not been modified. Two overlap conditions apply. NOLA (nonzero overlap-add): the summed squared windows are never zero, which is the only condition the inverse above requires. COLA (constant overlap-add): the plain summed windows are the same constant for every , which plain overlap-add without the division needs. WOLA works even for windows that are not COLA. If the STFT is modified before inversion (masking, a phase vocoder), the frames are no longer consistent and the same formula gives the least-squares estimate of Griffin and Lim (1984) rather than an exact inverse. With centring, the output has samples.
Spectrogram scales. Power (no division by ), magnitude , and decibels
in dB re 1 (an absolute reference), where floor is the LogParams value in
dB. LogParams::new(-80.0) clamps at power ; it is not a floor
relative to the peak (80 dB below it), and a loud bin can be far above 0 dB. A
log-frequency spectrogram resamples the bins onto log-spaced frequencies; a
mel spectrogram sums power through triangular filters on the Slaney mel scale
(chapter 10).
With the library
use audio_samples::{AudioSamples, AudioTransforms, nzu, sample_rate, white_noise};
use spectrograms::{StftParams, WindowType};
use std::time::Duration;
let x = white_noise::<f64>(Duration::from_secs(1), sample_rate!(16000), 0.5, Some(7));
let l = x.samples_per_channel().get(); // 16 000
let frames = |n: usize, h: usize, centre: bool| {
let p = if centre { n / 2 } else { 0 };
1 + (l + 2 * p - n) / h
};
let params = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, true).unwrap();
let st = x.stft(¶ms).unwrap();
assert_eq!(st.data.nrows(), 257); // N/2 + 1 bins
assert_eq!(st.data.ncols(), frames(512, 128, true));
assert_eq!(st.data.ncols(), 126);
let no_centre = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, false).unwrap();
assert_eq!(x.stft(&no_centre).unwrap().data.ncols(), 122);
// Inverse: 125 * 128 + 512 - 512 = 16 000 samples, equal to the input to round-off.
let y = AudioSamples::<f64>::istft(st).unwrap();
assert_eq!(y.samples_per_channel().get(), 16_000);
let err = x.as_slice().unwrap().iter().zip(y.as_slice().unwrap())
.map(|(a, b)| (a - b).abs()).fold(0.0, f64::max);
assert!(err < 1e-12); // measured: about 3e-16
Measuring the trade-off: two tones 50 Hz apart, and a click, analysed with a 16 ms and a 128 ms window (hop 2 ms in both):
use audio_samples::utils::generation::impulse;
use audio_samples::{AudioSamples, AudioTransforms, nzu, sample_rate, sine_wave};
use spectrograms::{StftParams, WindowType};
use std::num::NonZeroUsize;
use std::time::Duration;
let half_s = Duration::from_millis(500);
let a = sine_wave::<f64>(1000.0, half_s, sample_rate!(16000), 0.5);
let b = sine_wave::<f64>(1050.0, half_s, sample_rate!(16000), 0.5);
let tones: Vec<f64> = a.as_slice().unwrap().iter().zip(b.as_slice().unwrap()).map(|(p, q)| p + q).collect();
let tones = AudioSamples::new_mono(ndarray::Array1::from(tones), sample_rate!(16000)).unwrap();
let click = impulse::<f64>(half_s, sample_rate!(16000), 1.0, 0.25);
let analyse = |n: usize| -> (usize, f64) {
let p = StftParams::new(NonZeroUsize::new(n).unwrap(), nzu!(32), WindowType::Hanning, true).unwrap();
// Frequency: summed magnitude per bin; count peaks between 800 and 1250 Hz.
let s = tones.stft(&p).unwrap();
let avg: Vec<f64> = (0..s.data.nrows()).map(|k| s.data.row(k).iter().map(|c| c.norm()).sum()).collect();
let bin = 16_000.0 / n as f64;
let (lo, hi) = ((800.0 / bin) as usize, (1250.0 / bin) as usize);
let top = avg[lo..hi].iter().copied().fold(0.0, f64::max);
let peaks = (lo..hi).filter(|&i| avg[i] > avg[i - 1] && avg[i] >= avg[i + 1] && avg[i] > 0.5 * top).count();
// Time: frames where the click's energy exceeds half its maximum, in ms.
let c = click.stft(&p).unwrap();
let e: Vec<f64> = (0..c.data.ncols()).map(|j| c.data.column(j).iter().map(|v| v.norm_sqr()).sum()).collect();
let emax = e.iter().copied().fold(0.0, f64::max);
let spread_ms = e.iter().filter(|&&v| v > 0.5 * emax).count() as f64 * 2.0;
(peaks, spread_ms)
};
let (peaks_short, spread_short) = analyse(256); // 16 ms window, Δf = 62.5 Hz
let (peaks_long, spread_long) = analyse(2048); // 128 ms window, Δf = 7.8 Hz
assert_eq!((peaks_short, peaks_long), (1, 2)); // only the long window resolves 50 Hz
assert_eq!((spread_short, spread_long), (6.0, 46.0)); // ... and spreads the click 8x wider
Spectrogram types and the absolute dB floor. A full-scale 1 kHz sine is bin-centred for at 16 kHz (bin 32), so its peak power is and its dB value is dB re 1: above 0 dB.
use audio_samples::utils::generation::silence;
use audio_samples::{AudioTransforms, nzu, sample_rate, sine_wave};
use spectrograms::{LogHzParams, LogParams, MelParams, SpectrogramParams, StftParams, WindowType};
use std::time::Duration;
let sine = sine_wave::<f64>(1000.0, Duration::from_millis(500), sample_rate!(16000), 1.0);
let stft = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, true).unwrap();
let sp = SpectrogramParams::new(stft, 16_000.0).unwrap();
let floor = LogParams::new(-80.0).unwrap();
let lin = sine.linear_db_spectrogram(&sp, &floor).unwrap();
let loghz = sine.loghz_power_spectrogram(&sp, &LogHzParams::new(nzu!(64), 80.0, 8000.0).unwrap()).unwrap();
let mel = sine.mel_power_spectrogram(&sp, &MelParams::new(nzu!(40), 0.0, 8000.0).unwrap()).unwrap();
assert_eq!((lin.data().nrows(), loghz.data().nrows(), mel.data().nrows()), (257, 64, 40));
let peak_db = lin.data().iter().copied().fold(f64::MIN, f64::max);
assert!((peak_db - 42.13).abs() < 0.01);
// Silence is exactly at the floor: -80 dB re 1, whatever else is in the file.
let quiet = silence::<f64>(Duration::from_millis(250), sample_rate!(16000));
let q = quiet.linear_db_spectrogram(&sp, &floor).unwrap();
assert!(q.data().iter().all(|&v| (v + 80.0).abs() < 1e-9));
// The mel band with the most energy is the one centred nearest 1 kHz (1031 Hz).
// (frequencies() is correct here because the range is 0 Hz to Nyquist.)
let f = mel.frequencies();
let best = (0..40).max_by(|&i, &j| mel.data().row(i).sum().total_cmp(&mel.data().row(j).sum())).unwrap();
assert!((f[best] - 1031.0).abs() < 1.0);
How it works
Rebuild one frame from the definition (pad, slice, window, DFT), check the
power spectrogram is , and measure the overlap-add sums that istft
relies on:
use audio_samples::educational::Explain;
use audio_samples::{AudioTransforms, nzu, sample_rate, white_noise};
use num_complex::Complex;
use spectrograms::{SpectrogramParams, StftParams, WindowType};
use std::f64::consts::PI;
use std::time::Duration;
let x = white_noise::<f64>(Duration::from_millis(250), sample_rate!(16000), 0.5, Some(3));
let xs = x.as_slice().unwrap();
let (n, h, m) = (512usize, 128usize, 10usize);
let params = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, true).unwrap();
let st = x.stft(¶ms).unwrap();
// Symmetric Hann, denominator N - 1.
let w: Vec<f64> = (0..n).map(|i| 0.5 - 0.5 * (2.0 * PI * i as f64 / (n - 1) as f64).cos()).collect();
// Frame m covers padded samples mH .. mH + N, i.e. original samples mH - N/2 ...
let frame: Vec<f64> = (0..n).map(|i| {
let idx = (m * h + i) as isize - (n / 2) as isize;
if idx >= 0 && (idx as usize) < xs.len() { xs[idx as usize] * w[i] } else { 0.0 }
}).collect();
for k in [0, 1, 37, 128, 256] {
let xk: Complex<f64> = frame.iter().enumerate()
.map(|(i, &v)| Complex::from_polar(v, -2.0 * PI * ((k * i) % n) as f64 / n as f64)).sum();
// 512-term sums of O(0.5) values: rounding of order 1e-13.
assert!((xk - st.data[[k, m]]).norm() < 1e-10);
}
// Power spectrogram = |X|^2, element by element.
let sp = SpectrogramParams::new(params.clone(), 16_000.0).unwrap();
let pow = x.linear_power_spectrogram(&sp).unwrap();
assert!((pow.data()[[37, m]] - st.data[[37, m]].norm_sqr()).abs() < 1e-9);
// Overlap-add sums in the steady state (away from the ends).
let sum_w: Vec<f64> = (0..h).map(|r| (0..n / h).map(|q| w[r + q * h]).sum()).collect();
let sum_w2: Vec<f64> = (0..h).map(|r| (0..n / h).map(|q| w[r + q * h].powi(2)).sum()).collect();
let mean = sum_w.iter().sum::<f64>() / h as f64;
let (lo, hi) = sum_w.iter().fold((f64::MAX, f64::MIN), |(a, b), &v| (a.min(v), b.max(v)));
let ripple = (hi - lo) / mean;
// The symmetric Hann at 75 % overlap is *almost* COLA (sum ~ 2), not exactly.
assert!((mean - 2.0).abs() < 0.01 && ripple > 0.0 && ripple < 0.01);
// NOLA holds with a wide margin, so istft is exact regardless.
assert!(sum_w2.iter().copied().fold(f64::MAX, f64::min) > 1.0);
let doc = params.explain();
assert!((doc.quantity("Overlap-add ripple").unwrap().value - ripple).abs() < 1e-6);
Ask the library to explain
StftParams implements Explain. Through a
signal (ExplainFor) it receives the sample
rate and length; given the expected pitch range as well, it warns when the
window is too short for the lowest expected f0:
use audio_samples::educational::{Explain, ExplainContext, ExplainFor, WarningLevel};
use audio_samples::{nzu, sample_rate, sine_wave};
use spectrograms::{StftParams, WindowType};
use std::num::NonZeroUsize;
use std::time::Duration;
let speech_like = sine_wave::<f64>(120.0, Duration::from_secs(1), sample_rate!(16000), 0.5);
let params = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, true).unwrap();
let doc = speech_like.explain(¶ms);
assert_eq!(doc.quantity("Frequency resolution").unwrap().value, 31.25); // Hz
assert_eq!(doc.quantity("Frame period").unwrap().value, 8.0); // ms
assert_eq!(doc.quantity("Frames").unwrap().value, 126.0);
// f0 between 80 and 400 Hz: two periods of 80 Hz last 25 ms.
for (n, short, coarse) in [(1024, false, false), (256, true, false), (128, true, true)] {
let p = StftParams::new(NonZeroUsize::new(n).unwrap(), NonZeroUsize::new(n / 4).unwrap(),
WindowType::Hanning, true).unwrap();
let ctx = ExplainContext::for_audio(&speech_like).with_f0_range(80.0, 400.0);
let d = p.explain_with(&ctx);
// 16 ms (N = 256) and 8 ms (N = 128) windows are shorter than 25 ms ...
assert_eq!(d.has_warning_containing("shorter than two periods"), short);
// ... and at N = 128 the 125 Hz bins are coarser than 80 Hz.
assert_eq!(d.has_warning_containing("coarser than the lowest expected f0"), coarse);
assert_eq!(d.has_warning(WarningLevel::Caution), short || coarse);
}
println!("{doc}");
Exercises
- How many frames does
stftproduce for 2.5 s of audio at 22.05 kHz with , and centring? Predict, then check. - At 16 kHz, one frame is required every 10 ms. Choose , then the smallest power-of-two for each of two requirements: (a) a bin spacing of at most 20 Hz; (b) a Hann main lobe ( dB width about , the actual resolution) of at most 20 Hz. Confirm (a) with the explainer.
- A signal is 0.5 s of a full-scale 1 kHz sine followed by 0.5 s of silence.
With
LogParams::new(-100.0), what value do the bins of the last frame take, and how far below the spectrogram’s peak is that?
Answers
use audio_samples::educational::ExplainContext;
use audio_samples::educational::Explain;
use audio_samples::{AudioSamples, AudioTransforms, nzu, sample_rate, sine_wave};
use spectrograms::{LogParams, SpectrogramParams, StftParams, WindowType};
use std::time::Duration;
// 1. L = 55 125; 1 + floor((55 125 + 2*1024 - 2048) / 512) = 1 + 107 = 108.
let l = 55_125usize;
assert_eq!(1 + (l + 2 * 1024 - 2048) / 512, 108);
let x = sine_wave::<f64>(440.0, Duration::from_millis(2500), sample_rate!(22050), 0.5);
assert_eq!(x.samples_per_channel().get(), l);
let p = StftParams::new(nzu!(2048), nzu!(512), WindowType::Hanning, true).unwrap();
assert_eq!(x.stft(&p).unwrap().data.ncols(), 108);
// 2. H = 0.010 * 16 000 = 160.
// (a) bin spacing: N >= 16 000 / 20 = 800, next power of two 1024.
// (b) main lobe: 2 * 16 000 / N <= 20, so N >= 1600, next power of two 2048.
let h = (0.010f64 * 16_000.0) as usize;
let n = ((16_000.0f64 / 20.0).ceil() as usize).next_power_of_two();
let n_lobe = ((2.0 * 16_000.0f64 / 20.0).ceil() as usize).next_power_of_two();
assert_eq!((h, n, n_lobe), (160, 1024, 2048));
let p = StftParams::new(nzu!(1024), nzu!(160), WindowType::Hanning, true).unwrap();
let d = p.explain_with(&ExplainContext::new().with_sample_rate(16_000.0));
assert_eq!(d.quantity("Frame period").unwrap().value, 10.0);
assert_eq!(d.quantity("Frequency resolution").unwrap().value, 15.625); // bin spacing
// (b) The explainer's measured -6 dB main-lobe width: too wide at N = 1024,
// narrow enough at N = 2048 (about 2 bins in both cases).
let lobe = |n: usize| {
let p = StftParams::new(std::num::NonZeroUsize::new(n).unwrap(), nzu!(160), WindowType::Hanning, true).unwrap();
p.explain_with(&ExplainContext::new().with_sample_rate(16_000.0))
.quantity("Main-lobe width (-6 dB)").unwrap().value
};
assert!(lobe(1024) > 20.0 && lobe(2048) <= 20.0);
// 3. -100 dB re 1, which is 100 + 42.13 = 142.13 dB below the peak.
let mut v = sine_wave::<f64>(1000.0, Duration::from_millis(500), sample_rate!(16000), 1.0)
.as_slice().unwrap().to_vec();
v.extend(std::iter::repeat_n(0.0, 8000));
let a = AudioSamples::new_mono(ndarray::Array1::from(v), sample_rate!(16000)).unwrap();
let p = StftParams::new(nzu!(512), nzu!(128), WindowType::Hanning, true).unwrap();
let sp = SpectrogramParams::new(p, 16_000.0).unwrap();
let s = a.linear_db_spectrogram(&sp, &LogParams::new(-100.0).unwrap()).unwrap();
let last = s.data().ncols() - 1;
assert!(s.data().column(last).iter().all(|&db| (db + 100.0).abs() < 1e-9));
let peak = s.data().iter().copied().fold(f64::MIN, f64::max);
assert!((s.data()[[0, last]] - peak + 142.13).abs() < 0.01);
Common pitfalls
- Centring pads with zeros. Tools that pad by reflection give different first and last frames.
- The window is the symmetric Hann; many tools use the periodic one by default. Values differ slightly unless both use the same window.
- dB values are re 1 with an absolute floor; there is no
top_db. Subtract the maximum to get “dB below peak”. - Power is with no or window normalisation: a full-scale sine reads about dB at , and the number grows with .
istftcan return up to samples fewer than the input; trim or pad.- A smaller hop does not improve resolution; only (and the window shape) sets it.
References
- J. B. Allen and L. R. Rabiner, “A unified approach to short-time Fourier analysis and synthesis”, Proceedings of the IEEE 65(11), 1558-1564, 1977.
- D. W. Griffin and J. S. Lim, “Signal estimation from modified short-time Fourier transform”, IEEE Transactions on Acoustics, Speech, and Signal Processing 32(2), 236-243, 1984.
- F. J. Harris, “On the use of windows for harmonic analysis with the discrete Fourier transform”, Proceedings of the IEEE 66(1), 51-83, 1978.
- B. McFee et al., “librosa: audio and music signal analysis in Python”, Proceedings of the 14th Python in Science Conference, 2015.
See also
AudioTransforms::stft,AudioTransforms::istft,AudioTransforms::linear_db_spectrogram,AudioTransforms::loghz_power_spectrogram,AudioTransforms::mel_power_spectrogram- The
transformsmodule docs (framing, dB and mel conventions) Explain,ExplainContext::with_f0_range,topics::window- Previous: 03. The DFT, windows and leakage. Next: 05. Convolution and FIR filters. Mel features in depth: 10. Cepstrum, mel and MFCC.
- Lab:
examples/tutorial_04_stft_spectrograms.rs(cargo run --release --example tutorial_04_stft_spectrograms --features educational,full_no_plotting).