我正在创建一个 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)) );