Calculate average of only 10 most recent float values in Update

I need to calculate the average of only the 10 most recent float values in a method being called every frame with the float values constantly changing.

If I have the following
1- Would I correctly be getting the average value of the 10 most recent floats?

Thanks!

List<float> floatVals= new List<float> { };
Update(){

floatVals.Add(myChangingFloatVal);

if(floatVals.count>=10){
floatVals.RemoveRange(0, 1);
_averageFloat= floatVals.Average();

}

I’m not sure where you are getting myChangingFloatVal at, but in your current code, when the for loop is hit, it will simply add the same number 10 times to your list. There is no pause for it to wait during the loop to have myChangingFloatVal change.

Unless you plan to change myChangingFloatVal within the for loop or use a coroutine where you can wait between iterations of adding to the list, you’re not going to see a changing value.

As far as Garbage collection goes, you will just need to profile it. Your other option is just use a class list that you can clear before adding values to it. Again, profile.

1 Like
  1. No.
  2. Yes.

You’ll want to keep a list around that persists between frames by making it a variable outside of this method. You’ll then want to add your float each time to the list, and truncate the list’s size down to 10 if it grows too big.

1 Like

Are you creating a new list each frame?

I would think you’d do something more like this:

private List<float> floatVals = new List<float>();

//Call with a new float to add each frame
//The return value is the average of all floats, with a maximum of 10 floats stored
private float getAverageFloat(float newFloat)
{
    floatVals.Add(newFloat);
    if (floatVals.Count > 10)  //Remove the oldest when we have more than 10
    {
        floatVals.RemoveAt(0);
    }

    float total = 0f;
    foreach (float f in floatVals)  //Calculate the total of all floats
    {
        total += f;
    }
    float average = total / (float)floatVals.Count;  //average is of course the total divided by the number of floats

    return average;
}
2 Likes

Actually, this makes sense if OP always wants the last 10. I might have misread it as OP wants to reach 10, get the average and then generate 10 new numbers.

2 Likes

Yeah the OP could be more clear. I’m making a lot of assumptions of what he/she really wants :stuck_out_tongue:

You could also calculate a moving average for the window. Probably overkill for summing ten numbers, but I’ve found these algorithms helpful a number of times. See the “boxcar filter” section: Moving average - Wikipedia

1 Like

Which begs the question: What is it used for, and does it actually NEED to be calculated every frame, or will every 10 frames do? Every 10 frames will still give 3+ values per second, should be more than enough data.

True, this isn’t anything remotely processor-heavy, but should still determine how much is actually required to get a usable result.

I think the only time I’ve ever needed this kind of thing was to display average frames per second. So that’s my wild guess :stuck_out_tongue:

1 Like

Thanks for the replies. I updated my question.