pub fn stft<T>(
x: &[T],
window: Window,
nperseg: usize,
noverlap: Option<usize>,
nfft: Option<usize>,
fs: Option<f64>,
detrend: Option<bool>,
return_onesided: Option<bool>,
boundary: Option<&str>,
) -> FFTResult<(Vec<f64>, Vec<f64>, Array2<Complex64>)>
Expand description
Compute the Short-Time Fourier Transform (STFT) of a signal.
The 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 signal arraywindow
- Window specification (function or array of lengthnperseg
)nperseg
- Length of each segmentnoverlap
- Number of points to overlap between segments (default:nperseg // 2
)nfft
- Length of the FFT used (default:nperseg
)fs
- Sampling frequency of thex
time series (default: 1.0)detrend
- Whether to remove the mean from each segment (default: true)return_onesided
- If true, return half of the spectrum (real signals) (default: true)boundary
- Behavior at boundaries (default: None)
§Returns
- A tuple containing:
- Frequencies vector (f)
- Time vector (t)
- STFT result matrix (Zxx) where rows are frequencies and columns are time segments
§Errors
Returns an error if the computation fails or if parameters are invalid.
§Examples
use scirs2_fft::spectrogram::stft;
use scirs2_fft::window::Window;
use std::f64::consts::PI;
// Generate a chirp signal
let fs = 1000.0; // 1 kHz sampling rate
let time = (0..1000).map(|i| i as f64 / fs).collect::<Vec<_>>();
let chirp = time.iter().map(|&ti| (2.0 * PI * (10.0 + 10.0 * ti) * ti).sin()).collect::<Vec<_>>();
// Compute STFT
let (f, t, zxx) = stft(
&chirp,
Window::Hann,
256,
Some(128),
None,
Some(fs),
Some(true),
Some(true),
None,
).unwrap();
// Check dimensions
assert_eq!(f.len(), zxx.shape()[0]);
assert_eq!(t.len(), zxx.shape()[1]);