+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Junior Member level 1
    Points: 544, Level: 4

    Join Date
    Jul 2017
    Posts
    19
    Helped
    0 / 0
    Points
    544
    Level
    4

    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.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 81,162, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,459
    Helped
    3734 / 3734
    Points
    81,162
    Level
    69

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisement

        
       

  3. #3
    Super Moderator
    Points: 264,335, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,189
    Helped
    14047 / 14047
    Points
    264,335
    Level
    100

    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



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 30,125, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,995
    Helped
    1138 / 1138
    Points
    30,125
    Level
    42
    Blog Entries
    9

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  5. #5
    Advanced Member level 5
    Points: 9,242, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,601
    Helped
    368 / 368
    Points
    9,242
    Level
    22

    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[100];
    
    int main(void)
    {
    	uint16_t i;
    
    	for (i = 0; i != 100; i++)
    		buf[i] = 0.0;
    
    	for (i = 0; i != 100; i++) {
    		add_value(buf, 100, i / 10);
    	}
    
    	printf("Average 5: %f\n", get_average(buf, 100, 5));
    	printf("Average 10: %f\n", get_average(buf, 100, 10));
    	printf("Average 50: %f\n", get_average(buf, 100, 50));
    }
    Last edited by Easyrider83; 4th January 2020 at 11:58.
    Love me or hate me. All infractions will be ignored.



--[[ ]]--