Я пытаюсь вычислить производную по времени ускорения (рывка) из потоковых данных акселерометра в циклическом методе onReceive () Android.
Я предположил, что от одного обновления датчика к следующему Я мог бы аппроксимировать это, просто посчитав дельта-ускорение (x, y, z) и ассоциированное время дельты. Чтобы обеспечить максимальную точность, я использовал метод System.nanoTime()
(деленный на 10e8).
Все выглядело счастливым, и появлялись данные о рывках, но я подумал, что стоит проверить, что сумма всех delta_time
s (sumDeltaTime
) было близко к разнице между last_time
и first_time
. К моему удивлению, разница была в несколько тысяч раз больше. Даже замена System.nanoTime()
на System.currentTimeMillis()
(деленная на 10e2) не изменила эту дискретность. Вот мой код:
// 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
Кто-нибудь может увидеть, что я должен делать неправильно? Спасибо!