收藏!單片機(jī)上常用的10個濾波算法

一、限幅濾波

1)方法


根據(jù)經(jīng)驗判斷兩次采樣允許的最大偏差值A(chǔ)。


每次采新值時判斷:

  • 若本次值與上次值之差<=A,則本次有效;

  • 若本次值與上次值之差>A,本次無效,用上次值代替本次。


2)優(yōu)缺點(diǎn)


克服脈沖干擾,無法抑制周期性干擾,平滑度差。


3)代碼


/* A值根據(jù)實(shí)際調(diào),Value有效值,new_Value當(dāng)前采樣值,程序返回有效的實(shí)際值 */#define A 10char Value;char filter(){  char new_Value;  new_Value = get_ad();    //獲取采樣值  if( abs(new_Value - Value) > A)  return Value;   //abs()取絕對值函數(shù)  return new_Value;}


二、位值濾波

1)方法


連續(xù)采樣N次,按大小排列;取中間值為本次有效值。


2)優(yōu)缺點(diǎn)


克服波動干擾,對溫度等變化緩慢的被測參數(shù)有良好的濾波效果,對速度等快速變化的參數(shù)不宜。


3)代碼


#define N 11char 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];}


三、算數(shù)平均濾波

1)方法


  • 連續(xù)采樣N次,取平均;

  • N較大時平滑度高,靈敏度低;

  • N較小時平滑度低,靈敏度高。

  • 一般N=12。


2)優(yōu)缺點(diǎn)


適用于存在隨機(jī)干擾的系統(tǒng),占用RAM多,速度慢。


3)代碼

#define N 12char filter(){  int sum = 0;  for(count = 0;count<N;count++)    sum += get_ad();  return (char)(sum/N);}


四、遞推平均濾波

1)方法


取N個采樣值形成隊列,先進(jìn)先出;取均值。

一般N=4~12。


2)優(yōu)缺點(diǎn)


對周期性干擾抑制性好,平滑度高;

適用于高頻振動系統(tǒng);

靈敏度低,RAM占用較大,脈沖干擾嚴(yán)重。


3)代碼


/* A值根據(jù)實(shí)際調(diào),Value有效值,new_Value當(dāng)前采樣值,程序返回有效的實(shí)際值 */#define A 10char Value;char filter(){  char new_Value;  new_Value = get_ad();        //獲取采樣值  if( abs(new_Value - Value) > A)     return Value;    //abs()取絕對值函數(shù)  return new_Value;}


五、中位值平均濾波

1)方法


  • 采樣N個值,去掉最大最小;

  • 計算N-2的平均值;

  • N=3~14。


2)優(yōu)缺點(diǎn)


融合了中位值、平均值的優(yōu)點(diǎn),消除脈沖干擾

計算速度慢,RAM占用大。


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)方法


每次采樣數(shù)據(jù)先限幅后送入隊列,取平均值。


2)優(yōu)缺點(diǎn)


融合限幅、均值、隊列的優(yōu)點(diǎn),消除脈沖干擾,占RAM較多。


3)代碼


#define A 10#define N 12char 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)方法


取a=0~1


本次濾波結(jié)果= (1-a) × 本次采樣 + a × 上次結(jié)果


2)優(yōu)缺點(diǎn)


良好一直周期性干擾,適用波動頻率較高場合。

靈敏度低,相位滯后。


3)代碼


/*為加快程序處理速度,取a=0~100*/#define a 30char value;char filter(){  char new_value;  new_value=get_ad();  return ((100-a)*value + a*new_value);}


八、加權(quán)遞推平均濾波

1)方法


對遞推平均濾波的改進(jìn),不同時刻的數(shù)據(jù)加以不同權(quán)重,通常越新的數(shù)據(jù)權(quán)重越大,這樣靈敏度高,但平滑度低。


2)優(yōu)缺點(diǎn)


適用有較大滯后時間常數(shù)和采樣周期短的系統(tǒng),對滯后時間常數(shù)小,采樣周期長、變化慢的信號不能迅速反應(yīng)其所受干擾。


3)代碼


/* coe數(shù)組為加權(quán)系數(shù)表 */#define N 12char 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)方法


設(shè)置一個濾波計數(shù)器,將采樣值與當(dāng)前有效值比較、

  • 若采樣值=當(dāng)前有效值,則計數(shù)器清0。

  • 若采樣值不等于當(dāng)前有效值,則計數(shù)器+1。

  • 若計數(shù)器溢出,則采樣值替換當(dāng)前有效值,計數(shù)器清0。


2)優(yōu)缺點(diǎn)


對變化慢的信號濾波效果好,變化快的不好。

避免臨界值附近的跳動,計數(shù)器溢出時若采到干擾值則無法濾波。


3)代碼


#define N 12char 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)優(yōu)缺點(diǎn)


融合了限幅、消抖的優(yōu)點(diǎn),避免引入干擾值,對快速變化的信號不宜。


3)代碼


#define A 10#define N 12char 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;  }}
*本文系網(wǎng)絡(luò)轉(zhuǎn)載,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系刪除
登錄后免費(fèi)查看全文
立即登錄
App下載
技術(shù)鄰APP
工程師必備
  • 項目客服
  • 培訓(xùn)客服
  • 平臺客服

TOP

1