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

【经验分享】STM32的DSP库的应用

[复制链接]
STMCU小助手 发布时间:2022-1-15 21:53
本篇内容将简要分析STM32自带的DSP库文件,其用汇编语言编写,代码执行效率明显优于C语言,ST公司封装好了了库文件,我们不必看懂其汇编代码,只要会调用接口函数即可。
1,代码分析
    首先我们需要在一个已经建立好的工程文件里添加如下编译路径:
262145381378421.jpg
工程需要添加的文件如下图:
262149227151512.jpg

       为了产生fft变换信号,我们可以自己产生个采样信号:使用三角函数生成采样点,供FFT计算,fx  = 4000 * sin(PI2*i*50.0/Fs) + 4000 * sin(PI2*i*2500.0/Fs) + 4000*sin(PI2*i*2550.0/Fs)。
      模拟采样数据,采样数据中包含3种频率正弦波:50Hz,2500Hz,2550Hz, lBUFIN数组中,每个单元数据高字(高16位)中存储采样数据的实部,低字(低16位)存储采样数据的虚部(总是为0)。
      其中dsp_asm_init()函数的作用是产生采样信号,实际工程应用中我们使用的是ADC采样的处理值。dsp_asm_test()函数的作用是进行FFT变换,并计算各次谐波幅值。具体代码参见下面:
  1. 1 /*
  2.   2 *********************************************************************************************************
  3.   3 *                                     MICIRUM BOARD SUPPORT PACKAGE
  4.   4 *
  5.   5 *                             (c) Copyright 2007; Micrium, Inc.; Weston, FL
  6.   6 *
  7.   7 *               All rights reserved.  Protected by international copyright laws.
  8.   8 *               Knowledge of the source code may NOT be used to develop a similar product.
  9.   9 *               Please help us continue to provide the Embedded community with the finest
  10. 10 *               software available.  Your honesty is greatly appreciated.
  11. 11 *********************************************************************************************************
  12. 12 */
  13. 13
  14. 14 /*
  15. 15 *********************************************************************************************************
  16. 16 *
  17. 17 *                                        BOARD SUPPORT PACKAGE
  18. 18 *
  19. 19 *                                     ST Microelectronics STM32
  20. 20 *                                              with the
  21. 21 *                                   STM3210B-EVAL Evaluation Board
  22. 22 *
  23. 23 * Filename      : bsp.c
  24. 24 * Version       : V1.00
  25. 25 * Programmer(s) : Brian Nagel
  26. 26 *********************************************************************************************************
  27. 27 */
  28. 28
  29. 29 /*
  30. 30 *********************************************************************************************************
  31. 31 *                                             INCLUDE FILES
  32. 32 *********************************************************************************************************
  33. 33 */
  34. 34
  35. 35 #define  DSP_ASM
  36. 36 #include "stm32f10x.h"
  37. 37 #include "dsp_asm.h"
  38. 38 #include "stm32_dsp.h"
  39. 39 #include "table_fft.h"
  40. 40 #include <stdio.h>
  41. 41 #include <math.h>
  42. 42
  43. 43
  44. 44 /*
  45. 45 *********************************************************************************************************
  46. 46 *                                           LOCAL CONSTANTS
  47. 47 *********************************************************************************************************
  48. 48 */
  49. 49 #define PI2 6.28318530717959
  50. 50 #define NPT_1024 1024
  51. 51 //#define NPT_256 256
  52. 52 //#define NPT_1024 1024
  53. 53
  54. 54 // N=64,Fs/N=50Hz,Max(Valid)=1600Hz
  55. 55 #ifdef NPT_64
  56. 56 #define NPT 64
  57. 57 #define Fs  3200
  58. 58 #endif
  59. 59
  60. 60 // N=256,Fs/N=25Hz,Max(Valid)=3200Hz
  61. 61 #ifdef NPT_256
  62. 62 #define NPT 256
  63. 63 #define Fs  6400
  64. 64 #endif
  65. 65
  66. 66 // N=1024,Fs/N=5Hz,Max(Valid)=2560Hz
  67. 67 #ifdef NPT_1024
  68. 68 #define NPT 1024
  69. 69 #define Fs  5120
  70. 70 #endif
  71. 71
  72. 72
  73. 73 /*
  74. 74 *********************************************************************************************************
  75. 75 *                                          LOCAL DATA TYPES
  76. 76 *********************************************************************************************************
  77. 77 */
  78. 78
  79. 79
  80. 80 /*
  81. 81 *********************************************************************************************************
  82. 82 *                                            LOCAL TABLES
  83. 83 *********************************************************************************************************
  84. 84 */
  85. 85
  86. 86
  87. 87 /*
  88. 88 *********************************************************************************************************
  89. 89 *                                       LOCAL GLOBAL VARIABLES
  90. 90 *********************************************************************************************************
  91. 91 */
  92. 92 long lBUFIN[NPT];         /* Complex input vector */
  93. 93 long lBUFOUT[NPT];        /* Complex output vector */
  94. 94 long lBUFMAG[NPT];/* Magnitude vector */
  95. 95 /*
  96. 96 *********************************************************************************************************
  97. 97 *                                      LOCAL FUNCTION PROTOTYPES
  98. 98 *********************************************************************************************************
  99. 99 */
  100. 100 void dsp_asm_powerMag(void);
  101. 101
  102. 102 /*
  103. 103 *********************************************************************************************************
  104. 104 *                                     LOCAL CONFIGURATION ERRORS
  105. 105 *********************************************************************************************************
  106. 106 */
  107. 107
  108. 108
  109. 109 /*
  110. 110 ******************************************************************************************************************************
  111. 111 ******************************************************************************************************************************
  112. 112 **                                         Global Functions
  113. 113 ******************************************************************************************************************************
  114. 114 ******************************************************************************************************************************
  115. 115 */
  116. 116
  117. 117 void  dsp_asm_init()
  118. 118 {
  119. 119   u16 i=0;
  120. 120   float fx;
  121. 121   for(i=0;i<NPT;i++)
  122. 122   {
  123. 123     fx  = 4000 * sin(PI2*i*50.0/Fs) + 4000 * sin(PI2*i*2500.0/Fs) + 4000*sin(PI2*i*2550.0/Fs);
  124. 124     lBUFIN[i] = ((long)fx)<<16;
  125. 125   }
  126. 126 }
  127. 127
  128. 128 void  dsp_asm_test()
  129. 129 {
  130. 130
  131. 131 #ifdef NPT_64
  132. 132   cr4_fft_64_stm32(lBUFOUT, lBUFIN, NPT);
  133. 133 #endif
  134. 134
  135. 135 #ifdef NPT_256
  136. 136   cr4_fft_256_stm32(lBUFOUT, lBUFIN, NPT);
  137. 137 #endif
  138. 138
  139. 139 #ifdef NPT_1024
  140. 140   cr4_fft_1024_stm32(lBUFOUT, lBUFIN, NPT);
  141. 141 #endif
  142. 142
  143. 143   // 计算幅值
  144. 144   dsp_asm_powerMag();
  145. 145   
  146. 146 }
  147. 147
  148. 148 void dsp_asm_powerMag(void)
  149. 149 {
  150. 150   s16 lX,lY;
  151. 151   u32 i;
  152. 152   for(i=0;i<NPT/2;i++)
  153. 153   {
  154. 154     lX  = (lBUFOUT[i] << 16) >> 16;
  155. 155     lY  = (lBUFOUT[i] >> 16);
  156. 156     {
  157. 157     float X    = NPT * ((float)lX) /32768;
  158. 158     float Y    = NPT * ((float)lY) /32768;
  159. 159     float Mag = sqrt(X*X + Y*Y)/NPT;
  160. 160     lBUFMAG[i]    = (u32)(Mag * 65536);
  161. 161     }
  162. 162   }
  163. 163 }
