namespace KitsuneCafe.Sys
{
  public class ExponentialMovingAverage
  {
    private readonly float alpha;
    private float previousAverage = float.NaN;

    public ExponentialMovingAverage(int period)
    {
      alpha = 2f / (period + 1);
    }

    public float Add(float value)
    {
      if (float.IsNaN(previousAverage))
      {
        return value;
      }
      return (value - previousAverage) * alpha + previousAverage;
    }
  }
}
