单片机ADC常用的十大滤波方法,学到就是赚到!
一、限幅滤波
1、方法
- 每次采新值时判断:若本次值与上次值之差<=A,则本次有效;若本次值与上次值之差>A,本次无效,用上次值代替本次。
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;
}
二、中位值滤波
1、方法
2、优缺点
- 克服波动干扰,对温度等变化缓慢的被测参数有良好的滤波效果,对速度等快速变化的参数不宜。
3、代码
#define N 11
char filter()
{
char value_buf[N];
char count,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];
}
三、算数平均滤波
1、方法
2、优缺点
3、代码
#define N 12
char filter()
{
int sum = 0;
for(count = 0;count<N;count++)
sum += get_ad();
return (char)(sum/N);
}
四、递推平均滤波
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;
}
五、中位值平均滤波
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));
}
六、限幅平均滤波
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);
}
七、一阶滞后滤波
1、方法
- 本次滤波结果=(1-a)* 本次采样 + a * 上次结果
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);
}
八、加权递推平均滤波
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];
int sum=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);
}
九、消抖滤波
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;
}
十、限幅消抖滤波
1、方法
2、优缺点
3、代码
#define A 10
#define N 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;
}
}