Created
May 7, 2019 22:02
-
-
Save J3698/5565706778f6e36ae4b0d409d3b2f8ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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