Close

FFT in real-time in STM32 MCUs?

yerkebulanYerkebulan wrote 03/16/2024 at 16:40 • 3 min read • Like

I 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.

STM32 ATM MATH Installation
            STM32 ARM MATH Installation

STM32 ARM MATH Installation

STM32 ARM MATH Installation

Finally, the installation of the library is over and we can include it into our project by writing this line:

#includearm_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:

  1. STM32 Course
  2. ARM MATH library for trigonometric equations: Attitude Estimation Course
  3. Video tutorials: ARM Math Installation and FFT implementation
Like

Discussions