-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathMovingAverage.cs
42 lines (34 loc) · 1.11 KB
/
MovingAverage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections.Generic;
namespace RuntimeUnityEditor.Core.Utils
{
internal class MovingAverage
{
private readonly int _windowSize;
private readonly Queue<long> _samples;
private long _sampleAccumulator;
public MovingAverage(int windowSize = 11)
{
_windowSize = windowSize;
_samples = new Queue<long>(_windowSize + 1);
}
///// <summary>
///// Highest sample value ever, even if the sample is no longer counted in the average.
///// </summary>
//public long PeakValue { get; private set; }
public long GetAverage()
{
if (_samples.Count == 0)
return 0;
return _sampleAccumulator / _samples.Count;
}
public void Sample(long newSample)
{
_sampleAccumulator += newSample;
_samples.Enqueue(newSample);
if (_samples.Count > _windowSize)
_sampleAccumulator -= _samples.Dequeue();
//if (PeakValue < newSample)
// PeakValue = newSample;
}
}
}