matlab filter怎么写成C语言?求教
- 提问者网友:情歌越听越心酸
- 2021-01-14 11:20
- 五星知识达人网友:不想翻身的咸鱼
- 2021-01-14 12:24
对于第N个数,根据以下的循环就好了
y[n] = 1/a[1] *(b[1]x[n]+b[2]x[n-1]+...+b[N]x[n-B+1]-a[2]y[n-1]-...-a[N]y[n-N+1]);
代码前人肯定有的,例如http://mechatronics.ece.usu.edu/yqchen/filter.c/:注意需要适当改写,另外参数名跟你不同,建议你用matlab给的参数名,能给转换带来方便
#include
#define ORDER 3
#define NP 1001
filter(int ord, float *a, float *b, int np, float *x, float *y)
{
int i,j;
y[0]=b[0]*x[0];
for (i=1;i
y[i]=0.0;
for (j=0;j y[i]=y[i]+b[j]*x[i-j];
for (j=0;j y[i]=y[i]-a[j+1]*y[i-j-1];
}
for (i=ord+1;i
y[i]=0.0;
for (j=0;j
for (j=0;j
}
}
main()
{
FILE *fp;
float x[NP],y[NP],a[ORDER+1],b[ORDER+1];
int i,j;
if((fp=fopen("acc1.dat","r"))!=NULL)
{
for (i=0;i
fscanf(fp,"%f",&x[i]);
}
}
else
{
printf("
file not found!
");
exit(-1);
}
close(fp);
b[0]=0.0007;
b[1]=0.0021;
b[2]=0.0021;
b[3]=0.0007;
a[0]=1.0000;
a[1]=-2.6236;
a[2]=2.3147;
a[3]=-0.6855;
filter(ORDER,a,b,NP,x,y);
for (i=0;i
filter(ORDER,a,b,NP,x,y);
for (i=0;i
for (i=0;i
if((fp=fopen("acc10.dat","w+"))!=NULL)
{
for (i=0;i
fprintf(fp,"%f
",y[i]);
}
}
else
{
printf("
file cannot be created!
");
exit(-1);
}
close(fp);
}