根据公式
,我试图在C中实现双二阶低通滤波器y [n] = a0 * x [n] + a1 * x [n-1] + a2 * x [n-2] - b1 * y [n-1] - b2 * y [n-2]
我给出幅度为0.8的正弦扫描输入,并对输出进行滤波。但正弦的幅度正在增加并被削减。这是因为C代码中有任何错误吗?
代码是
#include <stdio.h>
//#include
float in_state[40];
float out_state[40];
#define blocksize 32
float coeff[5] = {
0.6589417254311722,
1.3178834508623445,
0.6589417254311722,
-1.1314481923390387,
-0.5043187093856503,
};
void biquad(float* x, float * y)
{
in_state[0] = in_state[blocksize];
in_state[1] = in_state[blocksize+1];
out_state[0] = out_state[blocksize];
out_state[1] = out_state[blocksize+1];
#if 1
for(int i=0;i<blocksize;i++)
{
in_state[i+2]=x[i];
}
#endif
for(int i = 0; i<blocksize;i++)
{
float temp = coeff[0]*in_state[i+2]+ // a0 * x[n]
coeff[1]*in_state[i+1]+ // a1 * x[n-1]
coeff[2]*in_state[i]+ // a2 * x[n-2]
coeff[3]*out_state[i+1]+ // -b1 * y[n-1]
coeff[4]*out_state[i]; // -b2 * y[n-2]
out_state[i+2] = temp;
y[i]=temp;
}
}
int main()
{
FILE *infile = fopen("sweep.dat","rb");
FILE *outfile = fopen("filter.dat","wb");
float inbuf[32],outbuf[32];
while(fread(inbuf,sizeof(float),blocksize,infile) == blocksize)
{
biquad(inbuf,outbuf);
fwrite(outbuf,sizeof(float),blocksize,outfile);
}
fclose(infile);
fclose(outfile);
}
我从此网站Biquad calculator
获取的滤镜系数答案 0 :(得分:1)
您为滤波器系数引入了增益。确保你有均匀的收益。您必须在站点上选择0 dB增益:)