單片機常用數(shù)字濾波函數(shù)
#define A 10
char value;
char filter()
{
char new_value;
new value="get"_ad();
if ((new_value-value>A)‖(value-new_value> A)
return value;
return new_value;
}
/////////////////////////中位值濾波/////////////////////////
#define N 11
char filter()
{
char value_buf[N];
char count,i,j,temp;
for (count=0;count
value_buf[count]=get_ad();
delay();
}
for (j=0;j
{
for (i=0;i
if(value_buf[i]>value_buf[i+1])
{
temp=value_buf[i];
value_buf[i]=value_buf[i+1];
value_buf[i+1]=temp;
}
}
}
return value_buf[(N-1)/2];
}
/////////////////////////算術(shù)平均濾波//////////////////////
#define N 12
char filter()
{
int sum="0";
for(count=0;count
sum+=get_ad();
delay();
}
return (char)(sum/N);
}
///////////////////去極值平均濾波
#define N 11
int sum="0";
char filter()
{
char value_buf[N];
char count,i,j,temp;
for (count=0;count
value_buf[count]=get_ad();
delay();
}
for (j=0;j
{
for (i=0;i
if(value_buf[i]>value_buf[i+1])
{
temp=value_buf[i];
value_buf[i]=value_buf[i+1];
value_buf[i+1]=temp;
}
}
}
for(count=1;count<(N-1);count++)
{
sum+=value_buf[count];
delay();
}
return (char)(sum/(N-2));
}
///////////////////移動平均濾波(遞推平均濾波)///////////
#define N 12
char value_buf[N];
char i="0";
char filter()
{
char count;
int sum="0";
value_buf[i++]=get_ad();
if(i=N) i=0;
for (count=0;count
return (char)(sum/N)
}
//////////////////////加權(quán)平均濾波///////////////////////
#define N 12
char code jq[N]={1,2,3,4,5,6,7,8,9,10,11,12};//加權(quán)系數(shù)表
char code sum_jq=1+2+3+4+5+6+7+8+9+10+11+12;
char filter()
{
char count;
char value_buf[N];
int sum="0";
for (count=0;count
delay();
}
for (count=0;count
return (char)(sum/sum_jq);
}
/////////////////////////低通濾波////////////////////////
#define a
char value;
char filter()
{
char new_value;
new_value=get_ad();
return (a*new_value+(1-a)*value);
}
評論