# Simple C code for 50 point moving average filter using Buffer

1. ## Simple C code for 50 point moving average filter using Buffer

Hi all,
Anybody has a code written for a N point moving average filter in C?
I am basically stuck with the implementation when it comes to the averaging part.
It would be great if someone can help me kickstart with sample code.

Regards.  Reply With Quote

•

2. ## Re: Simple C code for 50 point moving average filter using Buffer

Hi,

A forum is not meant that others do your job.
--> Show what you have done so far.

There are explanations as well as code in the internet...

Klaus  Reply With Quote

•

3. ## Re: Simple C code for 50 point moving average filter using Buffer

I am basically stuck with the implementation when it comes to the averaging part
Refer to definition of arithmetic mean, apply it to the buffer values. https://en.wikipedia.org/wiki/Arithmetic_mean  Reply With Quote

•

4. ## Re: Simple C code for 50 point moving average filter using Buffer

code for 50 point moving average filter using Buffer
Another useful concept you'll need to know during code implementation is the circular buffer, which can be used to add/remove samples, one per time, keeping the last 49 ones.  Reply With Quote

5. ## Re: Simple C code for 50 point moving average filter using Buffer

Code:
```void add_value(double *buf, uint16_t size, double value)
{
uint16_t i;

for (i = 1; i != size; i++)
buf[i] = buf[i + 1];

buf[size - 1] = value;
}

double get_average(double *buf, uint16_t size, uint16_t average)
{
double sum = 0;
uint16_t i;

for (i = size - average; i != size; i++)
sum += buf[i];

return sum / average;
}```
Usage:
Code:
```double buf;

int main(void)
{
uint16_t i;

for (i = 0; i != 100; i++)
buf[i] = 0.0;

for (i = 0; i != 100; i++) {  Reply With Quote