pub fn spectrogram_normalized<T>(
x: &[T],
fs: Option<f64>,
nperseg: Option<usize>,
noverlap: Option<usize>,
db_range: Option<f64>,
) -> FFTResult<(Vec<f64>, Vec<f64>, Array2<f64>)>
Expand description
Compute a normalized spectrogram suitable for display as a heatmap.
This is a convenience function that computes a spectrogram and normalizes its values to a range suitable for visualization. It also applies a logarithmic scaling to better visualize the dynamic range of the signal.
§Arguments
x
- Input signal arrayfs
- Sampling frequency of the signal (default: 1.0)nperseg
- Length of each segment (default: 256)noverlap
- Number of points to overlap between segments (default:nperseg // 2
)db_range
- Dynamic range in dB for normalization (default: 80.0)
§Returns
- A tuple containing:
- Frequencies vector (f)
- Time vector (t)
- Normalized spectrogram result matrix (Sxx_norm) with values in [0, 1]
§Errors
Returns an error if the computation fails or if parameters are invalid.
§Examples
use scirs2_fft::spectrogram_normalized;
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 normalized spectrogram
let (f, t, sxx_norm) = spectrogram_normalized(
&chirp,
Some(fs),
Some(128),
Some(64),
Some(80.0),
).unwrap();
// Values should be normalized to [0, 1]
for row in sxx_norm.axis_iter(scirs2_core::ndarray::Axis(0)) {
for &val in row {
assert!((0.0..=1.0).contains(&val));
}
}