NO.1
限幅滤波
1 方法
2 优缺点
克服脉冲干扰,无法抑制周期性干扰,平滑度差。
3 代码
/* A值根据实际调,Value有效值,new_Value当前采样值,程序返回有效的实际值 */
# define A 10
char Value;
char filter()
{
char new_Value;
new_Value = get_ad(); //获取采样值
if( abs(new_Value - Value) > A)
return Value; //abs()取绝对值函数
return new_Value;
}
NO.2
中位值滤波
1 方法
2 优缺点
克服波动干扰,对温度等变化缓慢的被测参数有良好的滤波效果,对速度等快速变化的参数不宜。
3 代码
#define N 11
char filter(){
char value_buf[N];
charcount,i,j,temp;
for(count =0;count < N;count++) //获取采样值
{
value_buf[count] = get_ad();
delay();
}
for(j =0;j<(N-1);j++)
for(i =0;i<(n-j);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];
}
NO.3
算数平均滤波
1 方法
2 优缺点
适用于存在随机干扰的系统,占用RAM多,速度慢。
3 代码
#define N 12
charfilter(){
int sum =0;
for(count =0;count<N;count++)
sum +=get_ad();
return(char)(sum/N);
}
NO.4
递推平均滤波
1 方法
2 优缺点
3 代码
#define A 10
char Value;
char filter(){
char new_Value;
new_Value=get_ad();
if(abs(new_Value-Value)>A)
return Value;
return new_Value;
}
NO.5
中位值平均滤波
1 方法
2 优缺点
3 代码
char filter(){
char count,i,j;
char Value_buf[N];
int sum=0;
for(count=0;count<N;count++)
Value_buf[count]= get_ad();
for(j=0;j<(N-1);j++)
for(i=0;i<(N-j);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];
return(char)(sum/(N-2));
}
NO.6
限幅平均滤波
1 方法
2 优缺点
3 代码
#define A 10
#define N 12
char value,i=0;
char value_buf[N];
char filter(){
char new_value,sum=0;
new_value=get_ad();
if(Abs(new_value-value)<A)
value_buf[i++]=new_value;
if(i==N) i=0;
for(count =0;count<N;count++)
sum+=value_buf[count];
return(char)(sum/N);
}
NO.7
一阶滞后滤波
1 方法
2 优缺点
3 代码
/*为加快程序处理速度,取a=0~100*/
#define a 30
char value;
char filter(){
char new_value;
new_value=get_ad();
return ((100-a)*value +a*new_value);
}
NO.8
加权递推平均滤波
1 方法
对递推平均滤波的改进,不同时刻的数据加以不同权重,通常越新的数据权重越大,这样灵敏度高,但平滑度低。
2 优缺点
适用有较大滞后时间常数和采样周期短的系统,对滞后时间常数小,采样周期长、变化慢的信号不能迅速反应其所受干扰。
3 代码
/* coe数组为加权系数表 */
#define N 12
char code coe[N]={1,2,3,4,5,6,7,8,9,10,11,12};
char code sum_coe={1+2+3+4+5+6+7+8+9+10+11+12};
char filter(){
char count;
char value_buf[N];
intsum=0;
for(count=0;count<N;count++) {
value_buf[count]=get_ad();
}
for(count=0;count<N;count++)
sum+=value_buf[count]*coe[count];
return(char)(sum/sum_coe);
}
NO.9
消抖滤波
1 方法
2 优缺点
3 代码
#define N 12
char filter(){
char count=0,new_value;
new_value=get_ad();
while(value!=new_value){
count++;
if(count>=N)
return new_value;
new_value=get_ad();
}
return value;
}
NO.10
限幅消抖滤波
1 方法
先限幅,后消抖。
2 优缺点
3 代码
#defineA 10
#defineN 12
char value;
char filter(){
char new_value,count=0;
new_value=get_ad();
while(value!=new_value) {
if(Abs(value-new_value)<A) {
count++;
if(count>=N)
return new_value;
new_value=get_ad();
}
return value;
}
}