BowlerKernel
RollingAverageFilter.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.util;
2 
3 // TODO: Auto-generated Javadoc
7 public class RollingAverageFilter {
8 
10  double [] data;
11 
13  int index = 0;
14 
16  double average = 0;
17 
24  public RollingAverageFilter(int size, double startingValue){
25  data = new double[size];
26  average = startingValue*size;
27  for(int i=0;i<size;i++){
28  data[i]=startingValue;
29  }
30  }
31 
38  public double add(double value){
39  average +=value;
40  average -=data[index];
41  data[index]=value;
42  index++;
43  if(index==data.length){
44  index=0;
45  }
46 
47  return getValue();
48  }
49 
55  public double getValue(){
56  return average/data.length;
57  }
58 
59 }