pub fn samples_fft_to_spectrum(
    samples: &[f32],
    sampling_rate: u32,
    frequency_limit: FrequencyLimit,
    scaling_fn: Option<SpectrumScalingFunction<'_>>
) -> Result<FrequencySpectrum, SpectrumAnalyzerError>
Expand description

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´]
  • scaling_fn See crate::scaling::SpectrumScalingFunction for details.

Returns value

New object of type FrequencySpectrum.

Examples

Scaling via dynamic closure

use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
// get data from audio source
let samples = vec![0.0, 1.1, 5.5, -5.5];
let res = samples_fft_to_spectrum(
        &samples,
        44100,
        FrequencyLimit::All,
        Some(&|val, info| val - info.min),
 );

Scaling via static function

use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
use spectrum_analyzer::scaling::scale_to_zero_to_one;
// get data from audio source
let samples = vec![0.0, 1.1, 5.5, -5.5];
let res = samples_fft_to_spectrum(
        &samples,
        44100,
        FrequencyLimit::All,
        Some(&scale_to_zero_to_one),
 );

Panics

  • When samples.len() > 4096 and microfft is used (restriction by the crate)