我正在创建一个android phonegap应用程序,它使用加速度计来检测道路上的瑕疵。但是,当我使用原始加速度计值(x,y和z)时,它们从一个尝试到另一个尝试变化太大。我尝试使用相同的设备,相同的车辆和位置来获取相同的缺陷,但加速度计给出的值完全不同。我每0.25秒获得一次加速计值,所以每秒钟我会得到4个x,y和z值。
有人能告诉我如何平滑这些值以获得更平滑,更可靠的价值吗?我将需要使用这些值对它们进行进一步处理,因此x,y和z值的准确性至关重要。是否有任何公式可以应用于值以获得更平滑的输出?
更新: 添加我的代码片段:
function acc() {
var accOpt = { frequency: 1000 };
previousReading = {
x : null,
y : null,
z : null,
lr : null,
fb : null
};
watchID = navigator.accelerometer.watchAcceleration(win, fail, accOpt);
}
function win (acceleration) {
var tiltLR = 0;
var tiltFB = 0;
var facingUp = -1;
if (acceleration.z > 0) {
facingUp = +1;
}
tiltLR = Math.round(((acceleration.x) / 9.81) * -90);
tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp);
var changes = {};
if (previousReading.x !== null) {
changes.x = Math.abs(previousReading.x-acceleration.x);
changes.y = Math.abs(previousReading.y-acceleration.y);
changes.z = Math.abs(previousReading.z-acceleration.z);
changes.lr = Math.abs(previousReading.lr-tiltLR);
changes.fb = Math.abs(previousReading.fb-tiltFB);
}
previousReading = {
x: acceleration.x,
y: acceleration.y,
z: acceleration.z,
lr: tiltLR,
fb: tiltFB
};
console.log('previousReading x: ' + previousReading.x + ' previousReading y: ' + previousReading.y + ' previousReading z: ' + previousReading.z+ ' previousReading lr: ' + tiltLR + ' previousReading fb: ' + tiltFB);
console.log('changes x: ' + changes.x + ' changes y: ' + changes.y + ' changes z: ' + changes.z + ' changes lr: ' + changes.lr + ' changes fb: ' + changes.fb);
console.log(acceleration.x + ' ' + acceleration.y + ' ' + acceleration.z);
}
过滤器: 我遇到过这段代码,可以平滑x,y和z值。如果我将它应用于当前的x,y,z值,然后将它们与之前的值(也被过滤掉)进行比较,那么这在我的情况下会起作用吗?
#define kFilteringFactor 0.1
// Use a basic low-pass filter to keep only the gravity component of each axis.
grav_accelX = (acceleration.x * kFilteringFactor) + ( grav_accelX * (1.0 - kFilteringFactor));
grav_accelY = (acceleration.y * kFilteringFactor) + ( grav_accelY * (1.0 - kFilteringFactor));
grav_accelZ = (acceleration.z * kFilteringFactor) + ( grav_accelZ * (1.0 - kFilteringFactor));
// Subtract the low-pass value from the current value to get a simplified high-pass filter
instant_accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (instant_accelX * (1.0 - kFilteringFactor)) );
instant_accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (instant_accelY * (1.0 - kFilteringFactor)) );
instant_accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (instant_accelZ * (1.0 - kFilteringFactor)) );