在傳感器使用中,我們常常需要對傳感器數(shù)據(jù)進行各種整理,讓應(yīng)用獲得更好的效果,以下介紹幾種常用的簡單處理方法:
加權(quán)平滑:平滑和均衡傳感器數(shù)據(jù),減小偶然數(shù)據(jù)突變的影響。
抽取突變:去除靜態(tài)和緩慢變化的數(shù)據(jù)背景,強調(diào)瞬間變化。
簡單移動平均線:保留數(shù)據(jù)流最近的K個數(shù)據(jù),取平均值。
下面,具體介紹一下這3種處理方法。
1、加權(quán)平滑
使用算法如下:
(新值) = (舊值)*(1 - a) + X * a其中a為設(shè)置的權(quán)值,X為最新數(shù)據(jù),程序?qū)崿F(xiàn)如下:
float ALPHA = 0.1f; public void onSensorChanged(SensorEvent event){ x = event.values[0]; y = event.values[1]; z = event.values[2]; mLowPassX = lowPass(x,mLowPassX); mLowPassY = lowPass(x,mLowPassY); mLowPassZ = lowPass(x,mLowPassZ); } private float lowPass(float current,float last){ return last * (1.0f - ALPHA) + current * ALPHA; }
2、抽取突變
此算法采用上面加權(quán)平滑的逆算法,實現(xiàn)代碼如下:
public void onSensorChanged(SensorEvent event){ final float ALPHA = 0.8;gravity[0] = ALPHA * gravity[0] + (1-ALPHA) * event.values[0]; gravity[1] = ALPHA * gravity[1] + (1-ALPHA) * event.values[1]; gravity[2] = ALPHA * gravity[2] + (1-ALPHA) * event.values[2];filteredValues[0] = event.values[0] - gravity[0]; filteredValues[1] = event.values[1] - gravity[1]; filteredValues[2] = event.values[2] - gravity[2]; }
3、簡單移動平均線
這個算法,保留傳感器數(shù)據(jù)流中最近的K個數(shù)據(jù),返回它們的平均值。k表示平均“窗口”的大小,實現(xiàn)代碼如下:
public class MovingAverage{ private float circularBuffer[]; //保存?zhèn)鞲衅髯罱腒個數(shù)據(jù) private float avg; //返回到傳感器平均值 private float sum; //數(shù)值中傳感器數(shù)據(jù)的和 private float circularIndex; //傳感器數(shù)據(jù)數(shù)組節(jié)點位置 private int count;public MovingAverage(int k){ circularBuffer = new float[k]; count= 0; circularIndex = 0; avg = 0; sum = 0; } public float getValue(){ return arg; } public long getCount(){ return count; } private void primeBuffer(float val){ for(int i=0;i= circularBuffer.length){ return 0; } return curIndex + 1; } public void pushValue(float x){ if(0 == count++){ primeBuffer(x); } float lastValue = circularBuffer[circularIndex]; circularBuffer[circularIndex] = x; //更新窗口中傳感器數(shù)據(jù) sum -= lastValue; //更新窗口中傳感器數(shù)據(jù)和 sum += x; avg = sum / circularBuffer.length; //計算得傳感器平均值 circularIndex = nextIndex(circularIndex); } };++i){>
來源:STM32嵌入式開發(fā)
免責(zé)聲明:本文為轉(zhuǎn)載文章,轉(zhuǎn)載此文目的在于傳遞更多信息,版權(quán)歸原作者所有。本文所用視頻、圖片、文字如涉及作品版權(quán)問題,請聯(lián)系小編進行處理
-
傳感器
+關(guān)注
關(guān)注
2557文章
51757瀏覽量
758929 -
單片機
+關(guān)注
關(guān)注
6050文章
44705瀏覽量
641310 -
數(shù)據(jù)處理
+關(guān)注
關(guān)注
0文章
623瀏覽量
28737
發(fā)布評論請先 登錄
相關(guān)推薦
4~20mA傳感器數(shù)據(jù)處理新途徑
電子稱重顯示器的數(shù)據(jù)處理方法
如何去使用數(shù)字信號處理算法
FCS中的智能傳感器的數(shù)據(jù)處理方法
基于單片機的氣敏傳感器測試系統(tǒng)

智能儀器的數(shù)據(jù)處理算法

基于部分存儲和選擇性加載的數(shù)據(jù)處理算法

使用PIC單片機開發(fā)的被動紅外傳感器模塊的報警器

淺析單片機開發(fā)中傳感器的數(shù)據(jù)處理算法

紅外雨量計(光學(xué)雨量傳感器)不同雨量場景如何優(yōu)化數(shù)據(jù)處理算法

八位單片機的大數(shù)處理算法及在數(shù)控中的應(yīng)用

評論