pub fn stft<T>(
x: &[T],
window: Window,
nperseg: usize,
noverlap: Option<usize>,
nfft: Option<usize>,
fs: Option<f64>,
detrend: Option<bool>,
boundary: Option<&str>,
) -> FFTResult<(Vec<f64>, Vec<f64>, Array2<Complex64>)>
Expand description
Performs a Short-Time Fourier Transform (STFT).
Short-Time Fourier Transform (STFT) is used to determine the sinusoidal frequency and phase content of local sections of a signal as it changes over time.
§Arguments
x
- Input signalwindow
- Window function to applynperseg
- Length of each segmentnoverlap
- Number of points to overlap between segmentsnfft
- Length of the FFT (optional, default is nperseg)fs
- Sampling frequency of the signaldetrend
- Whether to remove the mean from each segmentboundary
- Boundary to pad with (‘zeros’, ‘constant’, ‘reflect’, etc.)
§Returns
- Tuple of (frequencies, times, Zxx) where Zxx is the STFT result
§Errors
Returns an error if the computation fails.
§Examples
use scirs2_fft::{stft, window::Window};
use std::f64::consts::PI;
// Generate a simple sine wave
let fs = 1000.0; // 1 kHz sampling rate
let t = (0..1000).map(|i| i as f64 / fs).collect::<Vec<_>>();
let signal = t.iter().map(|&ti| (2.0 * PI * 100.0 * ti).sin()).collect::<Vec<_>>();
// Compute STFT
let (frequencies, times, result) = stft(
&signal,
Window::Hann,
256,
Some(128),
None,
Some(fs),
None,
None,
).unwrap();
// Check dimensions
assert_eq!(frequencies.len(), result.shape()[0]);
assert_eq!(times.len(), result.shape()[1]);