You can simply do:
double approxRollingAverage (double avg, double new_sample) {
avg -= avg / N;
avg += new_sample / N;
return avg;
}
Where N
is the number of samples where you want to average over.
Note that this approximation is equivalent to an exponential moving average.
See: Calculate rolling / moving average in C++