-
FFT in real-time in STM32 MCUs?
03/16/2024 at 16:40 • 0 commentsI remember old days when the microcontrollers had modest capabilities, handling only easy tasks. Nowadays, however, the MCUs are becoming more and more powerful reaching a level of handling complex tasks in real-time. This modification opens up new possibilities in embedded programming, and in this article, we delve into one such capability – running Fast Fourier Transform (FFT) on STM32 MCUs, leveraging the ARM MATH Library.
ARM Math library is a collection of functions designed to handle various mathematical operations, from linear algebra to signal processing and trigonometric functions. Despite its extensive range of functions, this article focuses on integrating the library into a project and implementing FFT.
STM32 Hardware and ARM Math Library Installation
For demonstration purposes, I've used the STM32F4 Discovery Board, with the project set up in STM32CubeIDE and peripheral configurations handled using CubeMX.
- Once you create a project, navigate to ‘Select Components’
- Then, in the new window search for ‘DSP’ and you will find the DSP library as shown in the Figure below. Do not forget to select it by ticking the corresponding box. THEN PRESS OK.
- Once you do it, go to ‘Software Packs’ and within ALGOBUILD’, integrate DSP Library by ticking the box. After, you can configure other peripherals you need and save the file to generate the code.
- There are a few more steps to finally integrate the ARM MATH library. The next step is to open the properties of the project and navigate to C/C++ general -> Paths and Symbols ->Symbols. Then, add the symbol “ARM_MATH_CM4” as shown in the figure below. However, if you are using a microcontroller with a different cortex, for example, Cortex M7, you need to add the corresponding symbol (ARM_MATH_CM7). Then, you can press ‘APPLY and Close. ’
Finally, the installation of the library is over and we can include it into our project by writing this line:
#include “arm_math.h”
STM32 FFT Implementation
Now, let's dive into the steps of implementing FFT using the ARM MATH Library.
Define an FFT instance using the data type:
arm_rfft_fast_instance_f32 fft_instance;
Initialize the instance by specifying the length:
arm_rfft_fast_init_f32(&fft_instance, FFT_LENGTH);
Apply the FFT using the following function:
arm_rfft_fast_f32(&fft_instance, input_fft, output_fft, 0);
So the arguments of this function are the instance pointer, input array, and output array. The last argument (0) signifies that it is not an inverse FFT. Once, you apply this function, the output function holds the complex numbers including the phase information (even positions are real terms while odd positions are imaginary terms). However, in most of the cases, we want to obtain the absolute values of the frequencies. Fortunately, there is another function to obtain it:
arm_cmplx_mag_f32(output_fft, output_fft_mag, FFT_LENGTH/2);
Once you apply this function, output_fft_mag holds the absolute values.
Personally, I tested this function in real time when recording audio data at 32 kHz. And it worked seamlessly, having any glitches.
This article scratches the surface of the ARM MATH Library's capabilities, focusing on FFT implementation. Future articles will explore additional features of this powerful library.
Additional resources:
- STM32 Course
- ARM MATH library for trigonometric equations: Attitude Estimation Course
- Video tutorials: ARM Math Installation and FFT implementation