Skip to main content

spectral_peaks

Function spectral_peaks 

Source
pub fn spectral_peaks(
    mag: &[f32],
    sample_rate: f32,
    max_peaks: usize,
    out_freq: &mut [f32],
    out_mag: &mut [f32],
) -> Result<usize, AudioError>
Expand description

Extract the strongest spectral peaks from a magnitude spectrum mag.

mag is a one-sided magnitude spectrum covering DC..Nyquist inclusive, so a spectrum of N bins was produced by an FFT of size 2*(N-1) and the bin spacing is sample_rate / (2*(N-1)) Hz. Each interior local maximum is parabolically interpolated to a sub-bin frequency. The up-to-max_peaks strongest peaks are written magnitude-descending (strongest first): out_freq[k] the interpolated frequency in Hz, out_mag[k] the interpolated peak magnitude.

Returns the number of peaks written.

Zero-heap: the output arrays hold an insertion-sorted top-K; no allocation.

ยงErrors