![](/img/trans.png)
[英]How should I handle getting real-time data, storing it in a Java type, and plotting a real-time graph?
[英]How should one calculate the time derivative of accelerometer data (jerk) in real-time (Java)?
我正在嘗試從onReceive()
循環 Android 方法中的流式加速度計數據計算加速度 (jerk) 的時間導數。
我假設,從一個傳感器更新到下一個,我可以通過簡單地計算增量加速度 (x, y, z) 和相關的增量時間來近似計算。 為了確保最大的准確性,我使用了System.nanoTime()
方法(並除以 10e8)。
一切似乎很高興,挺舉數據出現,但我認為明智的做法是檢查所有的總和delta_time
S( sumDeltaTime
)很接近的區別last_time
和first_time
。 令我驚訝的是,差異是幾千倍。 即使用System.currentTimeMillis()
(除以 10e2)替換System.nanoTime()
也沒有改變這種差異。 這是我的代碼:
// 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
誰能看到我一定做錯了什么? 謝謝!
您沒有在第一遍( accel_count == 1
)時初始化prev_time
,因此您第一次計算delta_time
時它可能為 0。 這使得第一個delta_time
異常大,因為new_time
遠大於 0,就像System.nanoTime()
遠大於 0 一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.