Here is an example of **F**ast **F**ourier **T**ransform on STM32F4xx devices. This HAL library works for F4 and F7 series!

That was the main reason I decided to make a library for **FFT** on STM32F4xx.

To use this library, some third-party libraries are also required. All these required files can be found in STM32F4xx Standard peripheral drivers and DSP instructions provided from ST.com from their website. Now is version 1.5.1 of these drivers and also .lib file is included for ARM MATH for Cortex-M4 Little-Endian with Floating point instructions.

Point of this library is that user don’t actually need to know how FFT works in real life. You just have to know, that calculated FFT is always half of size in length than data, which are used for calculation, because ARM DSP library uses real and imaginary part in input array, so input array is twice of size of output array.

# Library

## Features

- Calculate FFT result with float 32 type of variable
- Calculate max value of your FFT result
- Works with highly optimized ARM DSP library
- Support for variable FFT size
- Allows 16, 32, 64, 128, 256, 512, 1024, 2048 or 4096 samples for FFT size

## Dependencies

- HAL
- TM
- STM32Fxxx HAL
- defines.h
**defines.h configuration example 130072 downloads 0.00 KB**

- ARM DSP
**arm_const_structs.c**, available in “CMSIS\DSP_Lib\Source\CommonTables”**arm_cortexM4lf_math.lib**for ARM compiler, available in “CMSIS\Lib\ARM”

CMSIS folder can be found if you download these libraries directly from ARM.com or if you download any Cube package.

## ARM MATH

ARM MATH is a library provided from ARM and is the same for all Cortex families, except that you have to provide some informations to library.

In your global compiler defines, you should add these 2 lines:

1 2 3 4 5 |
/* FPU present on STM32F4xx device */ #define __FPU_PRESENT 1 /* Use ARM MATH for Cortex-M4 */ #define ARM_MATH_CM4 |

## FFT Samples count

ARM FFT library allows you to use specific number of samples for data calculation.

These values can be every number which **is power of 2** from **2^4** and **2^12**. So, 9 different FFT length options. These number are passed into function when you initialize FFT with my library.

## FFT input/output buffers

FFT works in a way that you first fill input buffer with samples and then you process them and you got samples in output buffer. Complex (CFFT) Fast Fourier Transform, which is also used behind the scenes in my library so uses real and imaginary part in input buffer and only real part is calculated to output buffer.

For this reason, **input** buffer **HAVE TO** be **2 * FFT_Size** in length and **output** buffer **HAVE TO** be **FFT_Size** in length where FFT_Size is the same as FFT_Samples count explained above.

Library is able to use malloc() to allocate memory for you. You just need to enable this feature when you initialize FFT module and everything will be done for you. You just have to make sure that you have enough HEAP memory available for malloc, otherwise malloc will fail and you will not get anything.

For example, if you have 512 length FFT size, then input buffer must be **2 * 512 = 1024** samples of float 32 and output buffer is **512** samples of float 32. In common, this is **1536** samples of float32 which is 4-bytes long in memory. So, together this would be **6144 Bytes** of HEAP memory.

