signal::spectral::frames
The short-time Fourier transform, its inverse by weighted overlap-add (Griffin and Lim 1984), and the spectrogram, with SciPy's framing.
Structs
StftSource
pub struct StftAn STFT: z[k, m] is frequency f[k] in the segment centred at
time t[m].
SpectrogramSource
pub struct SpectrogramA spectrogram: s[k, m] is the power at f[k] in the segment
centred at t[m].
Functions
stftSource
pub fn stft(x: some RealSamples, fs: f64 = 1.0, window: Window = Window::Hann, nperseg: usize = 256, noverlap: usize? = none, nfft: usize? = none) throws (SignalError) -> Stft The STFT of x (as SciPy's stft): nperseg / 2 zeros added at
each end and zeros at the end to fill the last segment, so every
sample is covered; segments of nperseg (256) overlapping by
noverlap (half), windowed by a periodic Hann window, each a real
FFT of nfft points scaled by 1 / sum(window). istft inverts
it when the window and overlap satisfy the overlap-add condition
(Hann at half overlap does).
use core::array::Array;
let x = Array::from_fn([1000], |i: [usize; 1]| -> f64 { (i[0] as f64 * 0.3).sin() });
let s = try signal::spectral::stft(x, nperseg: 64);
println(s.z.shape()); // [33, 33]
istftSource
pub fn istft(z: Matrix<Complex<f64>>, fs: f64 = 1.0, window: Window = Window::Hann, nperseg: usize? = none, noverlap: usize? = none, nfft: usize? = none) throws (SignalError) -> Vector<f64> The signal back from its STFT (as SciPy's istft): each column is
inverse-transformed, windowed and overlap-added, divided by the
summed squared window, and the nperseg / 2 boundary samples are
dropped. nperseg defaults to 2 (rows - 1); pass the nperseg,
noverlap, nfft and window given to stft. Returns the
samples; trim them to the original length.
use core::array::Array;
let x = Array::from_fn([500], |i: [usize; 1]| -> f64 { (i[0] as f64 * 0.1).cos() });
let s = try signal::spectral::stft(x, nperseg: 64);
let y = try signal::spectral::istft(s.z, nperseg: 64);
println((y[100] - x[100]).abs() < 1e-12); // true
spectrogramSource
pub fn spectrogram(x: some RealSamples, fs: f64 = 1.0, window: Window = Window::Tukey(0.25), nperseg: usize = 256, noverlap: usize? = none, nfft: usize? = none, detrend: Detrend? = Detrend::Constant, scaling: Scaling = Scaling::Density) throws (SignalError) -> Spectrogram The spectrogram of x (as SciPy's spectrogram): power spectral
density of each segment, by default nperseg 256 samples of a
Tukey(0.25) window overlapping by an eighth, each detrended. No
boundary padding: t holds the segment centres.
use core::array::Array;
let x = Array::from_fn([2048], |i: [usize; 1]| -> f64 { (i[0] as f64 * 0.5).sin() });
let s = try signal::spectral::spectrogram(x);
println(s.s.shape()); // [129, 9]