Skip to content

Instantly share code, notes, and snippets.

@bketen
Created April 26, 2018 15:51
Show Gist options
  • Save bketen/b356ff9c29093f193a1cc793233d1446 to your computer and use it in GitHub Desktop.
Save bketen/b356ff9c29093f193a1cc793233d1446 to your computer and use it in GitHub Desktop.
Moving average example in C
//Moving average filter implementation in c
#include <stdio.h>
int main() {
const int NUMBERS_SIZE = 10;
double series[NUMBERS_SIZE] = {10, 20, 30, 40, 50,
60, 70, 80, 90, 100};
int windowSize = 4;
double Mem[windowSize];
double movSum = 0;
double movAverage = 0;
int j=0;
for (int i = 0; i < (NUMBERS_SIZE) ; i++) {
Mem[j]=series[i];
j += 1;
j = j & (windowSize-1) ;
for (int b =0; b <= windowSize; b++) {
movSum += Mem[b];
}
movAverage = movSum / windowSize;
printf("Moving Average = %lf\n", movAverage);
movSum = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment