calculating a average

klaasje

Member
Hello,
I have an incoming ADC signal from which I must have an average of the last 100 measured signals .
So a kind of shift register whit max 100 values ​​for which an average is calculated over.
It must be so that only places with a value are to be included in the calculation, otherwise the outcome is not right.

Someone an example?


Thanks,

Klaas
 

Technical

Technical Support
Staff member
The simplest way is to just add them together and then divide by 100
Code:
w1 = 0
for b1 = 1 to 100
   readadc pin, b0
   w1 = w1 + b0
next b1

result = w1 / 100
 

Hemi345

Senior Member
Do you mean ignoring a 0 ADC reading? Like this:

Code:
symbol result = w2
w1 = 0
for b1 = 1 to 100
   readadc C.1, b0
   if b0 > 0 then
       w1 = w1 + b0
   else
       dec b1 ;ignore this zero reading
   end if
next b1
result = w1 / 100
 

premelec

Senior Member
I would think likely you'd filter within a range.... IF b0 > X AND B0 < Y THEN add it in... X and Y could be adjusted as you go in some cases... I'm assuming you are trying to get rid of noisy or spurious values...
 

klaasje

Member
I have incomming anolog readings. So i start the reading whit a switsch and then the readings must go into the memory. So when you have 5 readings in the memory you must divide it in 5 and when you have 9 reading you must divide it 9 ect.
 

klaasje

Member
I have incomming anolog readings. So i start the reading whit a switsch and then the readings must go into the memory. So when you have 5 readings in the memory you must divide it in 5 and when you have 9 reading you must divide it 9 ect.
 

westaust55

Moderator
Do you specifically need to separately store each of the 100 analogue reading/measurement values?
If not then the following approach should suffice:

Code:
Symbol Start_Switch  = pinX.X ; high on this switch starts the measurement and averaging
Symbol ADC_Pin = Y.Y ; this is the IO pin for analog signal measurements
Symbol Summation = w0 ; summation/addition of all valid readings to date
Symbol Average = w1
Symbol Counter = b4 ; number of valid readings to date
Symbol Reading = b5 ; the current analog signal reading/measurement

Wait4Switch:
    IF Start_Switch = 0 THEN Wait4Switch
    Summation = 0 ; clear the accumulator
    Counter  = 0 ; clear reading counter
    DO
        READADC ADC_Pin,  reading
        IF reading > 0 THEN         ; if non zero then accept the new reading
            Summation  = Summation + reading
            INC Counter
            Average = Summation / Counter
        ENDIF
LOOP UNTIL Counter = 100 ; here set basis to complete taking readings

If you do need to separately store the values then depending upon which PICAXE chip you are using (M2 or X2 part) you could use the bptr or ptr indirect addressing pointer with post increment (ie @bptrinc or @ptrinc) to save the readings in consecutive memory location for later calculation.
 

klaasje

Member
Thanks westaust55

That's exactly what I was looking for. Its so simple. Why did i not think of it.

Thanks to all
 
Top