NO.1
限幅濾波?
1 方法
根據(jù)經(jīng)驗(yàn)判斷兩次采樣允許的最大偏差值A(chǔ)
每次采新值時(shí)判斷:若本次值與上次值之差<=A,則本次有效;若本次值與上次值之差>A,本次無(wú)效,用上次值代替本次。
2 優(yōu)缺點(diǎn)
克服脈沖干擾,無(wú)法抑制周期性干擾,平滑度差。
3 代碼
/* 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( abs(new_Value - Value) > A) return Value; //abs()取絕對(duì)值函數(shù) return new_Value; }
NO.2
中位值濾波
1 方法
連續(xù)采樣N次,按大小排列
取中間值為本次有效值
2 優(yōu)缺點(diǎn)
克服波動(dòng)干擾,對(duì)溫度等變化緩慢的被測(cè)參數(shù)有良好的濾波效果,對(duì)速度等快速變化的參數(shù)不宜。
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
算數(shù)平均濾波
1 方法
連續(xù)采樣N次,取平均
N較大時(shí)平滑度高,靈敏度低
N較小時(shí)平滑度低,靈敏度高
一般N=12
2 優(yōu)缺點(diǎn)
適用于存在隨機(jī)干擾的系統(tǒng),占用RAM多,速度慢。
3 代碼
#define N 12 charfilter(){ int sum =0; for(count =0;count
NO.4
遞推平均濾波
1 方法
取N個(gè)采樣值形成隊(duì)列,先進(jìn)先出
取均值
一般N=4~12
2 優(yōu)缺點(diǎn)
對(duì)周期性干擾抑制性好,平滑度高
適用于高頻振動(dòng)系統(tǒng)
靈敏度低,RAM占用較大,脈沖干擾嚴(yán)重
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 方法
采樣N個(gè)值,去掉最大最小
計(jì)算N-2的平均值
N= 3~14
2 優(yōu)缺點(diǎn)
融合了中位值,平均值的優(yōu)點(diǎn)
消除脈沖干擾
計(jì)算速度慢,RAM占用大
3 代碼
char filter(){ char count,i,j; char Value_buf[N]; int sum=0; for(count=0;countValue_buf[i+1]) { temp= Value_buf[i]; Value_buf[i]= Value_buf[i+1]; Value_buf[i+1]=temp; } for(count =1;count
NO.6
限幅平均濾波
1 方法
每次采樣數(shù)據(jù)先限幅后送入隊(duì)列
取平均值
2 優(yōu)缺點(diǎn)
融合限幅、均值、隊(duì)列的優(yōu)點(diǎn)
消除脈沖干擾,占RAM較多
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)
NO.7
一階滯后濾波
1 方法
取a=0~1
本次濾波結(jié)果=(1-a)* 本次采樣 + a * 上次結(jié)果
2 優(yōu)缺點(diǎn)
良好一直周期性干擾,適用波動(dòng)頻率較高場(chǎng)合
靈敏度低,相位滯后
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
加權(quán)遞推平均濾波
1 方法
對(duì)遞推平均濾波的改進(jìn),不同時(shí)刻的數(shù)據(jù)加以不同權(quán)重,通常越新的數(shù)據(jù)權(quán)重越大,這樣靈敏度高,但平滑度低。
2 優(yōu)缺點(diǎn)
適用有較大滯后時(shí)間常數(shù)和采樣周期短的系統(tǒng),對(duì)滯后時(shí)間常數(shù)小,采樣周期長(zhǎng)、變化慢的信號(hào)不能迅速反應(yīng)其所受干擾。
3 代碼
/* coe數(shù)組為加權(quán)系數(shù)表 */ #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
NO.9
消抖濾波
1 方法
設(shè)置一個(gè)濾波計(jì)數(shù)器
將采樣值與當(dāng)前有效值比較
若采樣值=當(dāng)前有效值,則計(jì)數(shù)器清0
若采樣值不等于當(dāng)前有效值,則計(jì)數(shù)器+1
若計(jì)數(shù)器溢出,則采樣值替換當(dāng)前有效值,計(jì)數(shù)器清0
2 優(yōu)缺點(diǎn)
對(duì)變化慢的信號(hào)濾波效果好,變化快的不好
避免臨界值附近的跳動(dòng),計(jì)數(shù)器溢出時(shí)若采到干擾值則無(wú)法濾波
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 優(yōu)缺點(diǎn)
融合了限幅、消抖的優(yōu)點(diǎn)
避免引入干擾值,對(duì)快速變化的信號(hào)不宜
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)=N) return new_value; new_value=get_ad(); } return value; } }
審核編輯:湯梓紅
-
adc
+關(guān)注
關(guān)注
99文章
6709瀏覽量
549224 -
代碼
+關(guān)注
關(guān)注
30文章
4900瀏覽量
70739 -
濾波算法
+關(guān)注
關(guān)注
2文章
90瀏覽量
13971
原文標(biāo)題:【補(bǔ)】ADC數(shù)據(jù)采集波動(dòng)大,那是你還不知道這些濾波算法
文章出處:【微信號(hào):chuxue_MCU,微信公眾號(hào):?jiǎn)纹瑱C(jī)技術(shù)宅】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論