Expand description
An easy to use and fast no_std library (with alloc) to get the frequency
spectrum of a digital signal (e.g. audio) using FFT.
§Getting started
If you are unsure what to pick, start here. The
samples_fft_to_spectrum() function is the entry into the library. The
following configuration works for most cases: take a block of samples, apply
a Hann window, and divide the result by the number of samples.
use spectrum_analyzer::scaling::divide_by_N;
use spectrum_analyzer::windows::hann_window;
use spectrum_analyzer::{FrequencyLimit, samples_fft_to_spectrum};
// your samples; the length must be a power of two
let samples = vec![0.0; 2048];
let windowed = hann_window(&samples);
let spectrum = samples_fft_to_spectrum(
&windowed,
44100,
FrequencyLimit::All,
Some(÷_by_N),
)
.unwrap();
// the loudest frequency in the block
let (frequency, value) = spectrum.max();§How many samples?
More samples mean a finer frequency resolution (sample_rate / N), but
they also cover a longer time span, so the spectrum reacts more slowly to
changes. At 44100 Hz, 2048 samples (~46 ms, ~22 Hz per bin) are a good
starting point, 4096 if you need to tell close frequencies apart.
§What next?
windows: which window function to applyscaling: which scaling to applysamples_fft_to_spectrum: what the resulting values meanFrequencySpectrum: what you can read from the result, e.g.FrequencySpectrum::maxfor the loudest frequency,FrequencySpectrum::freq_val_closestfor one specific frequency, orFrequencySpectrum::datato iterate over all of them
§Examples
§Scaling via dynamic closure
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
// get data from audio source, ideally in range `-1.0..=1.0`
let samples = vec![0.0, 1.1, 5.5, -5.5];
let res = samples_fft_to_spectrum(
&samples,
44100,
FrequencyLimit::All,
// Create your scaling function as closure on the fly as needed.
Some(&|val, info| val - info.min),
);§Scaling via static function
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
use spectrum_analyzer::scaling::divide_by_N;
// get data from audio source, ideally in range `-1.0..=1.0`
let samples = vec![0.0, 1.1, 5.5, -5.5];
let res = samples_fft_to_spectrum(
&samples,
44100,
FrequencyLimit::All,
// Use one of the provided scaling functions. Here, we make the
// values independent of the number of samples.
Some(÷_by_N),
);Modules§
- error
- Errors related to the spectrum analysis via FFT. Most probably, the errors will result in wrong input data, before the actual calculation has begun.
- scaling
- This module contains convenient public transform functions that you can use
as parameters in
samples_fft_to_spectrumfor scaling the frequency value (the FFT result). - windows
- Several window functions which you can apply before doing the FFT. For more information:
Structs§
- Finite
F32 - Wrapper around
f32that guarantees a finite number, i.e., neitherNaNnor infinite. This makes the number orderable and sortable. - Frequency
Spectrum - Convenient wrapper around the processed FFT result.
- NonNeg
F32 - Wrapper around
FiniteF32that additionally guarantees a number that is not negative, so0.0or higher.
Enums§
- Frequency
Limit - Can be used to specify a desired frequency limit.
- Frequency
Limit Error - Possible errors when creating a
FrequencyLimit-object.
Functions§
- samples_
fft_ to_ spectrum - Takes an array of samples (length must be a power of 2, such as 2048), applies an FFT, and returns all frequencies with their magnitude.
Type Aliases§
- Frequency
- A frequency in Hertz, which is never negative.
- Frequency
Value - The value of a
Frequencyin a frequency spectrum.