## Functions and enumerations

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
*/ /** * @brief Initializes and prepares FFT structure for signal operations * @param *FFT: Pointer to empty @ref TM_FFT_F32_t structure for FFT * @param FFT_Size: Number of samples to be used for FFT calculation * This parameter can be a value of 2^n where n is between 4 and 12, so any power of 2 between 16 and 4096 * @param use_malloc: Set parameter to 1, if you want to use HEAP memory and @ref malloc to allocate input and output buffers * @note It is recommended to use malloc for allocation, because FFT input and output buffers differs in length * @retval Initialization status: * - 0: Initialized OK, ready to use * - 1: Input FFT SIZE is not valid * - 2: Malloc failed with allocating input data buffer * - 3: Malloc failed with allocating output data buffer. If input data buffer is allocated, it will be free if this is returned. */ uint8_t TM_FFT_Init_F32(TM_FFT_F32_t* FFT, uint16_t FFT_Size, uint8_t use_malloc); /** * @brief Sets input and output buffers for FFT calculations * @note Use this function only if you set @arg use_malloc parameter to zero in @ref TM_FFT_Init_F32 function * @param *FFT: Pointer to @ref TM_FFT_F32_t structure where buffers will be set * @param *InputBuffer: Pointer to buffer of type float32_t with FFT_Size * 2 length * @param *OutputBuffer: Pointer to buffer of type float32_t with FFT_Size length * @retval None */ void TM_FFT_SetBuffers_F32(TM_FFT_F32_t* FFT, float32_t* InputBuffer, float32_t* OutputBuffer); /** * @brief Adds new sample to input buffer in FFT array * @param *FFT: Pointer to @ref TM_FFT_F32_t structure where new sample will be added * @param sampleValue: A new sample to be added to buffer, real part. Imaginary part will be set to 0 * @retval FFT calculation status: * - 0: Input buffer is not full yet * - > 0: Input buffer is full and samples are ready to be calculated */ uint8_t TM_FFT_AddToBuffer(TM_FFT_F32_t* FFT, float32_t sampleValue); /** * @brief Adds new sample (real and imaginary part if needed) to input buffer in FFT array * @param *FFT: Pointer to @ref TM_FFT_F32_t structure where new sample will be added * @param Real: A new sample to be added to buffer, real part. * @param Imag: A new sample to be added to buffer, imaginary part. * @retval FFT calculation status: * - 0: Input buffer is not full yet * - > 0: Input buffer is full and samples are ready to be calculated */ uint8_t TM_FFT_AddToBufferWithImag(TM_FFT_F32_t* FFT, float32_t Real, float32_t Imag); /** * @brief Processes and calculates FFT from InputBuffer and saves data to Output buffer * @note This function also calculates max value and max index in array where max value happens * @param *FFT: Pointer to @ref TM_FFT_F32_t where FFT calculation will happen * @retval None */ void TM_FFT_Process_F32(TM_FFT_F32_t* FFT); /** * @brief Free input and output buffers * @note This function has sense only, if you used @ref malloc for memory allocation when you called @ref TM_FFT_Init_F32 function * @param *FFT: Pointer to @ref TM_FFT_F32_t structure where buffers will be free * @retval None */ void TM_FFT_Free_F32(TM_FFT_F32_t* FFT); /** * @brief Gets max value from already calculated FFT result * @param FFT: Pointer to @ref TM_FFT_F32_t structure where max value should be checked * @retval None * @note Defined as macro for faster execution */ #define TM_FFT_GetMaxValue(FFT) ((FFT)->MaxValue) /** * @brief Gets index value where max value happens from already calculated FFT result * @param FFT: Pointer to @ref TM_FFT_F32_t structure where max index at max value should be checked * @retval None * @note Defined as macro for faster execution */ #define TM_FFT_GetMaxIndex(FFT) ((FFT)->MaxIndex) /** * @brief Gets FFT size in units of samples length * @param FFT: Pointer to @ref TM_FFT_F32_t structure where FFT size will be checked * @retval FFT size in units of elements for calculation * @note Defined as macro for faster execution */ #define TM_FFT_GetFFTSize(FFT) ((FFT)->FFT_Size) /** * @brief Gets FFT result value from output buffer at given index * @param FFT: Pointer to @ref TM_FFT_F32_t structure where FFT output sample will be returned * @param index: Index in buffer where result will be returned. Valid input is between 0 and FFT_Size - 1 * @retval Value at given index * @note Defined as macro for faster execution */ #define TM_FFT_GetFromBuffer(FFT, index) ((FFT)->Output[(uint16_t)(index)]) /** * @} */ |

## Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
/** * Keil project example for FFT and LCD * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required * * Notes: * - Under "Options for target" > "C/C++" > "Define" you must add 2 defines (I've already add them): * - ARM_MATH_CM4 or ARM_MATH_CM7 * - __FPU_PRESENT=1 */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_fft.h" #include "tm_stm32_lcd.h" /* Include arm_math.h mathematic functions */ #include "arm_math.h" /* FFT settings */ #define SAMPLES (512) /* 256 real party and 256 imaginary parts */ #define FFT_SIZE (SAMPLES / 2) /* FFT size is always the same size as we have samples, so 256 in our case */ #define FFT_BAR_MAX_HEIGHT 120 /* 120 px on the LCD */ /* Global variables */ float32_t Input[SAMPLES]; /*!< Input buffer is always 2 * FFT_SIZE */ float32_t Output[FFT_SIZE]; /*!< Output buffer is always FFT_SIZE */ /*!< FFT structure */ TM_FFT_F32_t FFT; /* Temporary sinus value */ float32_t sin_val; /* Draw bar for LCD */ /* Simple library to draw bars */ void DrawBar(uint16_t bottomX, uint16_t bottomY, uint16_t maxHeight, uint16_t maxValue, float32_t value, uint16_t foreground, uint16_t background); int main(void) { uint32_t i = 0, freq = 0; /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init LCD */ TM_LCD_Init(); #if defined(STM32F429_DISCOVERY) /* Rotate LCD to landscape mode */ TM_LCD_SetOrientation(2); #endif /* Print on LCD */ TM_LCD_SetXY(10, 10); TM_LCD_Puts("FFT Library example"); TM_LCD_SetXY(10, 30); TM_LCD_Puts("with software generated"); TM_LCD_SetXY(10, 50); TM_LCD_Puts("SINE wave"); /* Print "logo" */ TM_LCD_SetFont(&TM_Font_7x10); TM_LCD_SetXY(30, TM_LCD_GetHeight() - 15); TM_LCD_Puts("stm32f4-discovery.net"); /* Init FFT, FFT_SIZE define is used for FFT_SIZE, samples count is FFT_SIZE * 2, don't use malloc for memory allocation */ TM_FFT_Init_F32(&FFT, FFT_SIZE, 0); /* We didn't used malloc for allocation, so we have to set pointers ourself */ /* Input buffer must be 2 * FFT_SIZE in length because of real and imaginary part */ /* Output buffer must be FFT_SIZE in length */ TM_FFT_SetBuffers_F32(&FFT, Input, Output); while (1) { /* Let's fake sinus signal with 5, 15 and 30Hz frequencies */ do { /* Calculate sinus value */ sin_val = 0; sin_val += (float)0.5 * (float)sin((float)2 * (float)3.14159265359 * (float)1 * (float)freq * (float)i / (float)(FFT_SIZE / 2)); sin_val += (float)0.3 * (float)sin((float)2 * (float)3.14159265359 * (float)2 * (float)freq * (float)i / (float)(FFT_SIZE / 2)); sin_val += (float)0.1 * (float)sin((float)2 * (float)3.14159265359 * (float)3 * (float)freq * (float)i / (float)(FFT_SIZE / 2)); i++; } while (!TM_FFT_AddToBuffer(&FFT, sin_val)); /* Do FFT on signal, values at each bin and calculate max value and index where max value happened */ TM_FFT_Process_F32(&FFT); /* Display data on LCD, only single sided spectrum of FFT */ for (i = 0; i < (TM_FFT_GetFFTSize(&FFT) / 2); i++) { /* Draw FFT results */ DrawBar(30 + 2 * i, TM_LCD_GetHeight() - 30, FFT_BAR_MAX_HEIGHT, TM_FFT_GetMaxValue(&FFT), TM_FFT_GetFromBuffer(&FFT, i), 0x1234, 0xFFFF ); } /* Increase frequency */ freq++; /* Check value */ if (freq > 100) { freq = 0; } /* Little delay */ Delayms(50); } } /* Draw bar for LCD */ /* Simple library to draw bars */ void DrawBar(uint16_t bottomX, uint16_t bottomY, uint16_t maxHeight, uint16_t maxValue, float32_t value, uint16_t foreground, uint16_t background) { uint16_t height; value++; height = (uint16_t)((float32_t)value / (float32_t)maxValue * (float32_t)maxHeight); if (height == maxHeight) { TM_LCD_DrawLine(bottomX, bottomY, bottomX, bottomY - height, foreground); } else if (height < maxHeight) { TM_LCD_DrawLine(bottomX, bottomY, bottomX, bottomY - height, foreground); TM_LCD_DrawLine(bottomX, bottomY - height, bottomX, bottomY - maxHeight, background); } } |

Project is available on Github, download all libraries below.

## Recent comments