I want to calculate a moving average over a time window:
//object init
MovingAverageTimeBased movTime = new MovingAverageTimeBased(8000f);
//this gets called every Frame (around 180 FPS -- but varies of course)
movingAvg = movTime.next(value, System.currentTimeMillis());
And this is the class:
public class MovingAverageTimeBased {
float windowsize;
Queue<Float> queue;
Queue<Float> timestampQueue;
float sum;
float lastItemDeleted;
public MovingAverageTimeBased(float windowsize) {
this.windowsize = windowsize;
this.queue = new LinkedList<Float>();
this.timestampQueue = new LinkedList<Float>();
this.sum = 0;
}
public double next(float n, float timestamp) {
if(queue.size() > 1 && timestampQueue.size() > 1) {
System.out.println("Timestamp diff- " + (timestamp - (float) timestampQueue.peek()));
while (timestamp - (float) timestampQueue.peek() > windowsize) {
System.out.println("Unqueue item");
sum = sum - (float) queue.poll();
timestampQueue.poll();
}
}
queue.offer(n);
timestampQueue.offer(timestamp);
sum = sum + n;
return (float) sum / queue.size();
}
}
My error is that, that entries never seem to get deleted as the difference of the timestamps are always 0?
What could be the mistake?