A fast moving average would be something like this:mav = (1-alpha)*mav + alpha * new_data;where 0<alpha<1 as a weight for new observations. The smaller alpha is, the longer memory mav has of historical data.If you pick alpha to be 1/2^n, the math gets considerably easier as you can simply shift. For example, for alpha = 1/16:mav = mav + (new_data - mav) / 16.essentially, 1 subtraction, 1 shift, 1 increment.
P.S. Question for the C++ wiz. Would:mav += ((new_data - mav) / 16) ; work? faster?
QuoteP.S. Question for the C++ wiz. Would:mav += ((new_data - mav) / 16) ; work? faster? You can easily test if this works (depending on the datatypes you can have certain side effects !)faster? think yes you need to measure 10000 times or so to see diffs.
So would this in effect be acting as a low pass filter on the signal? If so how is the 16 related to the 'corner frequency of the effective filtering action, or is that more a function of how often the statement is executed per time unit?
mav += ((new_data - mav) / 16) ;
mav += ((signed short) (new_data - mav)) >> 4;
new_data=analogRead(MY_CHANNEL) << 4; //read the data mav += ((signed short) (new_data - mav)) >> 4; //compute moving average
mav = mav + (new_data - mav) / 16 ;
There are two issues: signed vs. unsigned, and integer math.Which one are you referring to?
there are 86 ms of delay
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16