The Fast Fourier Transform (FFT) is a mathematical operation that changes the domain (x-axis) of a signal from time to frequency. This is particularly useful for determining the frequency of a signal or for decomposing a signal consisting of multiple frequencies. I had this on my to-do list for quite some time and I finally managed to add this functionality to my Arduino Audio Tools library.
Length and Stride
The length defines the number of samples that are used to run the FFT. The length must be a value of the power of 2 (e.g. 1024). This parameter impacts
- the calculation speed and frequency of calculation
- the memory which is needed
- the frequency resolution of the result
The stride defines the number of samples that we use to advance at each step. If we do not define any stride we just consume the samples sequentially:
If we define a stride, we move ahead in steps as follows
As you can see in the image above the last length-stride samples are reprocessed with each step.
In our framework we can use the AudioRealFFT class as copy destination.
You start the processing by calling the begin method which is expecting configuration information where we define the length and stride. We also need to provide the regular audio parameters (channels, sample_rate, bits_per_sample). Finally we can define a callback method which will be called when we get a new result.
In order to determine the result we can use the result() method which provides the best AudioFFTResult or if we want to get the N best values we can call the resultArray() method to which we pass an array of AudioFFTResult.
The AudioFFTResult contains
- the frequency
- the magnitude (of the frequency)
- the musical note which is corresponding to the frequency (frequencyAsNote())
An example can be found on Github
Alternatively you can use the KissFFT Library which you will need to install separately.
Just replace the
#include "AudioLibs/AudioRealFFT.h" with
#include "AudioLibs/AudioKissFFT.h" and the
AudioRealFFT class with the
AudioKissFFT in your sketch.
In the default settings it uses int16 data types, but I did not notice any performance advantage on a ESP32.
Joe R · 18. December 2022 at 21:47
Have you done, or do you intend to do, a complex FFT for your Audio library?
pschatzmann · 18. December 2022 at 22:01
There is a nice integration and you can choose from 5 different implementation: I suggest that you have a look at the latest official documentation: https://github.com/pschatzmann/arduino-audio-tools/wiki/FFT