Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Apply advanced filtering issue 243
Browse files Browse the repository at this point in the history
  • Loading branch information
rohandhiman03 committed Nov 26, 2023
1 parent 69b9db4 commit c6305f8
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
import de.dennisguse.opentracks.sensors.AltitudeSumManager;

import java.util.LinkedList;
import java.util.Queue;



public class BarometerInternal {

private static final String TAG = BarometerInternal.class.getSimpleName();
Expand All @@ -21,6 +26,8 @@ public class BarometerInternal {

private AltitudeSumManager observer;

private static final int FILTER_WINDOW_SIZE = 5; // Window size for the moving average filter
private Queue<Float> pressureReadings = new LinkedList<>();
private final SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Expand All @@ -29,7 +36,21 @@ public void onSensorChanged(SensorEvent event) {
return;
}

observer.onSensorValueChanged(AtmosphericPressure.ofHPA(event.values[0]));
// Add new reading to the queue
if (pressureReadings.size() >= FILTER_WINDOW_SIZE) {
pressureReadings.poll(); // Remove the oldest reading
}
pressureReadings.offer(event.values[0]);

// Calculate the average of the readings in the queue
float sum = 0;
for (Float reading : pressureReadings) {
sum += reading;
}
float average = sum / pressureReadings.size();

// Use the averaged value instead of the raw reading
observer.onSensorValueChanged(AtmosphericPressure.ofHPA(average));
}

@Override
Expand Down

0 comments on commit c6305f8

Please sign in to comment.