33 lines
666 B
C#
33 lines
666 B
C#
using System;
|
|
|
|
namespace KitsuneCafe.Event
|
|
{
|
|
public sealed class ExponentialMovingAverage
|
|
{
|
|
private readonly float alpha;
|
|
private float lastAverage = float.NaN;
|
|
|
|
public ExponentialMovingAverage(int period)
|
|
{
|
|
if (period <= 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than 0.");
|
|
}
|
|
alpha = 2f / (period + 1);
|
|
}
|
|
|
|
public float NextValue(float value)
|
|
{
|
|
if (float.IsNaN(lastAverage))
|
|
{
|
|
lastAverage = value;
|
|
}
|
|
else
|
|
{
|
|
lastAverage = (value * alpha) + (lastAverage * (1 - alpha));
|
|
}
|
|
|
|
return lastAverage;
|
|
}
|
|
}
|
|
}
|