long av_acc; // Storage
int sample; // Raw data
int output; // filtered data
// Sample process routine
if(!av_acc) {
// Initialise for fast start up & settling
// You can use a bit set to show initialisation complete instead for speed, or if the samples are zero based.
output = sample;
av_acc = (long)(sample << 4);
}
else {
// Accumulator = 16x sample value, if the shift is 4.
// Output = accumulator / 16
// Subtract that = 15 samples old, add new sample.
output = (int)(av_acc >> 4);
av_acc -= (long)output;
av_acc += (long) sample;
}