登录    注册      
    

News Message

传感器电压数据常用算法



传感器电压数据常用算法



传感器使用中,我们常常需要对传感器数据进行各种整理,让应用获得更好的效果,以下介绍几种常用的简单处理方法:

1.加权平滑:平滑和均衡传感器数据,减小偶然数据突变的影响;
2.抽取突变:去除静态和缓慢变化的数据背景,强调瞬间变化;
3.简单移动平均线:保留数据流最近的K个数据,取平均值;

01

加权平滑


使用算法如下:
(新值) = (旧值)*(1 - a) + X * a其中a为设置的权值,X为最新数据,程序实现如下:
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;
}

02

抽取突变采用上面加权平滑的逆算法


实现代码如下:

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];
}


03

简单移动平均线


保留传感器数据流中最近的K个数据,返回它们的平均值。k表示平均“窗口”的大小;

实现代码如下:

public class MovingAverage{
private float circularBuffer[]; //保存传感器最近的K个数据
private float avg; //返回到传感器平均值
private float sum; //数值中传感器数据的和
private float circularIndex; //传感器数据数组节点位置
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;++i){
circularBuffer[i] = val;
sum += val;
}
}
private int nextIndex(int curIndex){
if(curIndex + 1 >= circularBuffer.length){
return 0;
}
return curIndex + 1;
}
public void pushValue(float x){
if(0 == count++){
primeBuffer(x);
}
float lastValue = circularBuffer[circularIndex];
circularBuffer[circularIndex] = x; //更新窗口中传感器数据
sum -= lastValue; //更新窗口中传感器数据和
sum += x;
avg = sum / circularBuffer.length; //计算得传感器平均值
circularIndex = nextIndex(circularIndex);
}
}



Share Http URL:  http://www.wittx.cn/get_news_message.do?new_id=934



请输入评论





























Best Last Month

人性的弱点

人性的弱点

Information industry

by wittx


Metamaterial Thermoelectric Conversion

Metamaterial Thermoelectric Conversion

Information industry

by wittx


Introduction to Probability

Introduction to Probability

Information industry

by wittx


上交所受理京东数科科创板上市申请,拟募资 200 亿元



全球及中国智能驾驶行业研究报告

全球及中国智能驾驶行业研究报告

Information industry

by wittx


Reinfocement Learning

Reinfocement Learning

Mechanical electromechanical

by wittx


Robot Attitude Control Algorithm Based on Complementary



上市公司突破4000家,30年市值暴增3.4万倍!A股正扩容加速



黄金涨逾30美元

黄金涨逾30美元

Information industry

by wittx


传感器的原理和应用

传感器的原理和应用

Information industry

by wittx