ruststft
A complete short-time Fourier transform toolkit for Rust: forward and inverse STFT, a rich window library, batch and streaming APIs, and optional mel spectrograms / MFCCs.
- Forward STFT over real signals, streaming or batch, backed by
realfft(≈2× faster and half the memory of a full complex FFT on real input). - Inverse STFT with weighted overlap-add (WOLA) for perfect reconstruction.
- Windows: rectangular, Hann, Hamming, Blackman, Blackman-Harris, Nuttall, flat-top, Bartlett, triangular, Welch, cosine, Tukey, Kaiser, Gaussian - periodic (spectral-analysis) or symmetric (filter-design).
- Spectrum helpers: magnitude, power, phase, and decibel conversions.
- Mel & MFCC (
melfeature): librosa-compatible filterbank, mel scales and an orthonormal DCT-II. #![forbid(unsafe_code)]- 100% safe Rust.no_std(withalloc) for the window, spectrum and mel math.
Install
[]
= "0.4"
Minimum supported Rust version: 1.85.
Batch spectrogram
use ;
let fs = 8_000.0;
let signal: =
.map
.collect;
let mut stft = builder
.window
.hop_size
.center
.build
.unwrap;
let spec = stft.spectrogram;
assert_eq!; // includes the Nyquist bin
Perfect reconstruction (STFT → ISTFT)
use ;
let signal: = .map.collect;
let mut stft = builder
.window
.hop_size // 75% overlap: Hann is COLA-compliant
.center
.build
.unwrap;
let spec = stft.spectrogram;
let recon = stft.inverse.unwrap.reconstruct.unwrap;
// recon matches `signal` in the interior to ~machine precision.
Streaming
use ;
let mut stft = builder
.window
.hop_size
.build
.unwrap;
let mut column = vec!;
let chunk: = .map.collect;
stft.append;
while stft.ready
Examples
Runnable programs live in examples/:
Feature flags
| Feature | Default | Description |
|---|---|---|
std |
yes | FFT-backed processors (Stft, Istft, batch). Required for the transforms. |
mel |
no | Mel filterbank, mel scales, and DCT-II for MFCCs. |
ndarray |
no | Spectrogram::to_array2 ([n_freqs, n_frames]). |
rayon |
no | Parallel per-frame batch spectrograms. |
serde |
no | (De)serialize configuration and window descriptions. |
wasm_simd |
no | WASM simd128 FFT kernels (implies std; build with -C target-feature=+simd128). |
Without the default std feature the crate builds as no_std (with alloc),
exposing the window library, the spectrum
helpers and the mel math. The
FFT processors require std because the FFT backend does.
Migrating from 0.3
0.4 is a breaking redesign. Rough mapping:
| 0.3 | 0.4 |
|---|---|
STFT::new(WindowType::Hanning, w, s) |
Stft::builder().window(Window::hann(w)).hop_size(s).build()? |
WindowType::Hanning (etc.) |
Window::hann(len) / WindowFunction::Hann |
stft.append_samples(x) |
stft.append(x) |
stft.contains_enough_to_compute() |
stft.ready() |
stft.compute_complex_column(&mut c) |
stft.process_into(&mut c)? |
stft.move_to_next_column() |
stft.step() |
stft.output_size() (= fft/2) |
stft.n_freqs() (= fft/2 + 1, fixes Nyquist) |
compute_magnitude_column / compute_column |
spectrum::magnitude / power_to_db on a column |
| (no inverse) | stft.inverse()?.reconstruct(&spec)? |
See CHANGELOG.md for details.
Contributing
Licensed under either of Apache-2.0 or MIT at your option.