当前位置:   article > 正文

STM32F407实现傅里叶变换的三种方法【附源码】_stm32快速傅里叶变换

stm32快速傅里叶变换

一、浅谈傅里叶变换(Fourier Transformation,FT)

1、傅里叶级数

想要了解傅里叶变换,就要先了解一下什么是傅里叶级数。

 

 如图所示,通过不断合成不同频率的正弦波(频率分量),合成后的波形会逐渐形成类似方波的图形。若叠加的正弦波足够多,那么可以认为最终得到的波形就是方波。

换句话说,方波是由许许多多不同频率的正弦波组成的。

不仅如此,我们能见到的,能听到的,能想到的所有波,不管高矮胖瘦,也不论是否奇形怪状,都可以分解为不同频率的正弦波。

可以从三个维度去观察“波”的特性,他们分别是幅度、频域和时域。

在频域中,频率是从0Hz开始的。0Hz有什么意义呢?

在信号处理中,0Hz的正弦波分量实际上对应了信号的直流(DC)分量。正弦波在0Hz时并不是一个振荡的信号,而是一个恒定的值,即信号的均值或直流偏置。

对于任何实数信号,在频率0处(或称为DC分量)通常是非零的,除非信号的平均值为0。这个DC分量表示了信号中恒定的部分,即不随时间变化的部分。

2、傅里叶变换

不难看出,傅里叶级数处理的是周期的,连续的信号,频域上表现为离散的非周期函数。

而傅里叶变换处理的是非周期的,连续的信号,频域上表现为连续的非周期函数。

可以把傅里叶变换看做是处理周期无限大信号的一种方法。

3、离散傅里叶变换(Discrete Fourier Transform,DFT)

我们都知道,计算机采集的信号都是离散的,不可能是连续的。

这时候我们就需要用到离散傅里叶变换——DFT。

4、快速傅里叶变换(Fast Fourier Transform,FFT)

通俗点来讲,快速傅里叶变换就是高效版的离散傅里叶变换。

二、软件实现

软件有三种方法实现傅里叶变换。

在此之前,要先确定采样点数N和采样频率Fs。

详情请看

【电赛2020E题】从硬件到软件icon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/137835864里面有介绍FS和N。

方法一:STM32cubeMX 调用 DSP库

STM32CubeMX关于添加DSP库的使用icon-default.png?t=N7T8https://blog.csdn.net/WandZ123/article/details/125593908

方法二:调用FFT.c和FFT.h文件

方法三:巧用DFT

以电赛2020E题为例,题中要求对1KHz的原信号取五次谐波,那么我们只需关心1KHz到5KHz的的傅里叶变换。即仅需计算1KHz、2KHz...5KHz的点即可(代码中1KHz对应100个点)。

三、原码

