This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

hi iam trying to find FFT using stm32F4 discovery board. i have tried using CFFT and RFFT available in arm CMSIS DSP library but iam getting wrong answers. Can someone please tell me where iam wrong. Iam posting the code below for both RFFT and CFFT ?

#include "main.h"
#include "usb_host.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define ARM_MATH_CM4
 
#include<stdbool.h>
#include<math.h>
#include"arm_math.h"
#include"arm_common_tables.h"
#include"arm_const_structs.h"
 
 
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define SAMPLES					128			/* 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 SAMPLING_RATE        5000
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
 
I2S_HandleTypeDef hi2s3;
 
SPI_HandleTypeDef hspi1;
 
UART_HandleTypeDef huart2;
 
/* USER CODE BEGIN PV */
uint8_t data[10];
uint8_t data2[10];
uint8_t data1[]="\r\n";
uint8_t data3[]="\t";
 
//float real_fsample = 10000;//sampling frequency
int k=0;
float t;
float32_t Input[SAMPLES];
float32_t Output[FFT_SIZE];
float32_t frequencybin[FFT_SIZE];
float32_t maxValue,peakfrequency,ratio;				/* Max FFT value is stored here */
	uint32_t maxIndex;				/* Index in Output array where max value is */
 
uint8_t outarray[14];
char charArray[10],charArray1[10];
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_I2S3_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART2_UART_Init(void);
void MX_USB_HOST_Process(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
 
 
 
	arm_cfft_radix4_instance_f32 S;	/* ARM CFFT module */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
 
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2S3_Init();
  MX_SPI1_Init();
  MX_USB_HOST_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Init(&huart2);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
 
 
    for ( k = 0; k< SAMPLES; k+= 2) {
    	  t=k*.0002;
 
    			/* We assume that sampling and other stuff will take about 1us */
 
    			/* Real part, must be between -1 and 1 */
    			Input[(uint16_t)k] = (float32_t)((float32_t).7*sin(2*3.14159*120*t));
    			/* Imaginary part */
    			Input[(uint16_t)(k + 1)] =0.0;
    		}
 
 
   arm_cfft_radix4_init_f32(&S, FFT_SIZE, 0, 1);
 
    		/* Process the data through the CFFT/CIFFT module */
    		arm_cfft_radix4_f32(&S, Input);
 
    		/* Process the data through the Complex Magniture Module for calculating the magnitude at each bin */
    		arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
 
    		/* Calculates maxValue and returns corresponding value */
    		arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
 
               ratio=(float)SAMPLING_RATE/SAMPLES;
               peakfrequency=ratio*maxIndex;
 
}

#include "main.h"
#include "usb_host.h"
 
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define ARM_MATH_CM4
 
#include<stdbool.h>
#include<math.h>
#include"arm_math.h"
#include"arm_common_tables.h"
#include"arm_const_structs.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
 
#define SAMPLES					128			/* 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 SAMPLING_RATE        5000
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
uint8_t data[10];
uint8_t data2[10];
uint8_t data1[]="\r\n";
uint8_t data3[]="\t";/* USER CODE BEGIN PM */
 
float real_fsample = SAMPLING_RATE;
float32_t Input[SAMPLES];
float32_t Output[SAMPLES],t,output[FFT_SIZE];
float32_t maxValue,peakfrequency;
float32_t frequencybin[FFT_SIZE],ratio;
uint32_t maxIndex;
char charArray[10],charArray1[10];
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
 
I2S_HandleTypeDef hi2s3;
 
SPI_HandleTypeDef hspi1;
 
UART_HandleTypeDef huart2;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_I2S3_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART2_UART_Init(void);
void MX_USB_HOST_Process(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2S3_Init();
  MX_SPI1_Init();
  MX_USB_HOST_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Init(&huart2);
  arm_rfft_fast_instance_f32 fft_handler;
  arm_rfft_fast_init_f32(&fft_handler, SAMPLES);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
    for(int i=0;i<SAMPLES;i++)
    {
    t=i*.001;
    Input[i]=.7*sin(2*3.14159*50*t);
    }
 
    ratio=(float)SAMPLING_RATE/SAMPLES;
 
 
 
                   for(int i=0;i<FFT_SIZE;i++)
                      {
                      	frequencybin[i]=ratio*i;;
                      }
 
 
 
 arm_rfft_fast_f32(&fft_handler,Input,Output,0);
 
 
 	  arm_cmplx_mag_f32(Output,output, SAMPLES);
 	 arm_max_f32(output, FFT_SIZE, &maxValue, &maxIndex);
 
 	   ratio=(float)SAMPLING_RATE/SAMPLES;
 	                 peakfrequency=ratio*maxIndex;
 
 	                for(int i=0;i<FFT_SIZE;i++)
 	                                  {
 	                                  	frequencybin[i]=ratio*i;;
 	                                  }
 
 
 
 	                  
  /* USER CODE END 3 */
}

i am also confused regarding the sampling frequency. where is it specified in CMSIS library?

Top replies