# STM32F4 FFT example

As you maybe know, STM32F4 is Cortex M4 with DSP instructions. This allows you to make a FFT with a few simple steps. For that purpose, I have made an example, on how to create FFT with STM32F4.

I recommend use my FFT library for future use. It is built on ARM DSP library with everything included for beginner.

When the ARM company issued Cortex-M4 core, it also published DSP libraries for mathematics and other stuff. And there are also FFT functions. When you’ve downloaded ST’s standard peripheral drivers, you also downloaded CMSIS (Cortex Microcontroller Software Interface Standard), which are designed for all Cortex-M4 processors from every company.

Note: Tutorial below for Keil DSP does not work anymore with my project. For that purpose I’ve update my project and include all DSP libraries inside. All other libraries are also included in project.

CMSIS libraries are also included in Keil uVision (5 and newest), you just need to enable them. Under “Manager Run-Time Environment” -> CMSIS selectÂ DSP. DSP or Digital Signal Processing is a library for “high mathematics” instructions, which are supported by Cortex-M4 with floating point unit.

Enable DSP library in Keil uVision

## Fast Fourier Transform – FFT

Very fast about FFT. FFT orÂ FastÂ FourierÂ Transform is an algorithm to convert time based signal intoÂ frequency domain. In other words, you are able to know from which sinus components is some signal created.

Everything about FFT is described on Wikipedia.

Let’s explain things that we will need here. Example on the bottom is a simple FFT audio equlizer. It will show frequencies in your audio that you will connect to pin. Sound is sampled with 44.1kHz. We will also sampled our signal with about (~) 44.1kHz. To get proper frequency from signal, we need at least 2 samples from one period of highest frequency we want to detect. For our purpose, if we sample with 44.1kHz, then largest frequency you can sample correct is 22050Hz.

One parameter in FFT result is resolution, how good you can detect different frequencies. This depends on how many samples you take before you calculate FFT. In example below, I will take 256 samples for FFT calculating, but only 128 samples will be valid to display them. In our case, we have ~45450Hz sampling frequency and if we take 256 samples, we get resolution of 45450 / 256 = 177.5 Hz. What does it say to us?

We will get back an array, basically 256 length, but resultsÂ from 0 – 127 are valid, results from 128 – 255 are the same results as first one, but in reverse order.

• You always have to take number of samples which are power of 2!
• I took 28 = 256 samples
• Maximal value of 1 FFT result depends on number of samples
• If you have only DC value on the input, then everything you will get is Output[0]
• If you create 256 samples, then it’s value will be 256
• If you create 1024 samples, then it’s value will be 1024

I will make a table on how to interpret results from FFT output. We have resolution of ~177.5Hz, so:

FFT sample Frequency Description
Output[0] 0Hz First parameter is always DC voltage in signal
Output[1] 177.5Hz Amplitude of 177.5Hz frequency in signal
Output[n] n * 177.5Hz n-th value of your frequency result
Output[N – 1] = Output[127] 127 * Resolution = 127 * ~177.5 = ~22542Hz Maximal frequency you can analyze is always for one resolution less than half of sampling frequency -> 45450 / 2 – 177.5 = ~22547

## Example

Example below works on STM32F429-Discovery board. This board has LCD on it, so it can be also a little bit graphical.

• Provides you a FFT functionality for Cortex-M4
• Displayed on LCD as graphical equalizer
• Samples with 45450Hz (every 22us) one sample with ADC
• Pin for ADC is PA0
• On pin PA5 is an output sinus signal of 10kHz.
• You can connect it to PA0 pin and you will get response on display
• If you do that, you will see that main bar is almost on the middle of LCD, because 10000Hz / 22750 = almost 0.5
• If you want connect sound to the board, then you have to connect it in a way like image below

Connect sound to STM32F4

• If nothing is connected to PA0 pin, then you will get Output(0) with maximum value and one bar will be displayed
• ADC is not driven with DMA, it’s with “delay” mode. It’s good to show you principle on FFT.
• In production, you should use at least DMA, maybe double buffered DMA

If you have any problems with compiling project, I’ve added compiled “FFT.hex” file to project.