[英]How should one calculate the time derivative of accelerometer data (jerk) in real-time (Java)?
I am trying to calculate the time-derivative of acceleration (jerk) from the streamed accelerometer data within the onReceive()
looping Android method.我正在尝试从
onReceive()
循环 Android 方法中的流式加速度计数据计算加速度 (jerk) 的时间导数。
I assumed that, from one sensor update to the next, I could approximate this by simply calculating the delta acceleration (x, y, z) and the associated delta time.我假设,从一个传感器更新到下一个,我可以通过简单地计算增量加速度 (x, y, z) 和相关的增量时间来近似计算。 To ensure maximal accuracy, I used the
System.nanoTime()
method (and divided by 10e8).为了确保最大的准确性,我使用了
System.nanoTime()
方法(并除以 10e8)。
Everything seemed happy, and jerk data was appearing, but I thought it wise to check that the sum of all delta_time
s ( sumDeltaTime
) was close to the difference between last_time
and first_time
.一切似乎很高兴,挺举数据出现,但我认为明智的做法是检查所有的总和
delta_time
S( sumDeltaTime
)很接近的区别last_time
和first_time
。 To my surprise, the difference was several thousand times out.令我惊讶的是,差异是几千倍。 Even replacing
System.nanoTime()
with System.currentTimeMillis()
(divided by 10e2) did not change this discrepency.即使用
System.currentTimeMillis()
(除以 10e2)替换System.nanoTime()
也没有改变这种差异。 Here is my code:这是我的代码:
// calculate jerk (time derivative of acceleration)
accel_count++;
if (accel_count == 1) {
first_time = new_time = System.nanoTime() / 10e8; // captures first time value (in seconds)
newAccel[0] = accel[0]; // x
newAccel[1] = accel[1]; // y
newAccel[2] = accel[2]; // z
} else {
prev_time = new_time; // assigns previous time value
new_time = System.nanoTime() / 10e8; // immediately updates to the new time value (in seconds)
prevAccel[0] = newAccel[0]; // x
prevAccel[1] = newAccel[1]; // y
prevAccel[2] = newAccel[2]; // z
// set up for next iteration
newAccel[0] = accel[0]; // x
newAccel[1] = accel[1]; // y
newAccel[2] = accel[2]; // z
}
float[] delta_accel; // difference in acceleration between consecutive sensor measurements
delta_accel = new float[] {
(newAccel[0] - prevAccel[0]), // x
(newAccel[1] - prevAccel[1]), // y
(newAccel[2] - prevAccel[2]) // z
};
double delta_time = (new_time - prev_time); // time difference between consecutive sensor measurements (in seconds)
float[] jerk;
jerk = new float[] {
(float) (delta_accel[0] / delta_time), // x
(float) (delta_accel[1] / delta_time), // y
(float) (delta_accel[2] / delta_time) // z
};
total_time = new_time - first_time; // total time duration of entire recording (in seconds)
sumDeltaTime += delta_time; // testing sum of deltas
Can anybody see what I must be doing wrong?谁能看到我一定做错了什么? Thanks!
谢谢!
You did not initialize prev_time
on the first pass ( accel_count == 1
), so it was probably 0 the first time you calculated delta_time
.您没有在第一遍(
accel_count == 1
)时初始化prev_time
,因此您第一次计算delta_time
时它可能为 0。 This made the first delta_time
unusually large, because new_time
is much larger than 0, just as System.nanoTime()
is much larger than 0.这使得第一个
delta_time
异常大,因为new_time
远大于 0,就像System.nanoTime()
远大于 0 一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.