你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32H7的DSP的FFT测试

[复制链接]
STMCU小助手 发布时间:2021-12-26 16:43
前面是关于怎样运行fpu的,以及相关的环境配置

FFT的测试
先看一段关于官方的FFT测试例程

里面添加了相关注释

  1. #include "arm_math.h"
  2. #include "arm_const_structs.h"

  3. #define TEST_LENGTH_SAMPLES 2048

  4. /* -------------------------------------------------------------------
  5. * External Input and Output buffer Declarations for FFT Bin Example
  6. * ------------------------------------------------------------------- */
  7. extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];
  8. static float32_t testOutput[TEST_LENGTH_SAMPLES/2];

  9. /* ------------------------------------------------------------------
  10. * Global variables for FFT Bin Example
  11. * ------------------------------------------------------------------- */
  12. uint32_t fftSize = 1024;
  13. uint32_t ifftFlag = 0;
  14. uint32_t doBitReverse = 1;

  15. /* Reference index at which max energy of bin ocuurs */
  16. uint32_t refIndex = 213, testIndex = 0;

  17. /* ----------------------------------------------------------------------
  18. * Max magnitude FFT Bin test
  19. * ------------------------------------------------------------------- */

  20. int32_t main(void)
  21. {

  22.   arm_status status;
  23.   float32_t maxValue;

  24.   status = ARM_MATH_SUCCESS;

  25. //注意这个模块的参数的相关用法,下面这个将是我们后面进行调用的函数模块
  26.   /* Process the data through the CFFT/CIFFT module */
  27.   arm_cfft_f32(&arm_cfft_sR_f32_len1024, testInput_f32_10khz, ifftFlag, doBitReverse);
  28.         //可以看到要想使用此函数,将调用4个参数
  29.         //参数1:指向浮点CFFT结构的一个实例。两种:arm_cfft_sR_f32_len1024、arm_cfft_sR_q31_len1024;
  30.         //参数2:数据缓冲区的起始地址,偶数位为实数位,奇数位为复数
  31.         //参数3:是否逆FFT标志位;1-是
  32.         //参数4:是否位反转输出标志位;1-是
  33.         //注意:此函数将覆盖源数据,并将其修改为对应操作后的数据


  34. //计算频域的幅度,由于对称性,只取前一半
  35.   /* Process the data through the Complex Magnitude Module for
  36.   calculating the magnitude at each bin */
  37.   arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);

  38. //计算频域的最大值
  39.   /* Calculates maxValue and returns corresponding BIN value */
  40.   arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);

  41.   if (testIndex !=  refIndex)
  42.   {
  43.     status = ARM_MATH_TEST_FAILURE;
  44.   }

  45.   /* ----------------------------------------------------------------------
  46.   ** Loop here if the signals fail the PASS check.
  47.   ** This denotes a test failure
  48.   ** ------------------------------------------------------------------- */

  49.   if ( status != ARM_MATH_SUCCESS)
  50.   {
  51.     while (1);
  52.   }

  53.   while (1);                             /* main function does not return */
  54. }
复制代码

实际使用后的测试
实际使用的时候,我们并不是使用对应的源代码c文件,而是使用对应的lib文件。
当我移植到工程中后,等下发布效果,不要在意上面如何能够运行,弄完后自己再实现。

python处理源数据得到频谱
源数据有2048个数据,但实际是由实部和虚部组成的,所以实际只有1024个数据点

20210119142023633.png


python会得到一部分负频率的成分,但是一般我们只讨论正频率部分

stm32处理源数据得到频谱
这是stm32通过串口上传至电脑,然后用excel绘制的图像

20210119145755419.png


两个结果一致

FFT如何使用

源数据------》调用相关函数-----》频域
频域中的各个点对应着相应的频率,幅值对应着当期成分的含量占比大小

cubemx的工程建立
和普通的工程一样,不过建立之前需要注意勾选,复制所有文件到当前工程中;如果后面发现没有我们要的文件,可能是这一步的问题

