赞
踩
想要了解傅里叶变换,就要先了解一下什么是傅里叶级数。
如图所示,通过不断合成不同频率的正弦波(频率分量),合成后的波形会逐渐形成类似方波的图形。若叠加的正弦波足够多,那么可以认为最终得到的波形就是方波。
换句话说,方波是由许许多多不同频率的正弦波组成的。
不仅如此,我们能见到的,能听到的,能想到的所有波,不管高矮胖瘦,也不论是否奇形怪状,都可以分解为不同频率的正弦波。
可以从三个维度去观察“波”的特性,他们分别是幅度、频域和时域。
在频域中,频率是从0Hz开始的。0Hz有什么意义呢?
在信号处理中,0Hz的正弦波分量实际上对应了信号的直流(DC)分量。正弦波在0Hz时并不是一个振荡的信号,而是一个恒定的值,即信号的均值或直流偏置。
对于任何实数信号,在频率0处(或称为DC分量)通常是非零的,除非信号的平均值为0。这个DC分量表示了信号中恒定的部分,即不随时间变化的部分。
不难看出,傅里叶级数处理的是周期的,连续的信号,频域上表现为离散的非周期函数。
而傅里叶变换处理的是非周期的,连续的信号,频域上表现为连续的非周期函数。
可以把傅里叶变换看做是处理周期无限大信号的一种方法。
我们都知道,计算机采集的信号都是离散的,不可能是连续的。
这时候我们就需要用到离散傅里叶变换——DFT。
通俗点来讲,快速傅里叶变换就是高效版的离散傅里叶变换。
软件有三种方法实现傅里叶变换。
在此之前,要先确定采样点数N和采样频率Fs。
详情请看
【电赛2020E题】从硬件到软件https://mp.csdn.net/mp_blog/creation/editor/137835864里面有介绍FS和N。
STM32CubeMX关于添加DSP库的使用https://blog.csdn.net/WandZ123/article/details/125593908
以电赛2020E题为例,题中要求对1KHz的原信号取五次谐波,那么我们只需关心1KHz到5KHz的的傅里叶变换。即仅需计算1KHz、2KHz...5KHz的点即可(代码中1KHz对应100个点)。
这里只展示main.c,需要完整文件请看文章最后。
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "adc.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "arm_math.h"
- #include "fft.h"
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- #define N 1024
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
-
- float FFT_INPUT[N];
- float FFT_OUTPUT[N];
- float FFT_1024POINT[2*N];
- complex FFT_POINT[N];
- _Bool Flag=0;
- arm_cfft_radix4_instance_f32 scfft;
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- float abscomplex(complex *in)
- {
- float out;
- out=in->real*in->real+in->imag*in->imag;
- return sqrt(out);
- }
- void DSP_FFTTC(float* IN)//IN是指主函数的FFT_INPUT[i]
- {
- u32 i;
- for(i=0;i<N;i++)
- {
- FFT_1024POINT[2*i]=*(IN+i);//存入实部,存入该数组偶数位置
- FFT_1024POINT[2*i+1]=0;//虚部置零,存入该数组奇数位置
- }
- arm_cfft_radix4_init_f32(&scfft,N,0,1);//初始化FFT结构体scfft
- arm_cfft_radix4_f32(&scfft,FFT_1024POINT);//进行FFT运算
- arm_cmplx_mag_f32(FFT_1024POINT,FFT_OUTPUT,N);//计算FFT的幅度
- printf("用DSP库的FFT\n");
- for(i=0;i<N/2;i++)
- {
- if(i==0)//在FFT的结果中,索引为0的元素对应的是直流分量,即频率为0的分量
- {
- FFT_OUTPUT[i]=FFT_OUTPUT[i]/N;//对于索引为0的元素,对应直流分量,需要除以N进行修正
- }
- else
- {
- FFT_OUTPUT[i]=FFT_OUTPUT[i]*2/N;//对于其他元素,乘以2并除以N进行修正
- }
- printf("第%d个FFT:%.2f\n",i,FFT_OUTPUT[i]);//测试
- }
- }
- void L_FFT(float* IN)
- {
- u32 i;
-
-
- for(i=0;i<N;i++)
- {
- FFT_POINT[i].real=*(IN+i);
-
- FFT_POINT[i].imag=*(IN+i);
-
- /*
- fft.h中定义了结构体
- typedef struct complex //复数类型
- {
- float real; //实部
- float imag; //虚部
- }complex;
- */
-
- }
-
- fft(N,FFT_POINT);//傅里叶变换函数
- c_abs(FFT_POINT,FFT_OUTPUT,N);//求所有复数的模
- printf("普通的FFT\n");
- for(i=0;i<N/2;i++)
- {
-
- /*详情注释见DSP库FFT*/
- if(i==0)
- {
- FFT_OUTPUT[i]=FFT_OUTPUT[i]/N;
- }
- else
- {
- FFT_OUTPUT[i]=FFT_OUTPUT[i]*2/N;
- }
- printf("第%d个FFT:%.2f\n",i,FFT_OUTPUT[i]);//测试
- }
- }
-
- void DFT(float* IN)
- {
- u32 i,k;
- complex y={.real=0,.imag=0};
- printf("DFT\n");
- for(k=0;k<6;k++)
- {
- for(i=0;i<N;i++)
- {
- y.real+=*(IN+i)*cos(-2*100*PI*i*k/N);
- y.imag+=*(IN+i)*sin(-2*100*PI*i*k/N);
- }
- y.real=2*y.real/N;
- y.imag=2*y.imag/N;
- printf("第%d个点的值为%.2f\n",k,abscomplex(&y));
- }
- }
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- u16 i;
- /* 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_ADC1_Init();
- MX_USART1_UART_Init();
- MX_TIM2_Init();
- MX_TIM3_Init();
- /* USER CODE BEGIN 2 */
- printf("开始\n");
- HAL_TIM_Base_Start_IT(&htim2);
- HAL_TIM_Base_Start(&htim3);
- HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);//PA6输出PWM
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
-
- if(Flag)
- {
-
- HAL_TIM_Base_Stop(&htim2);
- for(i=0;i<1024;i++)
- {
-
- FFT_INPUT[i]=FFT_INPUT[i]*3.3f/4096;//除以4096是因为单片机采到的值经过转换后数据寄存器最高位为2的12次方,正好是4096,乘3.3v是为了转化为电压
- printf("ADC_VALUE:%.2f\n",FFT_INPUT[i]);
- }
- DSP_FFTTC(FFT_INPUT);
- L_FFT(FFT_INPUT);
- DFT(FFT_INPUT);
- HAL_Delay(1000);
- Flag=0;
- HAL_TIM_Base_Start(&htim2);
- }
-
-
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
-
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /* USER CODE BEGIN 4 */
-
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- static u16 counter=0;
- if(htim==&htim2)
- {
- if(!Flag)
- {
- HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_15);//翻转PB15 用示波器测量以判断定时器频率
- FFT_INPUT[counter++]=get_adcvalue();
- if(counter==1024)
- {
- counter=0;
- Flag=1;
- }
- }
- }
- }
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
-
-

链接:https://pan.baidu.com/s/1DyjH0DaRx3qYEF7I_QdbpA?pwd=hk0x
提取码:hk0x
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。