复制代码
  1. 1 /*
  2. 2 *********************************************************************************************************
  3. 3 *                                     MICIRUM BOARD SUPPORT PACKAGE
  4. 4 *
  5. 5 *                             (c) Copyright 2007; Micrium, Inc.; Weston, FL
  6. 6 *
  7. 7 *               All rights reserved.  Protected by international copyright laws.
  8. 8 *               Knowledge of the source code may NOT be used to develop a similar product.
  9. 9 *               Please help us continue to provide the Embedded community with the finest
  10. 10 *               software available.  Your honesty is greatly appreciated.
  11. 11 *********************************************************************************************************
  12. 12 */
  13. 13
  14. 14 /*
  15. 15 *********************************************************************************************************
  16. 16 *
  17. 17 *                                        BOARD SUPPORT PACKAGE
  18. 18 *
  19. 19 *                                     ST Microelectronics STM32
  20. 20 *                                              with the
  21. 21 *                                   STM3210B-EVAL Evaluation Board
  22. 22 *
  23. 23 * Filename      : bsp.h
  24. 24 * Version       : V1.00
  25. 25 * Programmer(s) : Brian Nagel
  26. 26 *********************************************************************************************************
  27. 27 */
  28. 28
  29. 29 #ifndef  __DSP_ASM_H__
  30. 30 #define  __DSP_ASM_H__
  31. 31
  32. 32 /*
  33. 33 *********************************************************************************************************
  34. 34 *                                               EXTERNS
  35. 35 *********************************************************************************************************
  36. 36 */
  37. 37
  38. 38 #ifdef   DSP_ASM
  39. 39 #define  DSP_EXT
  40. 40 #else
  41. 41 #define  DSP_EXT  extern
  42. 42 #endif
  43. 43
  44. 44 /*
  45. 45 *********************************************************************************************************
  46. 46 *                                             INCLUDE FILES
  47. 47 *********************************************************************************************************
  48. 48 */
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53 /*
  54. 54 *********************************************************************************************************
  55. 55 *                                          GLOBAL VARIABLES
  56. 56 *********************************************************************************************************
  57. 57 */
  58. 58
  59. 59
  60. 60 /*
  61. 61 *********************************************************************************************************
  62. 62 *                                               MACRO'S
  63. 63 *********************************************************************************************************
  64. 64 */
  65. 65
  66. 66
  67. 67 /*
  68. 68 *********************************************************************************************************
  69. 69 *                                            FUNCTION PROTOTYPES
  70. 70 *********************************************************************************************************
  71. 71 */
  72. 72
  73. 73 void    dsp_asm_test(void);
  74. 74 void  dsp_asm_init(void);
  75. 75
  76. 76 #endif                                                          /* End of module include.                               */
