FileDocCategorySizeDatePackage
MovingAverage.javaAPI DocAzureus 3.0.3.41969Tue Mar 21 15:01:34 GMT 2006com.aelitis.azureus.core.util.average

MovingAverage

public class MovingAverage extends Object implements Average
Implements a basic moving average.

Fields Summary
private final int
periods
private double[]
data
private int
pos
Constructors Summary
public MovingAverage(int periods)
Create a new moving average.


   
            
      
      this.periods = periods;
      this.data = new double[periods];
      reset();
   
Methods Summary
private doublecalculateAve()

      double sum = 0.0;
      for (int i=0; i < periods; i++) {
         sum += data[i];
      }
      return sum / periods;
   
public doublegetAverage()
Return average-so-far.

 return calculateAve(); 
public voidreset()

   		pos = 0;
   		for (int i=0; i < periods; i++) { data[i] = 0.0; }
	
public doubleupdate(double newValue)
Update average and return average-so-far.

      data[pos] = newValue;
      pos++;
      if (pos == periods) pos = 0;
      return calculateAve();