这里只展示main.c,需要完整文件请看文章最后。

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "stdio.h"
  28. #include "arm_math.h"
  29. #include "fft.h"
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. #define N 1024
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. float FFT_INPUT[N];
  38. float FFT_OUTPUT[N];
  39. float FFT_1024POINT[2*N];
  40. complex FFT_POINT[N];
  41. _Bool Flag=0;
  42. arm_cfft_radix4_instance_f32 scfft;
  43. /* USER CODE END PD */
  44. /* Private macro -------------------------------------------------------------*/
  45. /* USER CODE BEGIN PM */
  46. /* USER CODE END PM */
  47. /* Private variables ---------------------------------------------------------*/
  48. /* USER CODE BEGIN PV */
  49. /* USER CODE END PV */
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. /* USER CODE BEGIN PFP */
  53. /* USER CODE END PFP */
  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */
  56. float abscomplex(complex *in)
  57. {
  58. float out;
  59. out=in->real*in->real+in->imag*in->imag;
  60. return sqrt(out);
  61. }
  62. void DSP_FFTTC(float* IN)//IN是指主函数的FFT_INPUT[i]
  63. {
  64. u32 i;
  65. for(i=0;i<N;i++)
  66. {
  67. FFT_1024POINT[2*i]=*(IN+i);//存入实部,存入该数组偶数位置
  68. FFT_1024POINT[2*i+1]=0;//虚部置零,存入该数组奇数位置
  69. }
  70. arm_cfft_radix4_init_f32(&scfft,N,0,1);//初始化FFT结构体scfft
  71. arm_cfft_radix4_f32(&scfft,FFT_1024POINT);//进行FFT运算
  72. arm_cmplx_mag_f32(FFT_1024POINT,FFT_OUTPUT,N);//计算FFT的幅度
  73. printf("用DSP库的FFT\n");
  74. for(i=0;i<N/2;i++)
  75. {
  76. if(i==0)//在FFT的结果中,索引为0的元素对应的是直流分量,即频率为0的分量
  77. {
  78. FFT_OUTPUT[i]=FFT_OUTPUT[i]/N;//对于索引为0的元素,对应直流分量,需要除以N进行修正
  79. }
  80. else
  81. {
  82. FFT_OUTPUT[i]=FFT_OUTPUT[i]*2/N;//对于其他元素,乘以2并除以N进行修正
  83. }
  84. printf("第%d个FFT:%.2f\n",i,FFT_OUTPUT[i]);//测试
  85. }
  86. }
  87. void L_FFT(float* IN)
  88. {
  89. u32 i;
  90. for(i=0;i<N;i++)
  91. {
  92. FFT_POINT[i].real=*(IN+i);
  93. FFT_POINT[i].imag=*(IN+i);
  94. /*
  95. fft.h中定义了结构体
  96. typedef struct complex //复数类型
  97. {
  98. float real; //实部
  99. float imag; //虚部
  100. }complex;
  101. */
  102. }
  103. fft(N,FFT_POINT);//傅里叶变换函数
  104. c_abs(FFT_POINT,FFT_OUTPUT,N);//求所有复数的模
  105. printf("普通的FFT\n");
  106. for(i=0;i<N/2;i++)
  107. {
  108. /*详情注释见DSP库FFT*/
  109. if(i==0)
  110. {
  111. FFT_OUTPUT[i]=FFT_OUTPUT[i]/N;
  112. }
  113. else
  114. {
  115. FFT_OUTPUT[i]=FFT_OUTPUT[i]*2/N;
  116. }
  117. printf("第%d个FFT:%.2f\n",i,FFT_OUTPUT[i]);//测试
  118. }
  119. }
  120. void DFT(float* IN)
  121. {
  122. u32 i,k;
  123. complex y={.real=0,.imag=0};
  124. printf("DFT\n");
  125. for(k=0;k<6;k++)
  126. {
  127. for(i=0;i<N;i++)
  128. {
  129. y.real+=*(IN+i)*cos(-2*100*PI*i*k/N);
  130. y.imag+=*(IN+i)*sin(-2*100*PI*i*k/N);
  131. }
  132. y.real=2*y.real/N;
  133. y.imag=2*y.imag/N;
  134. printf("第%d个点的值为%.2f\n",k,abscomplex(&y));
  135. }
  136. }
  137. /* USER CODE END 0 */
  138. /**
  139. * @brief The application entry point.
  140. * @retval int
  141. */
  142. int main(void)
  143. {
  144. /* USER CODE BEGIN 1 */
  145. u16 i;
  146. /* USER CODE END 1 */
  147. /* MCU Configuration--------------------------------------------------------*/
  148. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  149. HAL_Init();
  150. /* USER CODE BEGIN Init */
  151. /* USER CODE END Init */
  152. /* Configure the system clock */
  153. SystemClock_Config();
  154. /* USER CODE BEGIN SysInit */
  155. /* USER CODE END SysInit */
  156. /* Initialize all configured peripherals */
  157. MX_GPIO_Init();
  158. MX_ADC1_Init();
  159. MX_USART1_UART_Init();
  160. MX_TIM2_Init();
  161. MX_TIM3_Init();
  162. /* USER CODE BEGIN 2 */
  163. printf("开始\n");
  164. HAL_TIM_Base_Start_IT(&htim2);
  165. HAL_TIM_Base_Start(&htim3);
  166. HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);//PA6输出PWM
  167. /* USER CODE END 2 */
  168. /* Infinite loop */
  169. /* USER CODE BEGIN WHILE */
  170. while (1)
  171. {
  172. if(Flag)
  173. {
  174. HAL_TIM_Base_Stop(&htim2);
  175. for(i=0;i<1024;i++)
  176. {
  177. FFT_INPUT[i]=FFT_INPUT[i]*3.3f/4096;//除以4096是因为单片机采到的值经过转换后数据寄存器最高位为2的12次方,正好是4096,乘3.3v是为了转化为电压
  178. printf("ADC_VALUE:%.2f\n",FFT_INPUT[i]);
  179. }
  180. DSP_FFTTC(FFT_INPUT);
  181. L_FFT(FFT_INPUT);
  182. DFT(FFT_INPUT);
  183. HAL_Delay(1000);
  184. Flag=0;
  185. HAL_TIM_Base_Start(&htim2);
  186. }
  187. /* USER CODE END WHILE */
  188. /* USER CODE BEGIN 3 */
  189. }
  190. /* USER CODE END 3 */
  191. }
  192. /**
  193. * @brief System Clock Configuration
  194. * @retval None
  195. */
  196. void SystemClock_Config(void)
  197. {
  198. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  199. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  200. /** Configure the main internal regulator output voltage
  201. */
  202. __HAL_RCC_PWR_CLK_ENABLE();
  203. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  204. /** Initializes the RCC Oscillators according to the specified parameters
  205. * in the RCC_OscInitTypeDef structure.
  206. */
  207. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  208. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  209. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  210. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  211. RCC_OscInitStruct.PLL.PLLM = 4;
  212. RCC_OscInitStruct.PLL.PLLN = 168;
  213. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  214. RCC_OscInitStruct.PLL.PLLQ = 4;
  215. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  216. {
  217. Error_Handler();
  218. }
  219. /** Initializes the CPU, AHB and APB buses clocks
  220. */
  221. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  222. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  223. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  224. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  225. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  226. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  227. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. }
  232. /* USER CODE BEGIN 4 */
  233. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  234. {
  235. static u16 counter=0;
  236. if(htim==&htim2)
  237. {
  238. if(!Flag)
  239. {
  240. HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_15);//翻转PB15 用示波器测量以判断定时器频率
  241. FFT_INPUT[counter++]=get_adcvalue();
  242. if(counter==1024)
  243. {
  244. counter=0;
  245. Flag=1;
  246. }
  247. }
  248. }
  249. }
  250. /* USER CODE END 4 */
  251. /**
  252. * @brief This function is executed in case of error occurrence.
  253. * @retval None
  254. */
  255. void Error_Handler(void)
  256. {
  257. /* USER CODE BEGIN Error_Handler_Debug */
  258. /* User can add his own implementation to report the HAL error return state */
  259. __disable_irq();
  260. while (1)
  261. {
  262. }
  263. /* USER CODE END Error_Handler_Debug */
  264. }
  265. #ifdef USE_FULL_ASSERT
  266. /**
  267. * @brief Reports the name of the source file and the source line number
  268. * where the assert_param error has occurred.
  269. * @param file: pointer to the source file name
  270. * @param line: assert_param error line source number
  271. * @retval None
  272. */
  273. void assert_failed(uint8_t *file, uint32_t line)
  274. {
  275. /* USER CODE BEGIN 6 */
  276. /* User can add his own implementation to report the file name and line number,
  277. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  278. /* USER CODE END 6 */
  279. }
  280. #endif /* USE_FULL_ASSERT */

链接:https://pan.baidu.com/s/1DyjH0DaRx3qYEF7I_QdbpA?pwd=hk0x 
提取码:hk0x


参考链接

通俗易懂的理解傅里叶变换(一)[收藏]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/914280
推荐阅读
相关标签
  

闽ICP备14008679号