Moving average filter

I'm looking for a script that executes a moving average filter algorithm in C language, compatible with the H7 Portenta. Since I am new to C, I would appreciate if someone can let me know where I can find them or if there are any existing sources for scripts written in C.

Works good, sebnil/Moving-Avarage-Filter--Arduino-Library-: A moving average, also called rolling average, rolling mean or running average, is a type of finite impulse response filter (FIR) used to analyze a set of datum points by creating a series of averages of different subsets of the full data set. (github.com) and can be modified to suit your needs.

That library misses an important trick - for instance if you gave it a filter size of 100 it would sum 100 elements every time you pass it a new data point - not exactly efficient.

The trick with a moving average is to maintain the total without summing every value every time, ie pseudo-code more like:

  total -= array[i] ;     // oldest value is being lost off the end, so subtract from total
  total += new_value ;
  array[i] = new_value ;  // new value replaces oldest
  i = (i+1) % size ;   // step forward in buffer, wrapping as needed
  return (float) total / size ;   // no loop needed to return latest average as total is accurate.

Excellent post, that is a common error made in many moving average implementations. the speed up factor is about the number of the averaged samples, significant.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.