采样干扰十大滤波算法程序大全
首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责
1、幅限滤波法
***************************************************
*数函称名:AmplitudeLimiterFilter()-幅限滤波法
*点优:能有效战胜因偶尔素因起引的脉冲干扰
*点缺:没法制抑那种周期性的干扰,且平滑度差
*说明:
1、调用数函
GetAD(),该数函用来获得前当值
2、变量说明
Value:近来一次有效采样的值,该变量为全局变量
NewValue:前当采样的值
ReturnValue:返回值
3、常量说明
A:两次采样的大最误差值,该值要需使用者根据实际情况设置
*进口:Value,上一次有效的采样值,在主程序里赋值
*口出:ReturnValue,返回值,本次滤波结果
****************************************************/
#define A 10
unsigned char Value
unsigned char AmplitudeLimiterFilter()
{
unsigned char NewValue;
unsigned char ReturnValue;
NewValue=GatAD();
if(((NewValue-Value)>A))||((Value-NewValue)>A)))
ReturnValue=Value;
else ReturnValue=NewValue;
return(ReturnValue);
}
2、中位值滤波法
#define N 11
unsigned char MiddlevalueFilter()
{
unsigned char value_buf[N];
unsigned char i,j,k,temp;
for(i=0;i
{
value_buf[i] = get_ad();
delay();
}
for (j=0;j
{
for (k=0;k
{
if(value_buf[k]>value_buf[k+1])
{
temp = value_buf[k];
value_buf[k] = value_buf[k+1];
value_buf[k+1] = temp;
}
}
}
return value_buf[(N-1)/2];
}
3、算术均匀滤波法
#define N 12
char filter()
{
unsigned int sum = 0;
unsigned char i;
for (i=0;i
{
sum + = get_ad();
delay();
}
return(char)(sum/N);
}
4、递推均匀滤波法(称又滑动均匀滤波法)
#define N 12
unsigned char value_buf[N];
unsigned char filter()
{
unsigned char i;
unsigned char value;
int sum=0;
value_buf[i++] = get_ad(); //采集到的数据放入最高位
for(i=0;i
{
value_buf[i]=value_buf[i+1]; //全部数据左移,低位掉扔
sum += value_buf[i];
}
value = sum/N;
return(value);
}
5、中位值均匀滤波法(称又防脉冲干扰均匀滤波法)
#define N 12
uchar filter()
{
unsigned char i,j,k,l;
unsigned char temp,sum=0,value;
unsigned char value_buf[N],;
for(i=0;i
{
value_buf[i] = 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(i=1;i
sum += value_buf[i];
value = sum/(N-2);
return(value);
}
6、递推中位值滤波法
char filter(char new_data,char queue[],char n)
{
char max,min;
char sum;
char i;
queue[0]=new_data;
max=queue[0];
min=queue[0];
sum=queue[0];
for(i=n-1;i>0;i--)
{
if(queue[i]>max)
max=queue[i];
else if (queue[i]
min=queue[i];
sum=sum+queue[i];
queue[i]=queue[i-1];
}
i=n-2;
sum=sum-max-min+i/2; //说明:+i/2的的目是为了四舍五入
sum=sum/i;
return(sum);
}
7、幅限均匀滤波法
#define A 10
#define N 12
unsigned char data[];
unsigned char filter(data[])
{
unsigned char i;
unsigned char value,sum;
data[N]=GetAD();
if(((data[N]-data[N-1])>A||((data[N-1]-data[N])>A))
data[N]=data[N-1];
//else data[N]=NewValue;
for(i=0;i
{
data[i]=data[i+1];
sum+=data[i];
}
value=sum/N;
return(value);
}
8、一阶滞后滤波法
#define Thre_value 10
#define N 50
float Or_data[N];
unsigned char Dr0_flag=0,Dr1_flag=0;
void abs(float first,float second)
{
float abs;
if(first>second)
{
abs=first-second;
Dr1_flag=0;
}
else
{
abs=second-first;
Dr1_flag=1;
}
return(abs);
}
void filter(void)
{
uchar i=0,F_count=0,coeff=0;
float Abs=0.00;
//肯定一阶滤波系数
for(i=1;i
{
Abs=abs(Or_data[i-1],Or_data[i]);
if(!(Dr1_flag^Dr0_flag)) //前后数据变更方向致一
{
F_count++;
if(Abs>=Thre_value)
{
F_count++;
F_count++;
}
if(F_count>=12)
F_count=12;
coeff=20*F_count;
}
else //去动抖
coeff=5;
//一阶滤波算法
if(Dr1_flag==0) //前当值小于前一个值
Or_data[i]=Or_data[i-1]-coeff*(Or_data[i-1]-Or_data[i])/256;
else
Or_data[i]=Or_data[i-1]+coeff*(Or_data[i]-Or_data[i-1])/256;
F_count=0; //滤波计数器清零
Dr0_flag=Dr1_flag;
}
}
9、加权递推均匀滤波法
#define N 12
const char code coe[N] = {1,2,3,4,5,6,7,8,9,10,11,12};
const char code sum_coe = 1+2+3+4+5+6+7+8+9+10+11+12;
unsigned char filter()
{
unsigned char i;
unsigned char value_buf[N];
int sum=0;
for (i=0;i
{
value_buf[i] = get_ad();
delay();
}
for (i=0,i
{
value_buf[i]=value_buf[i+1];
sum += value_buf[i]*coe[i];
}
sum/=sum_coe;
value=sum/N;
return(value);
}
10、消抖滤波法
#define N 12
unsigned char filter()
{
unsigned char i=0;
unsigned char new_value;
new_value = get_ad();
if(value !=new_value);
{
i++;
if (i>N)
{
i=0;
value=new_value;
}
}
else i=0;
return(value);
}
文章结束给大家分享下程序员的一些笑话语录:
乔布斯:怎么样还是咱安全吧!黑客:你的浏览器支持国内网银吗?苹果可以玩国内的网游吗乔布斯:......不可以黑客:那我研究你的漏洞干嘛,我也需要买奶粉!