Skip to main content

spectrum

Function spectrum 

Source
pub fn spectrum(
    samples: &[f32],
    sample_rate: u32,
    fft_size: usize,
) -> Result<Spectrum>
Expand description

One-sided tone-amplitude magnitudes of the first fft_size samples.

Applies the symmetric Hann 0.5 - 0.5*cos(2*pi*i/(fft_size-1)), then divides complex magnitudes by the entire window sum and doubles interior bins. DC and Nyquist are included without doubling. Bin spacing is sample_rate / fft_size Hz. These are linear input-sample amplitude units, not calibrated band RMS, power or PSD; squared-bin shares are descriptive.

Extra input is truncated; short input is right-zero-padded without changing the window or normalization. Its samples occupy the window’s original positions. A sole sample at index zero therefore produces zero magnitudes, even if nonzero. FFT size two has an all-zero Hann and returns NaN magnitudes (zero divided by zero), preserving the existing result. Do not interpret an absent windowed signal as proof of silence in the original PCM.

sample_rate must be nonzero; fft_size must be a power of two in 2..=1048576. Every input sample must be finite, including truncated samples. Invalid arguments return crate::ErrorCode::InvalidArgument. Work is O(samples.len() + fft_size * log2(fft_size)), with O(fft_size) storage. Platform FFT/window math is report-only, not cross-host bit identity.