androidfiltersensorspressure

Low Pass Filter for Android Pressure Sensor


Have tried to adapt the android developer documentation for accelerator LPF but it does not seem to work with pressure

    float pressure_value = 0.0f;
    float height = 0.0f;
    float height2 = (float) 964.98;
    final float alpha = (float) 0.8;


    if( Sensor.TYPE_PRESSURE == event.sensor.getType() ) {
        pressure_value = event.values[0];
        event.values[0] = alpha * event.values[0] + (1 - alpha) * event.values[0];
        event.values[1] = alpha * event.values[1] + (1 - alpha) * event.values[1];
        event.values[2] = alpha * event.values[2] + (1 - alpha) * event.values[2];

does anyone have some insight?


Solution

  • The pressure sensor only has one value i.e. event.values[0]. A simple low pass filter for that would look like:

    pressure = alpha*event.values[0] + (1 - alpha)*pressure
    

    Extremely oversimplified explanation:

    For alpha=0.8, the 'new' pressure value is 80% of the actual current pressure supplied by the sensor + 20% of the value of the 'old' pressure. Increasing the alpha value will make it more responsive to pressure fluctuations, lower alpha values will make it less noisy (more filtered).

    More explanatory code:

    private float alpha = 0.8f; 
    private float filteredPressure = 0.0f; 
    
    @Override
    public final void onSensorChanged(SensorEvent event) {
       if (Sensor.TYPE_PRESSURE == event.sensor.getType()) { 
          float currentPressure = event.values[0]; 
          filteredPressure = (alpha*currentPressure) + (1 - alpha)*filteredPressure; 
       }
    }