题目要求从信号发生器输出两路波形 A 和 B 并通过一个增益为 1 的加法器生成信号 C,我们需要做的就是从 C 中将 A'和 B'解出来。一种方法是将 C 进行傅里叶变换然后识别出两路波形分别是什么然后将频域分离出来随后进行傅里叶逆变换再输出就行。另一种方法是本文章所使用的方法,由于题目不考虑波形的相位和峰峰值与原波形的关系,所以我们可以将输入的 C 信号进行傅里叶变换识别出两路波形的频率,并且由于 AB 波的峰峰值为固定的 1V,而正弦波为单音信号频谱集中,三角波有三次谐波的存在导致基波信号的峰峰值会被削弱使得可以通过比较频谱中基波的幅值大小来识别输入的波形是三角波还是正弦波,然后就能得到两路波的波形以及频率,随后就可以使用 DDS 重构出 A'和 B'波形。但是这个方法有一个问题就是由于重构的波形与信号发生器所出的波形不在同一个时钟上,由于不同时钟的微小差异,会使得相位差在不停的累积就会产生信号漂移,所以我们需要设计一个锁相环将生成的信号与原信号的相位锁住就不会再产生漂移了,移相只需要改变 ROM 表的地址就可以实现。
基本框架
C 信号——>高速 ADC——>FPGA——>FIFO——>串口——>stm32——>FFT——>识别波形和频率——>串口——>DDS——>锁相环——>高速 DAC——>A',B'
/* USER CODE BEGIN Header *//** ******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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"i2c.h"#include"tim.h"#include"usart.h"#include"gpio.h"/* Private includes ----------------------------------------------------------*//* USER CODE BEGIN Includes */#include"key.h"#include"OLED.h"#include"LED.h"#include"math.h"#include"arm_math.h"#include"arm_const_structs.h"/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*//* USER CODE BEGIN PTD */float max[2]; uint16_t idx[2]; uint16_t freq,freq_last;
float FFT_input[4096],FFT_output[2048]; float freq_change[2048];
uint16_t fre[2048]; uint16_t cnt; float v_max;
uint8_t wave[2]; uint8_t tx_data[6]; uint8_t flag;
uint8_t rx_data[2048]; uint8_t wave_form[2048];
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*//* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*//* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */char message[50];
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/voidSystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*//* USER CODE BEGIN 0 *//* USER CODE END 0 *//**
* @brief The application entry point.
* @retval int
*/intmain(void) {
/* USER CODE BEGIN 1 *//* 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_I2C1_Init(); MX_TIM2_Init(); MX_TIM4_Init(); MX_TIM3_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart2,rx_data,2048); OLED_Init();
OLED_PrintString(0,0,"Init Success",&font16x16,OLED_COLOR_NORMAL);
OLED_ShowFrame(); HAL_Delay(500);
/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1) {
for (int i = 0; i < 2048; i++) {
FFT_input[i * 2] = wave_form[i]; FFT_input[i * 2 + 1] = 0;
}
arm_cfft_f32(&arm_cfft_sR_f32_len2048, FFT_input, 0, 1);
arm_cmplx_mag_f32(FFT_input, FFT_output, 2048);
FFT_output[0] /= 2048;
for (int i = 1; i < 2048; i++) { FFT_output[i] /= 1024; }
for (int i=35; i<1000;i++) {
freq = (int)(((float)i*1000000/2048/5000)+0.5);
if(freq==freq_last) { v_max = FFT_output[i]*FFT_output[i]+v_max; }
else { v_max = sqrt(v_max); freq_change[cnt] = v_max; fre[cnt] = freq; freq_last=freq; cnt++; }
}
v_max=0; cnt=0;
for (int i = 0; i < 25; i++) { printf("%d,%.2f\n",fre[i],freq_change[i]); }
for(int i=0;i<100;i++) {
if(freq_change[i]>max[0]) { max[1]=max[0]; idx[1]=idx[0]; max[0]=freq_change[i]; idx[0]=2.5*(fre[i]-1); }
elseif(freq_change[i]>max[1] && freq_change[i]<max[0]) { max[1]=freq_change[i]; idx[1]=2.5*(fre[i]-1); }
}
if(max[0]>5.7) wave[0] = 0; //正弦波else wave[0] = 1; //三角波if(max[1]>5.7) wave[1] = 0;
else wave[1] = 1;
if(idx[0]>idx[1]) {
tx_data[0]=0xff; tx_data[1]=wave[1]; tx_data[2]=idx[1];
tx_data[3]=wave[0]; tx_data[4]=idx[0]; tx_data[5]=0xfe;
} else {
tx_data[0]=0xff; tx_data[1]=wave[0]; tx_data[2]=idx[0];
tx_data[3]=wave[1]; tx_data[4]=idx[1]; tx_data[5]=0xfe;
}
HAL_UART_Transmit(&huart2,tx_data,6,HAL_MAX_DELAY);
OLED_NewFrame();
sprintf(message,"%d : %d %.2f",wave[0],idx[0],max[0]);
OLED_PrintString(0,0,message,&font16x16,OLED_COLOR_NORMAL);
sprintf(message,"%d : %d %.2f",wave[1],idx[1],max[1]);
OLED_PrintString(0,20,message,&font16x16,OLED_COLOR_NORMAL);
OLED_ShowFrame();
for(uint8_t i=0;i<2;i++) { max[i]=0; idx[i]=0; wave[i]=0; }
/* USER CODE END WHILE *//* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/voidSystemClock_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 = 6; 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 */voidHAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART2) {
for(int i=0;i<2048;i++) { wave_form[i]=rx_data[i]; }
HAL_UART_Receive_IT(&huart2,rx_data,2048);
}
}
/* USER CODE END 4 *//**
* @brief This function is executed in case of error occurrence.
* @retval None
*/voidError_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
*/voidassert_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 */
3.stm32 串口发送
由于 A 波的频率始终小于 B 波,所以我们要先判断那个是 A 波哪个是 B 波,然后通过串口发送给 FPGA
module key ( input clk, input reset, input [3:0] key, output [3:0] key_num );
reg[31:0] timer; reg[3:0] key_first; reg[3:0] key_second; reg[3:0] key_now;
assign key_num=key_now;
always@(posedge clk or negedge reset) begin
if(reset==0) begin key_now<=4'b0000; key_first<=4'b1111; key_second<=4'b1111; timer<=32'd0; end
else begin
if(key!=4'b1111) begin key_first<=key; timer<=timer+32'd1; end
else begin timer<=32'd0; if(key_first==key_second) begin key_now<=~key_first; key_second<=0; end else key_now<=0; end
if(timer==32'd999_999) key_second<=key;
end
end
endmodule
2.按键设置相位差
需要用前三个按键将相位手动调零,然后按下第四个按键表示调零完毕,再通过前三个按键设置相位差
key key_inst ( .clk(clk), .reset(rst), .key(key), .key_num(key_num) );
always@(posedge clk or negedge rst) begin
if(!rst) begin phase<=0; phase0<=0; end
else begin
if(key_num == 4'b0001) begin phase<=phase+1; end
else if(key_num == 4'b0010) begin phase<=phase+5; end
else if(key_num == 4'b0100) begin phase<=phase+30; end
else if(key_num == 4'b1000) begin phase0<=phase; phase<=0; end
if(phase0 == 0) begin if(phase>360) phase<=phase-360; end
else begin if(phase>180) phase<=phase-180; end
end
end
3.数码管显示相位
module nixie( input clk, input rst, input [7:0] data, output [7:0] SMG_Data, output [5:0] Scan_Sig );
reg [5:0] sig; assign Scan_Sig = sig;
reg [7:0] num; reg [31:0] timer; reg clk_low; reg [9:0] count;
always@(posedge clk or negedge rst) begin
if(!rst) begin count<=0; clk_low<=0; end
else begin count<=count+1; if(count==49) begin count<=0; clk_low <= ~clk_low; end
end
end
always@(posedge clk_low or negedge rst) begin
if(!rst) begin num<=8'd0; timer<=0; end
else begin timer<=timer+1;
if(timer==50) begin sig=6'b011111; num = 8'h11; end
else if(timer==100) begin sig=6'b101111; num = 8'h11; end
else if(timer==150) begin sig=6'b110111; num = 8'h11; end
else if(timer==200) begin sig=6'b111011; num = 8'h11; end
else if(timer==250) begin sig=6'b111101; num = 8'h11; end
else if(timer==300) begin sig=6'b111110; num = 8'h11; timer<=0; end
if(sig == 6'b011111) begin num <= ~(8'h73); end
else if(sig == 6'b101111) begin num <= ~(8'h74); end
else if(sig == 6'b110111) begin num <= ~(8'h40); end
else if(sig == 6'b111011) begin case(data%1000/100)
1: num<=~(8'h06); 2: num<=~(8'h5b); 3: num<=~(8'h4f); 4: num<=~(8'h66); 5: num<=~(8'h6d); 6: num<=~(8'h7d); 7: num<=~(8'h07); 8: num<=~(8'h7f); 9: num<=~(8'h6f); 0: num<=~(8'h3f); endcase
end
else if(sig == 6'b111101) begin case(data%100/10)
1: num<=~(8'h06); 2: num<=~(8'h5b); 3: num<=~(8'h4f); 4: num<=~(8'h66); 5: num<=~(8'h6d); 6: num<=~(8'h7d); 7: num<=~(8'h07); 8: num<=~(8'h7f); 9: num<=~(8'h6f); 0: num<=~(8'h3f); endcase
end
else if(sig == 6'b111110) begin case(data%10)
1: num<=~(8'h06); 2: num<=~(8'h5b); 3: num<=~(8'h4f); 4: num<=~(8'h66); 5: num<=~(8'h6d); 6: num<=~(8'h7d); 7: num<=~(8'h07); 8: num<=~(8'h7f); 9: num<=~(8'h6f); 0: num<=~(8'h3f); endcase
end
end
end
assign SMG_Data = num;
endmodule