Search code examples
androidmathaccelerometersensorsandroid-sensors

Alpha value in removing gravity from acelerometer data


I came across this solution to remove gravity values from raw values of an acelerometer but I can't understand why alpha = 0.8 and not just 0.997.

 public void onSensorChanged(SensorEvent event)
 {
      // alpha is calculated as t / (t + dT)
      // with t, the low-pass filter's time-constant
      // and dT, the event delivery rate

      final float alpha = 0.8;

      gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
      gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
      gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

      linear_acceleration[0] = event.values[0] - gravity[0];
      linear_acceleration[1] = event.values[1] - gravity[1];
      linear_acceleration[2] = event.values[2] - gravity[2];
 }

Solution

  • As per the comment on your code: alpha = t / (t + dT), so alpha is just to be calculated according to your filter time lenght and the sampling rate of the accelerometer.

    if your solution sets alpha = 0.8 it simply means that dT = 0,025 t or, on the other side, t = 40dt.

    If you increase the sampling rate of the accelerometer, decreasing dT you'll have alpha pointing asyntotically to 1.