Open DigitalLibrarian opened 8 years ago
This should work
public class Smoother { public float Value { get { return ComputeLatest(); }}
int MaxSamples;
List<float> Samples { get; set; }
public Smoother(int maxSamples)
{
MaxSamples = maxSamples;
Samples = new List<float>();
CurrentSample = 0;
}
public void Update(float v)
{
if(CurrentSample == Samples.Count){
Samples.Add(v);
}else{
Samples[CurrentSample] = v;
}
IncSample();
}
int CurrentSample;
void IncSample()
{
CurrentSample++;
CurrentSample %= MaxSamples;
}
float ComputeLatest()
{
int numSamples = Samples.Count();
if (numSamples == 0) return 0f;
float total = 0f;
foreach(var sample in Samples)
{
total += sample;
}
return total / (float) numSamples;
}
}
We need a generic smoothing class that operates on float. It exposes a value that is the running average for the last N calls for some observed value V. This can be used for many things.
The chief reason for it, atm, is to have smoothed fps displays, showing the average for the last 60 frames, for example. We currently display the instantaneous fps, which isn't stable or useful as a load metric.