【DigiKey“智造万物,快乐不停”创意大赛】6、使用STM32 DSP库处理信号
[复制链接]
首先开启DSP库
然后添加DSP库的头文件
参考armbbs硬汉大佬的数字信号教程,使用matlab设计一个FIR数字滤波器
#define TEST_LENGTH_SAMPLES 1024 //采样点数
#define BLOCK_SIZE 1 //调用一次arm_fir_f32处理的采样点个数
#define NUM_TAPS 29 //滤波器系数个数
uint32_t blockSize = BLOCK_SIZE;
uint32_t numBlocks = TEST_LENGTH_SAMPLES/BLOCK_SIZE; //需要调用arm_fir_f32的次数
static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1]; //状态缓存,大小numTaps + blockSize - 1
//数字低通滤波器系数 通过matlab命令fadtool获取
const float32_t firCoeffs32LP[NUM_TAPS] = {
0.004408003297, 0.005197565537, 0.007328365929, 0.01079409383, 0.01550024934,
0.02126707509, 0.02783845738, 0.03489634395, 0.04207972065, 0.04900695756,
0.0552999489, 0.0606084317, 0.06463276595, 0.06714358181, 0.0679968819,
0.06714358181, 0.06463276595, 0.0606084317, 0.0552999489, 0.04900695756,
0.04207972065, 0.03489634395, 0.02783845738, 0.02126707509, 0.01550024934,
0.01079409383, 0.007328365929, 0.005197565537, 0.004408003297
};
//数字低通滤波器
void arm_fir_f32_lp(float32_t *in,float32_t *out)
{
arm_fir_instance_f32 S;
float32_t *inputF32, *outputF32;
inputF32 = in;
outputF32 = out;
arm_fir_init_f32(&S, NUM_TAPS, (float32_t *)&firCoeffs32LP[0], &firStateF32[0], blockSize);
//FIR滤波,每次处理1个点
for(int i=0; i < numBlocks; i++)
{
arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize);
}
}
在主函数中调用进行数据处理
void data_process(void)
{
for (int i = 0; i < 1024; i++)
{
inputbuf[i]=ADC_DMA_ConvertedValue[i] * 3.3 / 65535-1.65;//直流偏置1.65V,gain=1.0
}
arm_fir_f32_lp(inputbuf,outputbuf);
for (int i = 0; i < 1024; i++)
printf("%f,%f\n",inputbuf[i],outputbuf[i]);
}
在上一个帖子中进行了定时器触发ADC采集,这里输入信号是10KHz正弦、峰峰值0.5V、直流偏置1.65V,含有峰峰值100mV噪声:
墙裂安利VOFA+这款免费酷炫的串口调试软件,可以十分方便的显示串口数据波形,图中红色是滤波前,绿色是滤波后,效果十分nice,平滑作用十分明显:
c3d3641878d694f17fe0f1d120532130
|