赞
踩
这个代码输出不了测量后的相位差数据,但是不知道哪里有错误。
各位大佬麻烦帮帮忙【哭哭哭】
加了限幅滤波代码就更跑不动了,不知道该怎么办。
#include "stm32f10x.h"
#include "arm_math.h"
#include "math.h"
#include "key.h"
#include "stdio.h"
#include "delay.h"
#include "oled.h"
#include "bmp.h"
#include "led.h"
#include "sys.h"
#include "usart.h"
#define M_PI 3.14159265358979323846264338327
#define SIGNAL_LENGTH 1024
#define SIGNAL_GPIO_PORT GPIOA
#define SIGNAL_PIN_1 GPIO_Pin_6 // PA6
#define SIGNAL_PIN_2 GPIO_Pin_7 // PA7
#define THRESHOLD 2000 // 限幅阈值
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能 GPIO 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置 GPIO 引脚为输入模式
GPIO_InitStructure.GPIO_Pin = SIGNAL_PIN_1 | SIGNAL_PIN_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SIGNAL_GPIO_PORT, &GPIO_InitStructure);
}
extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024;
// 定义输入信号数组
float32_t inputSignal1[SIGNAL_LENGTH];
float32_t inputSignal2[SIGNAL_LENGTH];
// 定义 FFT 输出缓冲区
float32_t fftOutput1[SIGNAL_LENGTH];
float32_t fftOutput2[SIGNAL_LENGTH];
float32_t filteredSignal1[SIGNAL_LENGTH]; // 经过限幅滤波后的信号1
float32_t filteredSignal2[SIGNAL_LENGTH]; // 经过限幅滤波后的信号2
void generateSignals(void)
{
// 生成信号1(以正弦波为例)
for (int i = 0; i < SIGNAL_LENGTH; i++)
{
inputSignal1[i] = 1.5* sin(2 * M_PI * 50*i);
}
// 生成信号2(以正弦波为例)
for (int i = 0; i < SIGNAL_LENGTH; i++)
{
inputSignal2[i] = 1.5 * sin(2 * M_PI * 50*i+ M_PI/2);
}
}
int main(void)
{
// 初始化 GPIO 引脚
GPIO_Configuration();
// 初始化输入信号
generateSignals();
// 初始化DAC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_InitTypeDef DAC_InitStructure;
DAC_StructInit(&DAC_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
int index = 0;
delay_init();
uart_init(115200);
LED_Init();
KEY_Init();
u8 key_val = 0;
// 无限循环发送信号
while (1)
{
// 输出信号1
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(inputSignal1[index] + 2048));
// 输出信号2
DAC_SetChannel2Data(DAC_Align_12b_R, (uint16_t)(inputSignal2[index] + 2048));
// 更新index并延时
index = (index + 1) % SIGNAL_LENGTH;
for (uint32_t i = 0; i < 10000; i++); // 延时一段时间
// 执行FFT
arm_cfft_f32(&arm_cfft_sR_f32_len1024, inputSignal1, 0, 1);
arm_cfft_f32(&arm_cfft_sR_f32_len1024, inputSignal2, 0, 1);
// 计算相位谱
float32_t phaseSpectrum1[SIGNAL_LENGTH];
float32_t phaseSpectrum2[SIGNAL_LENGTH];
for (int i = 0; i < SIGNAL_LENGTH; i++)
{
float32_t real1 = inputSignal1[i];
float32_t imag1 = 0;
phaseSpectrum1[i] = atan2f(real1, imag1);
float32_t real2 = inputSignal2[i];
float32_t imag2 = 0;
phaseSpectrum2[i] = atan2f(real2, imag2);
}
// 计算相位差
float32_t phaseDifference = phaseSpectrum1[0] - phaseSpectrum2[0];
phaseDifference = fmod(phaseDifference, 2 * M_PI);
if (phaseDifference > M_PI) {
phaseDifference -= 2 * M_PI;
} else if (phaseDifference < -M_PI) {
phaseDifference += 2 * M_PI;
}
// 检测按键
key_val = KEY_Scan(0);
if(key_val == KEY0_PRES)
{
printf("相位差是:%.3f\r\n", phaseDifference);
}
else if(key_val == KEY1_PRES)
{
printf("重置\r\n");
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。