}, {
1, -1.999938965, 1 }, {
0.9985351563, 0, 0 }, {
1, -1.99987793, 1 }, {
0.9996337891, 0, 0 }, {
1, -1.99987793, 1 }, {
1, 0, 0 } };
const float IIR_A[IIR_NSEC][3] = { {
1, 0, 0 }, {
1, -1.938049316, 0.9401855469 }, {
1, 0, 0 }, {
1, -1.989501953, 0.9900512695 }, {
1, 0, 0 }, {
1, -1.996887207, 0.9971923828 }, {
1, 0, 0 }, {
1, -1.999084473, 0.9993286133 }, {
1, 0, 0 } };
保存文件,然后使用以下代码进行滤波
这段代码是根据Direct Form I 2阶IIR滤波的差分方程编写的
a0*y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] -a2*y[n-2];
//iir_filter.c
#include \#include \#include \
static float y[IIR_NSEC][3]; static float x[IIR_NSEC+1][3];
int16 iir_filter(int16 in) {
uint16 i;
x[0][0] = in;
for(i=0;i y[0] =x[0]*IIR_B[0]+x[1]*IIR_B[1]+x[2]*IIR_B[2]-y[1]*IIR_A[1]-y[2]*IIR_A[2]; y[0] /= IIR_A[0]; y[2]=y[1];y[1]=y[0]; x[2]=x[1];x[1]=x[0]; x[i+1][0] = y[0]; } if( x[IIR_NSEC][0]>32767) x[IIR_NSEC][0]=32767; if( x[IIR_NSEC][0]<-32768) x[IIR_NSEC][0]=-32768; return ((int16)x[IIR_NSEC][0]); } //复位滤波器 void iir_reset(void) { uint16 i,j; for(i=0;i for(j=0;j<3;j++) { x[j]=0; } } for(i=0;i for(j=0;j<3;j++) { y[j]=0; } } } //iir_filter.h #ifndef _IIR_FILTER_H__ #define _IIR_FILTER_H__ int16 iir_filter(int16 x); void iir_reset(void); #endif 使用方法: 首先写好iir_coefs.h,然后调用iir_filter.c对数据流进行滤波 一个伪代码例子: while(运行中) { 保存到SD卡(iir_filter(读取ADC采样值())); } 这个函数比STM32 DSP库中的函数要好很多,DSP库中的2个IIR滤波函数都不能连续处理数据流。 记得在开始滤波之前重置滤波器 iir_reset();
相关推荐: