Scott Dattalo wrote: > > Ralf Sigmund wrote: > > > So I would like to implement a running average algorithm, which gives > > the average of the last eight values. > > It should add up the last eight values and divide then by 8. > > but my values can be everything between 256 and 0. y = ( a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 ) / 8 ( ( ( a1 + a2 )/2 + ( a3 + a4 )/2 )/2 + ( ( a5 + a6 )/2 + ( a7 + a8 )/2 )/2 )/2 It will be more easy if your values will range from 0 to 255 ( byte value ) > Ralf, > > Is it necessary to average the last 8 values non-recursively? > In other words, you're proposing a low-pass digital FIR (finite > impulse response) filter. The frequency response is "okay", but... > Have you considered an IIR (infinite impulse response) filter? > Specifically, I've had good results with filters of this type: > > avg = (m*avg + n*sample) /(m+n) > > e.g. If I wish to give the average more weight than the current sample: > avg = (3*avg + sample)/4 > Or, vice-versa: > avg = (avg + 3*sample)/4 > > An example of the later case implemented in ASM (and untested): > > CLRF temp > CLRC > RLF sample,W ;W = 2*sample > RLF temp,F ;Catch the carry > ADDWF sample,W ;W = 3*sample > SKPNC > INCF temp,F ;Catch the carry again > ADDWF avg,F ;avg = avg+3*sample > SKPNC > INCF temp,F > RRF temp,F ;Get the carries > RRF avg,F ;avg /= 2 > RRF temp,F > RRF avg,F ;avg /= 4 > > 14 Cycles > > Scott There are more short version algorithm above ;) No additional cell required avg = (avg + 3*sample)/4 = ( ( avg + sample )/2 + sample )/2 MOVFW SAMPLE ADDWF AVG,F RRF AVG,F ADDWF AVG,F RRF AVG,F 5 Cycles WBR Dmitry.