[DemoCode下载] M451基于DSP内核的FFT

[复制链接]
840|2
 楼主| xinpian101 发表于 2018-9-25 22:57 | 显示全部楼层 |阅读模式
EC_M451_DSP_FFT_V1.0.zip (4.43 MB, 下载次数: 15)


 楼主| xinpian101 发表于 2018-9-25 22:57 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.0
  4. * $Date: 16/09/22 10:04a $
  5. * @brief
  6. *         Display how to use DSP FFT function
  7. *                    
  8. * @note
  9. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M451Series.h"
  14. #include "arm_math.h"

  15. #define TEST_LENGTH_SAMPLES 2048 //Must be fftsize*2

  16. /* -------------------------------------------------------------------
  17. * External Input and Output buffer Declarations for FFT Bin Example
  18. * ------------------------------------------------------------------- */
  19. extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];
  20. static float32_t testOutput[TEST_LENGTH_SAMPLES/2];

  21. /* ------------------------------------------------------------------
  22. * Global variables for FFT Bin Example
  23. * ------------------------------------------------------------------- */
  24. uint32_t fftSize = 1024;
  25. uint32_t ifftFlag = 0;
  26. uint32_t doBitReverse = 1;
  27. /* Reference index at which max energy of bin ocuurs */
  28. uint32_t refIndex = 213, testIndex = 0;
  29. arm_cfft_radix4_instance_f32 S;
  30. float32_t maxValue,Mainfreq;
  31. int CalTime;
  32. void SYS_Init(void)
  33. {
  34.            /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init System Clock                                                                                       */
  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Enable HIRC clock */
  38.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  39.     /* Waiting for HIRC clock ready */
  40.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  41.     /* Switch HCLK clock source to HIRC */
  42.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  43.     /* Enable HXT */
  44.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  45.     /* Waiting for clock ready */
  46.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  47.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  48.     CLK_SetCoreClock(72000000);
  49.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  50.     /* Enable peripheral clock */
  51.     CLK_EnableModuleClock(UART0_MODULE);
  52.     CLK_EnableModuleClock(TMR0_MODULE);

  53.     /* Peripheral clock source */
  54.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, CLK_CLKDIV0_UART(1));
  55.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

  56. }

  57. void UART_Init(void)
  58. {
  59.     /* Set PD multi-function pins for UART0 RXD and TXD */
  60.     SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
  61.     /* Reset UART module */
  62.     SYS_ResetModule(UART0_RST);

  63.     /* Configure UART0 and set UART0 Baudrate */
  64.     UART_Open(UART0, 115200);
  65. }

  66. /*---------------------------------------------------------------------------------------------------------*/
  67. /*  Main Function                                                                                          */
  68. /*---------------------------------------------------------------------------------------------------------*/
  69. int32_t main(void)
  70. {
  71.         /* Unlock protected registers */
  72.     SYS_UnlockReg();

  73.     /* Init System, peripheral clock and multi-function I/O */
  74.     SYS_Init();

  75.     /* Lock protected registers */
  76.     SYS_LockReg();

  77.     /* Init UART for printf */
  78.     UART_Init();

  79.                 /* Measure FFT calcultion time */
  80.                 TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
  81.                 TIMER_Start(TIMER0);
  82.          
  83.                 /* Initialize the CFFT/CIFFT module */  
  84.                 arm_cfft_radix4_init_f32(&S, fftSize,ifftFlag, doBitReverse);
  85.        
  86.                 /* Process the data through the CFFT/CIFFT module */
  87.                 arm_cfft_radix4_f32(&S, testInput_f32_10khz);
  88.                 TIMER_Close(TIMER0);
  89.                 /* Process the data through the Complex Magnitude Module for calculating the magnitude at each bin */
  90.                 arm_cmplx_mag_f32(testInput_f32_10khz, testOutput,fftSize/2);  

  91.                 /* Calculates maxValue and returns corresponding BIN value */
  92.                 arm_max_f32(testOutput, fftSize/2, &maxValue, &testIndex);

  93.                 CalTime=TIMER_GetCounter(TIMER0);

  94.                 /* Mainfreq=testindex*sample rate/fftsize */
  95.                 Mainfreq=testIndex*5000/fftSize;
  96.                
  97.                 printf("Main frequency is %f\n",Mainfreq);
  98.                 printf("time is %d \n",CalTime);
  99.          
  100.     while(1);      
  101.          
  102. }

 楼主| xinpian101 发表于 2018-9-25 22:57 | 显示全部楼层
那么有人知道离散FFT的原理吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

129

主题

1650

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部