STM32 Fast Fourier Transform.
For a long time I wanted to add a Fourier transform to the oscilloscope I described here, but for some reason I never got around to it. Quite recently, a month ago, I decided to increase the scope's sampling rate by replacing the stm32F103VE with a 1M sampling rate with an stm32F303VC with a 5M sampling rate. After replacing the chip and changing the program, I finally decided to figure out the fast Fourier transform (FFT).
You could try writing the FFT yourself, but that would take a lot of time, so I decided to use the CMSIS-DSP library, though I still had to brush up on the theory.
We have an ADC that gives us conversion results at a certain rate. We accumulate these results in a buffer before feeding them to the FFT input. The buffer size must be a power of two and contain 128 (2 to the 7th), 256 (2 to the 8th) samples, and so on.
The resolution depends on the number of points used in the FFT.
To find the step between harmonics, we simply divide the sampling rate by the number of points used in the FFT. If at a sampling rate of 512KHz the FFT uses 256 points, we get harmonic amplitudes with a step of 2KHz. If there are 512 points, we get harmonic amplitudes with a step of 1KHz.
And one more example to consolidate.
For example, with a sampling rate of 1MHz and 1024 points, spectral bin number 512 will correspond to a frequency of 500KHz.

As written above, we feed a real signal to the FFT input, i.e. the data received from the ADC, and at the output we get complex numbers, twice as many of them. That's it for the theory; let's move on to the actual implementation of the FFT on STM32.
First, we need to include the library and enable FPU support.

Now we can start writing the program. The code below is for the STM32F303.
Note that the input and output data formats differ, as shown in the table.

This must be taken into account when plotting the graph.
Here is the final result.


I'd like to remind you that the FFT result of a real signal is symmetric about zero frequency, and only half of the spectral bins (0 to 128) are shown on the display.
You could try writing the FFT yourself, but that would take a lot of time, so I decided to use the CMSIS-DSP library, though I still had to brush up on the theory.
We have an ADC that gives us conversion results at a certain rate. We accumulate these results in a buffer before feeding them to the FFT input. The buffer size must be a power of two and contain 128 (2 to the 7th), 256 (2 to the 8th) samples, and so on.
The resolution depends on the number of points used in the FFT.
To find the step between harmonics, we simply divide the sampling rate by the number of points used in the FFT. If at a sampling rate of 512KHz the FFT uses 256 points, we get harmonic amplitudes with a step of 2KHz. If there are 512 points, we get harmonic amplitudes with a step of 1KHz.
And one more example to consolidate.
For example, with a sampling rate of 1MHz and 1024 points, spectral bin number 512 will correspond to a frequency of 500KHz.

As written above, we feed a real signal to the FFT input, i.e. the data received from the ADC, and at the output we get complex numbers, twice as many of them. That's it for the theory; let's move on to the actual implementation of the FFT on STM32.
First, we need to include the library and enable FPU support.

Now we can start writing the program. The code below is for the STM32F303.
#define ARM_MATH_CM4// specify which core the MCU is based on
#include <arm_math.h> //include the library
#define FFT_SIZE 256//set the FFT size
//Create two buffers: one will store the ADC samples, the other the FFT results
q15_t fft_Sbuff[FFT_SIZE] = {0};
q15_t fft_Dbuff[FFT_SIZE*2] = {0};
uint16_t k;
uint8_t status;
arm_rfft_instance_q15 S;
status = arm_rfft_init_q15(&S, FFT_SIZE, 0, 1);//initialization function required for the FFT
if(status == ARM_MATH_SUCCESS)
{
arm_rfft_q15(&S,fft_Sbuff, fft_Dbuff);//perform the FFT
arm_cmplx_mag_q15(fft_Dbuff, fft_Sbuff, FFT_SIZE);//calculate the harmonic amplitudes
}
Note that the input and output data formats differ, as shown in the table.

This must be taken into account when plotting the graph.
Here is the final result.


I'd like to remind you that the FFT result of a real signal is symmetric about zero frequency, and only half of the spectral bins (0 to 128) are shown on the display.
