Useful for filtering analog inputs to produce a smooth looking output for display.
-
simple
-
generic
-
efficient
- inputting values to the filter takes constant time regardless of the filter size
- if filter size is a power of 2 then division operations (slow) will likely be removed by the compiler
-
no dynamic memory allocation
- Include
moving_average_filter.h
- Create an instance of the moving average filter type with a name, type, and size with the macro
MOVING_AVERAGE_FILTER
. - Create an instance of
struct maf_*
where*
is the name you passed toMOVING_AVERAGE_FILTER
. - Initialize the instance with
maf_*_init
and input new values withmaf_*_input
.
#include <stdio.h>
#include "moving_average_filter.h"
//create maf_example to filter 5 samples of analog value
MOVING_AVERAGE_FILTER(example, int, 5);
//create instance
struct maf_example filt;
int main(void) {
//init filter
maf_example_init(&filt);
while (1) {
//get raw analog value
int val = read_analog_pin();
//filter the value
int filtered = maf_example_input(&filt, val);
//show filtered value
printf("filtered value %d", filtered);
}
}