中值平均滤波算法
float data[20];
float middleFilter(float in_data)//
{
float sum = 0;
float temp[20];
float change;
int i,j;
for(i=0; i<19; i++)
{
data[i]=data[i+1];
}
data[19] = in_data;
for(i=0; i<20; i++)
temp[i] = data[i];
for(i=1; i<20; i++)
for(j=0; j<20-i; j++)
{
if(temp[j] > temp[j+1])
{
change = temp[j];
temp[j] = temp[j+1];
temp[j+1] = change;
}
}
for(i=5; i<15; i++)
sum = sum + temp[i];
return(sum/10);
}
该函数的功能是采集20个数据进行排序,去掉部分最大值和最小值取中间10个数的平均值,从而达到滤波处理的目的
程序的关键在与data[19] = in_data;把AD数据传进数组,因为AD数据是不断采集的,data数组就用data【18】保存上一次采集的值,data【19】继续更新,这样就能取到一组AD数据的值了,并且不断更新,最后对数组进行从小到大排序后即可。
一阶滞后滤波法
float first_order_lag_filter(float new_value)//
{
static float first_order_value , first_order_last_value;
first_order_value = first_order_last_value;
first_order_last_value = new_value;
return (1 - aa)* new_value + aa * first_order_value;
}
CPU运行速度快,容易跳变