Skip to content

Instantly share code, notes, and snippets.

@J3698
Created May 7, 2019 22:02
Show Gist options
  • Save J3698/5565706778f6e36ae4b0d409d3b2f8ec to your computer and use it in GitHub Desktop.
Save J3698/5565706778f6e36ae4b0d409d3b2f8ec to your computer and use it in GitHub Desktop.
typedef struct {
double average;
size_t oldest_index;
double *samples;
size_t num_samples;
} movingAverage_t;
void movingAverage_update(movingAverage_t *ma, double new_sample) {
ma->average -= ma->samples[ma->oldest_index];
ma->samples[ma->oldest_index] = new_sample;
ma->average += ma->samples[ma->oldest_index];
ma->oldest_index = (ma->oldest_index + 1) % ma->num_samples;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment