Trim collection used to calculate distance

I have a data collection where in each record there is information about the vehicle speed and the time when that record was made. The time difference between each recording is different and is about 300ms. I am using this collection to calculate the distance traveled. I would like to truncate this collection without losing too much precision. Is there some algorithm for this?

I tried to write a simple algorithm that removed the entry if the entry is no different from the previous one by at least X%. This algorithm does not work correctly. For example, when I used an algorithm with 3% and 4% cropping, the number of records was the same, but the calculated distance differed by 40m at a distance of 500m, where 4% was more accurate.

EDIT: Changes to Benjamin's algorithm:

I have a time when the record was collected, so I need 3 records.

double timeDifference = (next.time - curr.time) / (curr.time - previous.time); previuos.value = (previous.value + curr.value * timeDifference) / 2

Also I don't want the result to be double but long, so I need to use rounding which will change the distance. Also one minute is about 300 entries, so I hope the rounding won't affect the calculations.

+3


source to share


2 answers


You have a list of value pairs, i.e .:

    time | speed
1:  0    | 0
2:  300  | 10
3:  600  | 40
4:  900  | 48
5:  1200 | 50
6:  1500 | 30
7:  1800 | 0

      

You always need to look at the two values i

and i-1

. We start with i=1

, because otherwise we'll be accessing the index -1

:

for(int i=1; i<list.size(); i++) {
  if(Math.abs(list.get(i).speed - list.get(i-1).speed) < 3) {
    list.get(i-1).time = (list.get(i).time + list.get(i-1).time) / 2;
    list.get(i-1).speed = (list.get(i).speed + list.get(i-1).speed) / 2;
    list.remove(i);
  }
}

      

In words: If the difference in speed is between i

and i-1

less than 3, update the record i-1

and set its time and speed to average i

and i-1

. Then we remove the item i

from the list.



What happens to the example data: This code removes the record 5:

from the example data because abs(50-48) = 2

and 2 < 3

, and set the record 4:

to time = 1150

andspeed = 49

In the above example, of course, there are at least two problems (which are easy to fix - for example, homework): 1. If there is only one entry in the list, the code throws an exception. 2. After deleting the item, it changes list.size()

, but the above code is not recognized. This means: after deleting any record, it will throw an exception.

I haven't tested it, just to show you how you can clear the list.

You can additionally add code that prevents the last entry from being removed from the list, because otherwise you probably won't have an exact last time value. It is also possible to run this many times on the same data until list.size()

it remains constant.

(see comments below)

+2


source


If you don't need to save history, you can simply update the distance traveled when new data is received. I don't know how you do calculus, I am writing the first example I can think of.



Measurement record = null;
double distance = 0;
long totalTime = 0;

// New record handler
public void recordListener(Measurement newRecord) {
    if(record != null) {
        double avgSpeed = (record.speed + newRecord.speed) / 2.0;
        long timeDelta = newRecord.time - record.time;
        distance += avgSpeed * (timeDelta / 1000)   // millis to seconds
        totalTime += timeDelta;
    }
    record = newRecord;
}

      

0


source







All Articles