复制代码

     着重分析下dsp_asm_powerMag()函数的作用,其函数就是求幅值,首先定义的的一个16位的有符号的数据IX 和IY 这两个只是中间变量,然后定义的i,是32位的无符号型。语句的目的是Mag = sqrt(X*X + Y*Y)/NPT。但直接这么写不符合DSP的计算习惯也就是不符合浮点运算的习惯。因此语句在for函数i写道 lX  = (lBUFOUT << 16) >> 16 就是取32位的i的低16位数据,lY  = (lBUFOUT >> 16);是取高16位数据。下面的两句
    float X    = NPT * ((float)lX) /32768;  
    float Y    = NPT * ((float)lY) /32768
    目的就是把数据浮点化,至于为什么是除以32768 。可以这么说,浮点化就好像10进制里面的科学计数法。32768=2的15次。除以32768也就是去除了浮点数后面的那个基数,只剩下前面的。比如1991 改写成1.991*10的三次幂,再除以10的三次方,只剩下1.991,便于余下的运算。至于最后一句要乘以65536是因为我们定义的数据和我们需要求得的数据都是无符号32位的,之前已经把32位的数据拆开又分别浮点化了又开了个根号,所以再把它变回来 只需要乘以2的16次,也就是65536.比如说问你什么时候生日,你说是19911030,然而DSP是不习惯这么干的,他需要把它拆开为1991和1030。再写成1.991x10的3次方和1.030x10的3次方。然后才能进行其他的运算。
     这里是ST公司采用了DSP专用芯片(主要是指TI)的写法,也就是说尽管DSP的芯片类型很多,数据变量的定义也各有差异,但原理是一样的,最终还是要采用DSP习惯的运算方式。至于为什么一定要采用浮点运算,因为机器是傻子,然而TI公司的工程师是天才。
     main函数中我们只需在while(1)前加上dsp_asm_init();  dsp_asm_test();即可。
2,实验现象
       注意FFT运算结果的对称性,也即256点的运算结果,只有前面128点的数据是有效可用的。
     ① N=64,Fs/N=50Hz,Max(Valid)=1600Hz,64点FFt,采样率3200Hz,频率分辨率50Hz,测量最大有效频率1600Hz
64点FFT运算结果图(局部):
262213093876622.jpg
     上图中,数组下标X对应的谐波频率为:N×Fs/64=N×3200/64=N*50Hz.
     lBUFMAG[1] 对应 50Hz谐波幅值。
    上图中由于FFT分辨率50HZ,最大只能识别1600Hz谐波,导致结果中出现错误的数据。
         ②N=256,Fs/N=25Hz,Max(Valid)=3200Hz,256点FFt,采样率6400Hz,频率分辨率25Hz,测量最大有效频率3200Hz
256点FFT运算结果图(局部):
262216596836465.jpg
     上图中,数组下标X对应的谐波频率为:N×Fs/256=N×6400/256=N*25Hz.
     lBUFMAG[2] 对应 2×25 =50Hz谐波幅值
     lBUFMAG[100] 对应 100×25=2500Hz谐波幅值
     lBUFMAG[102] 对应 102×25=2550Hz谐波幅值
    ③N=1024,Fs/N=5Hz,Max(Valid)=2560Hz,1024点FFt,采样率5120Hz,频率分辨率5Hz,测量最大有效频率2560Hz
1024点FFT运算结果图(局部):
262221245744340.jpg
    上图中,数组下标X对应的谐波频率为:N×Fs/1024=N×5120/1024=N*5Hz.
     lBUFMAG[10] 对应 10×5 =50Hz谐波幅值
     lBUFMAG[500] 对应 500×5=2500Hz谐波幅值
     lBUFMAG[510] 对应 510×5=2550Hz谐波幅值

总结:该工程中模拟信号源为:4000 * sin(PI2*i*50.0/Fs) + 4000 * sin(PI2*i*2500.0/Fs) + 4000*sin(PI2*i*2550.0/Fs)。
信号为1个50Hz、1个2500Hz、1个2550Hz的正弦波混合信号,幅值为均为4000。

收藏 评论0 发布时间:2022-1-15 21:53

举报

0个回答

所属标签

相似分享

官网相关资源

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