#include <stdio.h>
#include <fdacoefs.h>
#define N 81 //阶数
int main(int argc, char *argv[])
{
float z[N-1]; //用于存放z,即x的前N-1个状态
float x,y; //x是当前获取的原始数据,y是滤波后的数据
float h[N]; //can get from 'fdacoefs.h'
int index,s; //index用于指向最后一个元素
int i,j;
FILE *fp_read, *fp_write;
//打开文件
if((fp_read = fopen("sensordata.txt","rt")) == NULL)
{
printf("Open file 'sensordata.txt' failure!/n");
return 0;
}
if((fp_write = fopen("filterdata.txt","wt")) == NULL)
{
printf("Open file 'filterdata.txt' failure!/n");
return 0;
}
//最开始前N-1个数据不进行滤波
i = 0;
while(i != N-1)
{
//Get x from file 'sensordata.txt' or from register
// _________
z[i++] = x;
//Output x
// _________
}
//滤波
index = N-2;
while("can get new x")
{
//Get x from file 'sensordata.txt' or from register
// _________
y = h[0] * x;
j = index;
for(i=1; i<N; i++)
{
y += h[i] * z[j];
j--;
if(j<0)
j += N-1;
}
s = (index+1) % (N-1);
z[s] = y;
index = (index+1) % (N-1);
//Output y
// _________
}
//关闭文件
fclose(fp_read);
fclose(fp_write);
return 0;
}