Skip to main content

SpectrogramPlan

Struct SpectrogramPlan 

Source
pub struct SpectrogramPlan<FreqScale, AmpScale>
where AmpScale: AmpScaleSpec + 'static, FreqScale: Copy + Clone + 'static,
{ /* 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>
where AmpScale: AmpScaleSpec + 'static, FreqScale: Copy + Clone + 'static,

Source

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.

Source

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.

Source

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.

Source

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>(&params, None)?;

// Compute just the first frame
let frame = plan.compute_frame(&samples, 0)?;
assert_eq!(frame.len(), nzu!(257)); // n_fft/2 + 1
Source

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 samples
  • output - Pre-allocated output matrix (must be correct size: n_bins x n_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>(&params, None)?;

// Pre-allocate output buffer
let mut output = Array2::<f64>::zeros((257, 63));
plan.compute_into(&samples, &mut output)?;
Source

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>(&params, None)?;

let (n_bins, n_frames) = plan.output_shape(nzu!(16000))?;
assert_eq!(n_bins, nzu!(257));
assert_eq!(n_frames, nzu!(63));

Auto Trait Implementations§

§

impl<FreqScale, AmpScale> Freeze for SpectrogramPlan<FreqScale, AmpScale>

§

impl<FreqScale, AmpScale> !RefUnwindSafe for SpectrogramPlan<FreqScale, AmpScale>

§

impl<FreqScale, AmpScale> !Send for SpectrogramPlan<FreqScale, AmpScale>

§

impl<FreqScale, AmpScale> !Sync for SpectrogramPlan<FreqScale, AmpScale>

§

impl<FreqScale, AmpScale> Unpin for SpectrogramPlan<FreqScale, AmpScale>
where AmpScale: Unpin, FreqScale: Unpin,

§

impl<FreqScale, AmpScale> !UnwindSafe for SpectrogramPlan<FreqScale, AmpScale>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.