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.
samplesraw audio, e.g. 16bit audio data but as f32. You should apply a window function (like Hann) on the data first. The final frequency resolution issample_rate / (N / 2)e.g.44100/(16384/2) == 5.383Hz, i.e. more samples => better accuracy/frequency resolution. The amount of samples must be a power of 2. If you don’t have enough data, provide zeroes.sampling_rateThe used sampling_rate, e.g.44100 [Hz].frequency_limitTheFrequencyLimit.scaling_fnSeeSpectrumScalingFunctionfor 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()isn’t a power of two less than or equal to16384andmicrofftis used