Function spectrum_analyzer::samples_fft_to_spectrum[][src]

pub fn samples_fft_to_spectrum(
    samples: &[f32],
    sampling_rate: u32,
    frequency_limit: FrequencyLimit,
    per_element_scaling_fn: Option<SimpleSpectrumScalingFunction<'_>>,
    total_scaling_fn: Option<ComplexSpectrumScalingFunction>
) -> FrequencySpectrum

Takes an array of samples (length must be a power of 2), e.g. 2048, applies an FFT (using the specified FFT implementation) on it and returns all frequencies with their volume/magnitude.

By default, no normalization/scaling is done at all and the results, i.e. the frequency magnitudes/amplitudes/values are the raw result from the FFT algorithm, except that complex numbers are transformed to their magnitude.

  • samples raw audio, e.g. 16bit audio data but as f32. You should apply an window function (like Hann) on the data first. The final frequency resolution is sample_rate / (N / 2) e.g. 44100/(16384/2) == 5.383Hz, i.e. more samples => better accuracy/frequency resolution.
  • sampling_rate sampling_rate, e.g. 44100 [Hz]
  • frequency_limit Frequency limit. See [`FrequencyLimit´]
  • per_element_scaling_fn See crate::SimpleSpectrumScalingFunction for details. This is easier to write, especially for Rust beginners. Everything that can be achieved with this, can also be achieved with parameter total_scaling_fn. See crate::scaling for example implementations.
  • total_scaling_fn See [crate::spectrum::SpectrumTotalScaleFunctionFactory] for details. See crate::scaling for example implementations.

Returns value

New object of type FrequencySpectrum.

Panics

  • When samples contains NaN or infinite values (regarding f32/float).
  • When samples.len() isn’t a power of two and samples.len() > 4096 (restriction by microfft-crate)