pub struct SpectrogramPlan<FreqScale, AmpScale>{ /* private fields */ }Expand description
A spectrogram plan is the compiled, reusable execution object.
It owns:
- FFT plan (reusable)
- window samples
- mapping (identity / mel filterbank / etc.)
- amplitude scaling config
- workspace buffers to avoid allocations in hot loops
It computes one specific spectrogram type: Spectrogram<FreqScale, AmpScale>.
§Type Parameters
FreqScale: Frequency scale type (e.g.LinearHz,LogHz,Mel, etc.)AmpScale: Amplitude scaling type (e.g.Power,Magnitude,Decibels, etc.)
Implementations§
Source§impl<FreqScale, AmpScale> SpectrogramPlan<FreqScale, AmpScale>
impl<FreqScale, AmpScale> SpectrogramPlan<FreqScale, AmpScale>
Sourcepub const fn params(&self) -> &SpectrogramParams
pub const fn params(&self) -> &SpectrogramParams
Get the spectrogram parameters used to create this plan.
§Returns
A reference to the SpectrogramParams used in this plan.
Sourcepub const fn freq_axis(&self) -> &FrequencyAxis<FreqScale>
pub const fn freq_axis(&self) -> &FrequencyAxis<FreqScale>
Get the frequency axis for this spectrogram plan.
§Returns
A reference to the FrequencyAxis<FreqScale> used in this plan.
Sourcepub fn compute(
&mut self,
samples: &NonEmptySlice<f64>,
) -> SpectrogramResult<Spectrogram<FreqScale, AmpScale>>
pub fn compute( &mut self, samples: &NonEmptySlice<f64>, ) -> SpectrogramResult<Spectrogram<FreqScale, AmpScale>>
Compute a spectrogram for a mono signal.
This function performs:
- framing + windowing
- FFT per frame
- magnitude/power
- frequency mapping (identity/mel/etc.)
- amplitude scaling (linear or dB)
It allocates the output Array2 once, but does not allocate per-frame.
§Arguments
samples- Audio samples
§Returns
A Spectrogram<FreqScale, AmpScale> containing the computed spectrogram.
§Errors
Returns an error if STFT computation or mapping fails.
Sourcepub fn compute_frame(
&mut self,
samples: &NonEmptySlice<f64>,
frame_idx: usize,
) -> SpectrogramResult<NonEmptyVec<f64>>
pub fn compute_frame( &mut self, samples: &NonEmptySlice<f64>, frame_idx: usize, ) -> SpectrogramResult<NonEmptyVec<f64>>
Compute a single frame of the spectrogram.
This is useful for streaming/online processing where you want to process audio frame-by-frame without computing the entire spectrogram.
§Arguments
samples- Audio samples (must contain at least enough samples for the requested frame)frame_idx- Frame index to compute
§Returns
A vector of frequency bin values for the requested frame.
§Errors
Returns an error if the frame index is out of bounds or if STFT computation fails.
§Examples
use spectrograms::*;
use non_empty_slice::non_empty_vec;
let samples = non_empty_vec![0.0; nzu!(16000)];
let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
let params = SpectrogramParams::new(stft, 16000.0)?;
let planner = SpectrogramPlanner::new();
let mut plan = planner.linear_plan::<Power>(¶ms, None)?;
// Compute just the first frame
let frame = plan.compute_frame(&samples, 0)?;
assert_eq!(frame.len(), nzu!(257)); // n_fft/2 + 1Sourcepub fn compute_into(
&mut self,
samples: &NonEmptySlice<f64>,
output: &mut Array2<f64>,
) -> SpectrogramResult<()>
pub fn compute_into( &mut self, samples: &NonEmptySlice<f64>, output: &mut Array2<f64>, ) -> SpectrogramResult<()>
Compute spectrogram into a pre-allocated buffer.
This avoids allocating the output matrix, which is useful when you want to reuse buffers or have strict memory requirements.
§Arguments
samples- Audio samplesoutput- Pre-allocated output matrix (must be correct size:n_binsxn_frames)
§Returns
An empty result on success.
§Errors
Returns an error if the output buffer dimensions don’t match the expected size.
§Examples
use spectrograms::*;
use ndarray::Array2;
use non_empty_slice::non_empty_vec;
let samples = non_empty_vec![0.0; nzu!(16000)];
let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
let params = SpectrogramParams::new(stft, 16000.0)?;
let planner = SpectrogramPlanner::new();
let mut plan = planner.linear_plan::<Power>(¶ms, None)?;
// Pre-allocate output buffer
let mut output = Array2::<f64>::zeros((257, 63));
plan.compute_into(&samples, &mut output)?;Sourcepub fn output_shape(
&self,
signal_length: NonZeroUsize,
) -> SpectrogramResult<(NonZeroUsize, NonZeroUsize)>
pub fn output_shape( &self, signal_length: NonZeroUsize, ) -> SpectrogramResult<(NonZeroUsize, NonZeroUsize)>
Get the expected output dimensions for a given signal length.
§Arguments
signal_length- Length of the input signal in samples.
§Returns
A tuple (n_bins, n_frames) representing the output spectrogram shape.
§Errors
Returns an error if the signal length is too short to produce any frames.
§Examples
use spectrograms::*;
let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
let params = SpectrogramParams::new(stft, 16000.0)?;
let planner = SpectrogramPlanner::new();
let plan = planner.linear_plan::<Power>(¶ms, None)?;
let (n_bins, n_frames) = plan.output_shape(nzu!(16000))?;
assert_eq!(n_bins, nzu!(257));
assert_eq!(n_frames, nzu!(63));