spectrogram

Function spectrogram 

Source
pub fn spectrogram<T>(
    x: &[T],
    fs: Option<f64>,
    window: Option<Window>,
    nperseg: Option<usize>,
    noverlap: Option<usize>,
    nfft: Option<usize>,
    detrend: Option<bool>,
    scaling: Option<&str>,
    mode: Option<&str>,
) -> FFTResult<(Vec<f64>, Vec<f64>, Array2<f64>)>
where T: NumCast + Copy + Debug,
Expand description

Compute a spectrogram of a time-domain signal.

A spectrogram is a visual representation of the frequency spectrum of a signal as it varies with time. It is often displayed as a heatmap where the x-axis represents time, the y-axis represents frequency, and the color intensity represents signal power.

§Arguments

  • x - Input signal array
  • fs - Sampling frequency of the signal (default: 1.0)
  • window - Window specification (function or array of length nperseg) (default: Hann)
  • nperseg - Length of each segment (default: 256)
  • noverlap - Number of points to overlap between segments (default: nperseg // 2)
  • nfft - Length of the FFT used (default: nperseg)
  • detrend - Whether to remove the mean from each segment (default: true)
  • scaling - Power spectrum scaling mode: “density” or “spectrum” (default: “density”)
  • mode - Power spectrum mode: “psd”, “magnitude”, “angle”, “phase” (default: “psd”)

§Returns

  • A tuple containing:
    • Frequencies vector (f)
    • Time vector (t)
    • Spectrogram result matrix (Sxx) 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;
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 + 50.0 * ti) * ti).sin()).collect::<Vec<_>>();

// Compute spectrogram
let (f, t, sxx) = spectrogram(
    &chirp,
    Some(fs),
    Some(Window::Hann),
    Some(128),
    Some(64),
    None,
    Some(true),
    Some("density"),
    Some("psd"),
).unwrap();

// Check dimensions
assert_eq!(f.len(), sxx.shape()[0]);
assert_eq!(t.len(), sxx.shape()[1]);