20210119151535753.png


添加相应的库文件
先来查看工程文件夹中的一些文件
20210119152533686.png


这里面的文件目录是:
工程文件夹\Drivers\CMSIS\Lib\ARM
里面就是我们所需的库文件

下面添加到我们的工程中:

20210119153038371.png


然后设置添加文件的类型:

20210119153208652.png


选中小端模式的库,然后点击add,如果选错了,后面将不能编译,然后点击关闭窗口

20210119153345635.png


20210119153500594.png


Keil工程的设置

开启FPU并设置编译器:
将下面的东西复制到后面的图片中

  1. ,__FPU_PRESENT=1,__FPU_USED=1,ARM_MATH_CM7,__CC_ARM
复制代码

然后添加一些文件

2021011916003141.png


在这里输入
…/Drivers/CMSIS/DSP/Include

20210119160141855.png


结果像这样
20210119160247877.png


然后点击ok

还要设置一个东东

20210119154104108.png



这里使用的是STM32H750,所以后面提到的ARM_MATH_CM7后缀是M7

现在就可以使用库里面的函数了

下面是测试

进行FFT测试
测试一
添加测试文件

使用的是官方测试文件

20210119154843902.png


这是例程所在目录

20210119155008969.png


20210119155111683.png


2021011915514321.png


然后打开我们的main.c进行编辑
注意:

我用的是uart4进行的串口通信

20210119155432852.png



  1. #include <stdio.h>
  2. #include "arm_math.h"
  3. #include "arm_const_structs.h"

  4. #define TEST_LENGTH_SAMPLES 2048

  5. extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];//only have 1024
  6. static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
  7. uint32_t fftSize = 1024;
  8. uint32_t ifftFlag = 0;
  9. uint32_t doBitReverse = 1;

  10. /* Reference index at which max energy of bin ocuurs */
  11. uint32_t refIndex = 213, testIndex = 0;


  12. int fputc(int ch,FILE *f)
  13. {
  14.     uint8_t temp[1]={ch};
  15.     HAL_UART_Transmit(&huart4,temp,1,2);        //uart4   一定要修改为自己的串口!!!!!!!!!!!!
  16. }
复制代码

然后

20210119155723560.png


  1. arm_status status;
  2.   float32_t maxValue;

  3.   status = ARM_MATH_SUCCESS;

  4.   /* Process the data through the CFFT/CIFFT module */
  5.   arm_cfft_f32(&arm_cfft_sR_f32_len1024, testInput_f32_10khz, ifftFlag, doBitReverse);

  6.   /* Process the data through the Complex Magnitude Module for
  7.   calculating the magnitude at each bin */
  8.   arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);

  9.   /* Calculates maxValue and returns corresponding BIN value */
  10.   arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);

  11.   if (testIndex !=  refIndex)
  12.   {
  13.     status = ARM_MATH_TEST_FAILURE;
  14.   }

  15.   /* ----------------------------------------------------------------------
  16.   ** Loop here if the signals fail the PASS check.
  17.   ** This denotes a test failure
  18.   ** ------------------------------------------------------------------- */

  19.   if ( status != ARM_MATH_SUCCESS)
  20.   {
  21.     while (1);
  22.   }

  23.   while (1)
  24.         {
  25.         //printf("hello\n");
  26.         HAL_Delay(5000);
  27.         for(int i =0;i<1024;i++)
  28.         {printf("%f\n",testOutput<i>);
  29.         </i>HAL_Delay(10);
  30.         }
  31.         while(1);
  32.         }
复制代码

这里完了就可以编译了

结果探究
打开串口助手,等待5s后会发送

20210119161237405.png


然后提取之后就是我们要的数据了,可以保存后用excel来进行绘制,就是我们上面的图像了


20210119153944812.png
收藏 评论0 发布时间:2021-12-26 16:43

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版