RustFFT | performance FFT library written in pure Rust
kandi X-RAY | RustFFT Summary
kandi X-RAY | RustFFT Summary
RustFFT is a high-performance FFT library written in pure Rust. It can compute FFTs of any size, including prime-number sizes, in O(nlogn) time. RustFFT supports the AVX instruction set for increased performance. No special code is needed to activate AVX: Simply plan a FFT using the FftPlanner on a machine that supports the avx and fma CPU features, and RustFFT will automatically switch to faster AVX-accelerated algorithms. For machines that do not have AVX, it also supports the SSE4.1 instruction set. As for AVX, this is enabled automatically when using the FftPlanner. Unlike previous major versions, RustFFT 5.0 has several breaking changes compared to RustFFT 4.0. Check out the Upgrade Guide for a walkthrough of the changes RustFFT 5.0 requires.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of RustFFT
RustFFT Key Features
RustFFT Examples and Code Snippets
Community Discussions
Trending Discussions on RustFFT
QUESTION
I wrote a low-pass filter based around the FFT algorithm.
First I process the input data using forward FFT, then I decrease the "volume" of parts of the resulting spectrum, then I put the data into the inverse FFT and finally I normalize the data like this:
...ANSWER
Answered 2022-Feb-28 at 13:30There are two issues with your code:
Since you want to process real data (i.e. data whose imaginary part is
0
), the output of the forward FFT is symmetrical and you need to apply the same coefficient to matching frequencies (i.e.spectrum[i]
andspectrum[spectrum.len() - i]
, remembering thatspectrum[0]
stands alone, and so doesspectrum[spectrum.len()/2]
if the length is even).If the frequencies are symmetrical, the result of the inverse FFT should be real (i.e. the imaginary part should be
0
± small rounding errors). Therefore yournormalize
function should usex.re
instead ofx.norm()
, which will allow it to retain its sign.
QUESTION
ANSWER
Answered 2021-Aug-08 at 20:07Your code is, in a sense, fine; what you are seeing are basic problems with interpreting the FFT, not with computing it.
First, the FFT is naturally a function from complex samples to complex samples. When you start with a real-valued input signal and convert it to complex by adding a zero imaginary component (or any other simple value, even copying the real input_buffer[i]
), the output will always be a mirrored spectrum.
(Complex-valued signals can have arbitrarily asymmetric spectra, distinguishing positive frequencies from negative ones. This is not widely useful in audio, but it is fundamental to software-defined radio (SDR) applications of FFT and other DSP operations.)
In order to not get the mirroring, you must discard one half of the output. (It's slightly more efficient — though not 50%, if I recall correctly — to skip computing that half, but it doesn't look like rustfft
offers that option.)
If you discard the upper half of the output (in terms of array indices), then you will find that the remaining “frequency bins” are arranged from 0 Hz up to 22.05 kHz. The library documentation notes this:
Output Order
Elements in the output are ordered by ascending frequency, with the first element corresponding to frequency 0.
Applications that do use the second half of the spectrum often swap the two halves, so that instead of a range of 0 Hz to [sampling frequency]/2, they go from −[sampling frequency]/2 to +[sampling frequency]/2. But since you're starting with a real, not complex, signal, this doesn't apply to you; I just mention it since you might have seen it in other plots that have 0 Hz at the center.
The drop-off which is visible in the center of your image corresponds to the high-pass anti-aliasing filter required in any digital signal processing. It should appear at the right edge once you've discarded the right half.
Finally, your code does not appear to have any windowing applied to the input signal. Windowing is a complex topic, but it is necessary to account for the fact that the FFT presumes a periodic signal which exactly repeats over the length of the input buffer, but we're actually feeding it a signal whose period is not an even division of the input buffer; windowing dampens the effects of this by attenuating the beginning and ending portions of the signal.
You should look up a standard window function and apply it to your input data before the FFT; this should reduce the secondary peak you're observing.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install RustFFT
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page