單片機(jī)軟件濾波方法
假定從8位AD中讀取數(shù)據(jù)(如果是更高位的AD可定義數(shù)據(jù)類型為int), AD采的返回給 get_ad();函數(shù).
本文引用地址:http://m.butianyuan.cn/article/201612/324119.htm1、限副濾波
/* A值可根據(jù)實(shí)際情況調(diào)整
value為有效值,new_value為當(dāng)前采樣值
濾波程序返回有效的實(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;
}
2、中位值濾波法
/* N值可根據(jù)實(shí)際情況調(diào)整
排序采用冒泡法*/
#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];
}
3、算術(shù)平均濾波法
#define N 12
char filter()
{
int sum = 0;
for ( count=0;count
sum + = get_ad();
delay();
}
return (char)(sum/N);
}
4、遞推平均濾波法(又稱滑動平均濾波法)
#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);
}
5、中位值平均濾波法(又稱防脈沖干擾平均濾波法)
#define N 12
char filter()
{
char count,i,j;
char value_buf[N];
int sum=0;
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
return (char)(sum/(N-2));
}
6、限幅平均濾波法
/*
*/
略 參考子程序1、3
7、一階滯后濾波法
/* 為加快程序處理速度假定基數(shù)為100,a=0~100 */
#define a 50
char value;
char filter()
{
char new_value;
new_value = get_ad();
return (100-a)*value + a*new_value;
}
8、加權(quán)遞推平均濾波法
/* coe數(shù)組為加權(quán)系數(shù)表,存在程序存儲區(qū)。*/
#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
value_buf[count] = get_ad();
delay();
}
for (count=0,count
return (char)(sum/sum_coe);
}
9、消抖濾波法
#define N 12
char filter()
{
char count=0;
char new_value;
new_value = get_ad();
while (value !=new_value);
{
count++;
if (count>=N) return new_value;
delay();
new_value = get_ad();
}
return value;
}
10、限幅消抖濾波法
/*
*/
評論