A simple and fast no_std
library to get the frequency spectrum of a digital signal (e.g. audio) using FFT.
It follows the KISS principle and consists of simple building blocks/optional features. In short, this is
a convenient wrapper around several FFT implementations which you can choose from during compilation time
via Cargo features.
I'm not an expert on digital signal processing. Code contributions are highly welcome! 🙂
The MSRV (minimum supported Rust version) is 1.51 Stable because this crate needs the
"resolver" feature of Cargo to cope with build problems occurring in no_std
-builds.
Please see file /EDUCATIONAL.md.
no_std
-environments)Most tips and comments are located inside the code, so please check out the repository on Github! Anyway, the most basic usage looks like this:
By default this crate uses the real
-module from the great microfft
-crate. It's the fastest implementation
and as of version v0.5.0
there should be no valid reason why you should ever change this. The multiple features
are there mainly for educational reasons and to support me while programming/testing.
```toml
no_std
-builds!no_std
build problems caused by wrong feature resolution of Cargoresolver = "2"
spectrum-analyzer = "
```rust use spectrumanalyzer::{samplesffttospectrum, FrequencyLimit}; use spectrumanalyzer::windows::hannwindow;
fn main() {
// This lib also works in no_std
environments!
let samples: &[f32] = getsamples(); // TODO you need to implement the samples source
// apply hann window for smoothing; length must be a power of 2 for the FFT
let hannwindow = hannwindow(&samples[0..4096]);
// calc spectrum
let spectrumhannwindow = samplesffttospectrum(
// (windowed) samples
&hann_window,
// sampling rate
44100,
// optional frequency limit: e.g. only interested in frequencies 50 <= f <= 150?
FrequencyLimit::All,
// optional per element scaling function, e.g. 20 * log10(x)
; see doc comments
None,
// optional total scaling at the end; see doc comments
None,
);
for (fr, fr_val) in spectrum_hann_window.data().iter() {
println!("{}Hz => {}", fr, fr_val)
}
} ```
As already mentioned, there are lots of comments in the code. Short story is:
Type ComplexSpectrumScalingFunction
can do anything like BasicSpectrumScalingFunction
whereas BasicSpectrumScalingFunction
is easier to write, especially for Rust beginners.
Measurements taken on i7-8650U @ 3 Ghz (Single-Core) with optimized build
| Operation | Time |
| ------------------------------------------------------ | ------:|
| Hann Window with 4096 samples | ≈70µs |
| Hamming Window with 4096 samples | ≈10µs |
| Hann Window with 16384 samples | ≈175µs |
| Hamming Window with 16384 samples | ≈44µs |
| FFT (rustfft/complex
) to spectrum with 4096 samples | ≈240µs |
| FFT (rustfft/complex
) to spectrum with 16384 samples | ≈740µs |
| FFT (microfft/real
) to spectrum with 4096 samples | ≈120µs |
In the following example you can see a basic visualization of frequencies 0 to 4000Hz
for
a layered signal of sine waves of 50
, 1000
, and 3777Hz
@ 44100Hz
sampling rate. The peaks for the
given frequencies are clearly visible. Each calculation was done with 2048
samples, i.e. ≈46ms.
The noise (wrong peaks) also comes from clipping of the added sine waves!
Peaks (50, 1000, 3777 Hz) are clearly visible but also some noise.
Peaks (50, 1000, 3777 Hz) are clearly visible and Hann window reduces noise a little bit. Because this example has few noise, you don't see much difference.
Peaks (50, 1000, 3777 Hz) are clearly visible and Hamming window reduces noise a little bit. Because this example has few noise, you don't see much difference.
I tested f64 but the additional accuracy doesn't pay out the ~40% calculation overhead (on x86_64).
Apply a window function, like Hann window or Hamming window. But I'm not an expert on this.
Also check out my blog post! https://phip1611.de/2021/03/programmierung-und-skripte/frequency-spectrum-analysis-with-fft-in-rust/