Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20563

Share your working BASCOM-AVR code here : Moving average for INTEGER and SINGLE data streams : REPLY

$
0
0
Hi Meister Well, my java skills are rather limited but it looks quite ok to me the way you build the averaging function. The mechanism is as follows: 1. Use an accumulator with enough width to hold the integer plus extra bits you loose when shifting. 2. Load the accumulator into a temporary variable of same length. 3. Right-Shift the number of steps you desire (e.g divide by 2^N) Be sure to obey the signbits when shifting so you dont shift in zeroes when accumulator was negative 4. Assign the above result to your output variable (Probably an integer) This is your new avarage 5. Add the new data from the input stream to your accumulator 6. Subtract the oldest data from the accumulator to compensate for this newly added (this is the "running" thing) Step 6 is where you make the magic trick. As you dont have the oldest indata value anymore (we dont save all data remember!), we have to use the most recent average as a approximative substitute for the oldest indata. In the long run this is a rather good guess, so the iteration always converge. In steady state the accumulator utilization will be the size of your indata * 2^N So you will not suffer from accumulator overflow as long as your average indata is less than accumulator_range/2^N /Per

Viewing all articles
Browse latest Browse all 20563

Trending Articles