Class MovingAverage


  • public class MovingAverage
    extends java.lang.Object
    Calculates an exponentially weighted moving average for a series of data.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double DEFAULT_WEIGHT  
    • Constructor Summary

      Constructors 
      Constructor Description
      MovingAverage​(double initialSample)
      Construct an object to track the exponentially weighted moving average for a series of data.
      MovingAverage​(double initialSample, double weight)
      Construct an object to track the exponentially weighted moving average for a series of data.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addSample​(double sample)
      Add a sample and calculate a new average.
      double getAverage()
      Returns the current average for all samples.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MovingAverage

        public MovingAverage​(double initialSample)
        Construct an object to track the exponentially weighted moving average for a series of data. The weight is set to a default of 0.9, which is good for data with lots of samples when the average should be resistant to spikes (i.e. frame rate).

        The weight is a ratio between 0 and 1 that represents how much of the previous average is kept compared to the new sample. With a weight of 0.9, 90% of the previous average is kept and 10% of the new sample is added to the average.

        Parameters:
        initialSample - the first sample in the average
      • MovingAverage

        public MovingAverage​(double initialSample,
                             double weight)
        Construct an object to track the exponentially weighted moving average for a series of data.

        The weight is a ratio between 0 and 1 that represents how much of the previous average is kept compared to the new sample. With a weight of 0.9, 90% of the previous average is kept and 10% of the new sample is added to the average.

        Parameters:
        initialSample - the first sample in the average
        weight - the weight to used when adding samples
    • Method Detail

      • addSample

        public void addSample​(double sample)
        Add a sample and calculate a new average.
      • getAverage

        public double getAverage()
        Returns the current average for all samples.