Skip to main content

FftPlanner

Struct FftPlanner 

Source
pub struct FftPlanner { /* private fields */ }
Expand description

A reusable FFT planner for efficient repeated FFT operations.

This planner caches FFT plans internally, making repeated FFT operations of the same size much more efficient than calling fft() repeatedly.

§Examples

use spectrograms::*;
use non_empty_slice::non_empty_vec;

let mut planner = FftPlanner::new();

// Process multiple signals of the same size efficiently
for _ in 0..100 {
    let signal = non_empty_vec![0.0; nzu!(512)];
    let spectrum = planner.fft(&signal, nzu!(512))?;
    // ... process spectrum ...
}

Implementations§

Source§

impl FftPlanner

Source

pub fn new() -> Self

Create a new FFT planner with empty cache.

Source

pub fn fft( &mut self, samples: &NonEmptySlice<f64>, n_fft: NonZeroUsize, ) -> SpectrogramResult<Array1<Complex<f64>>>

Compute forward FFT, reusing cached plans.

This is more efficient than calling the standalone fft() function repeatedly for the same FFT size.

§Automatic Zero-Padding

If the input signal is shorter than n_fft, it will be automatically zero-padded to the required length.

§Errors

Returns InvalidInput error if the input length exceeds n_fft.

§Examples
use spectrograms::*;
use non_empty_slice::non_empty_vec;

let mut planner = FftPlanner::new();

let signal = non_empty_vec![1.0; nzu!(512)];
let spectrum = planner.fft(&signal, nzu!(512))?;

assert_eq!(spectrum.len(), 257); // 512/2 + 1
Source

pub fn rfft( &mut self, samples: &NonEmptySlice<f64>, n_fft: NonZeroUsize, ) -> SpectrogramResult<Array1<f64>>

Compute forward real FFT magnitude

§Errors

Returns an error if:

  • n_fft doesn’t match the samples length
Source

pub fn irfft( &mut self, spectrum: &NonEmptySlice<Complex<f64>>, n_fft: NonZeroUsize, ) -> SpectrogramResult<NonEmptyVec<f64>>

Compute inverse FFT, reusing cached plans.

This is more efficient than calling the standalone irfft() function repeatedly for the same FFT size.

§Errors

Returns an error if:

  • The calculated expected length of spectrum doesn’t match its actual length
§Examples
use spectrograms::*;
use non_empty_slice::{non_empty_vec, NonEmptySlice};

let mut planner = FftPlanner::new();

// Forward FFT
let signal = non_empty_vec![1.0; nzu!(512)];
let spectrum = planner.fft(&signal, nzu!(512))?;

// Inverse FFT
let spectrum_slice = NonEmptySlice::new(spectrum.as_slice().unwrap()).unwrap();
let reconstructed = planner.irfft(spectrum_slice, nzu!(512))?;

assert_eq!(reconstructed.len(), nzu!(512));
Source

pub fn power_spectrum( &mut self, samples: &NonEmptySlice<f64>, n_fft: NonZeroUsize, window: Option<WindowType>, ) -> SpectrogramResult<NonEmptyVec<f64>>

Compute power spectrum with optional windowing, reusing cached plans.

§Automatic Zero-Padding

If the input signal is shorter than n_fft, it will be automatically zero-padded to the required length.

§Errors

Returns InvalidInput error if the input length exceeds n_fft.

§Examples
use spectrograms::*;
use non_empty_slice::non_empty_vec;

let mut planner = FftPlanner::new();

let signal = non_empty_vec![1.0; nzu!(512)];
let power = planner.power_spectrum(&signal, nzu!(512), Some(WindowType::Hanning))?;

assert_eq!(power.len(), nzu!(257));
Source

pub fn magnitude_spectrum( &mut self, samples: &NonEmptySlice<f64>, n_fft: NonZeroUsize, window: Option<WindowType>, ) -> SpectrogramResult<NonEmptyVec<f64>>

Compute magnitude spectrum with optional windowing, reusing cached plans.

§Automatic Zero-Padding

If the input signal is shorter than n_fft, it will be automatically zero-padded to the required length.

§Errors

Returns InvalidInput error if the input length exceeds n_fft.

§Examples
use spectrograms::*;
use non_empty_slice::non_empty_vec;

let mut planner = FftPlanner::new();

let signal = non_empty_vec![1.0; nzu!(512)];
let magnitude = planner.magnitude_spectrum(&signal, nzu!(512), Some(WindowType::Hanning))?;

assert_eq!(magnitude.len(), nzu!(257));

Trait Implementations§

Source§

impl Default for FftPlanner

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.
Source§

impl<T> Ungil for T
where T: Send,