Shop OBEX P1 Docs P2 Docs Learn Events
Averaging numbers... — Parallax Forums

Averaging numbers...

ArchiverArchiver Posts: 46,084
edited 2003-12-01 20:34 in General Discussion
Hello Stampers,
Is there an easy way to average large word numbers?. For example, I wish to
average 62319 and 62323, which averages at 62321. I cant add them together
obviously.

Is there a way to "and" them together or something ?

I would like to average a list of 10 words this way.

Cheers,
Chris Anderson
Sunny Western Australia

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-11-27 21:34
    In a message dated 11/27/2003 4:27:36 PM Eastern Standard Time,
    fuel@b... writes:


    > Is there an easy way to average large word numbers?. For example, I wish to
    > average 62319 and 62323, which averages at 62321. I cant add them together
    > obviously.
    >

    How accurate do you have to be? You could divide each number by 2, get your
    average, the double your result. Your average would be off by 1, I think.

    Sid Weaver
    W4EKQ
    Port Richey, FL


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-27 21:41
    That is such a stupidly simple answer, that I am embarrassed I asked the
    question... Its 5:40 AM here, I shouldnt have gotten up so early.

    Thankyou very much...

    Cheers,
    Chris


    Original Message
    From: <Newzed@a...>
    To: <basicstamps@yahoogroups.com>
    Sent: Friday, November 28, 2003 5:34 AM
    Subject: Re: [noparse][[/noparse]basicstamps] Averaging numbers...


    > In a message dated 11/27/2003 4:27:36 PM Eastern Standard Time,
    > fuel@b... writes:
    >
    >
    > > Is there an easy way to average large word numbers?. For example, I wish
    to
    > > average 62319 and 62323, which averages at 62321. I cant add them
    together
    > > obviously.
    > >
    >
    > How accurate do you have to be? You could divide each number by 2, get
    your
    > average, the double your result. Your average would be off by 1, I think.
    >
    > Sid Weaver
    > W4EKQ
    > Port Richey, FL
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-27 21:57
    In a message dated 11/27/2003 4:44:23 PM Eastern Standard Time,
    fuel@b... writes:


    > That is such a stupidly simple answer, that I am embarrassed I asked the
    > question... Its 5:40 AM here, I shouldnt have gotten up so early.
    >
    No problem, Chris. Anything for a cobber.

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 01:24
    An average is the sum of N values, divided by N.

    An average is also N value each divided by N, then summed, but has the
    advantage that the largest computed number is always less than the largest
    value.

    As you are dealing with integer numbers, to be entirely accurate you should
    multiply the result by 1.005 to account for the fractional portion of the
    number that was dropped during the integer divide.

    Original Message
    From: <Newzed@a...>
    To: <basicstamps@yahoogroups.com>
    Sent: Thursday, November 27, 2003 1:34 PM
    Subject: Re: [noparse][[/noparse]basicstamps] Averaging numbers...


    > In a message dated 11/27/2003 4:27:36 PM Eastern Standard Time,
    > fuel@b... writes:
    >
    >
    > > Is there an easy way to average large word numbers?. For example, I wish
    to
    > > average 62319 and 62323, which averages at 62321. I cant add them
    together
    > > obviously.
    > >
    >
    > How accurate do you have to be? You could divide each number by 2, get
    your
    > average, the double your result. Your average would be off by 1, I think.
    >
    > Sid Weaver
    > W4EKQ
    > Port Richey, FL
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 02:39
    Chris, glad to help, I just got into stamps about a month ago. You
    can divide the two numbers first by two, then add them. so (62319/2)+
    (62323/2)=62321

    Chris Schur

    --- In basicstamps@yahoogroups.com, "Chris Anderson" <fuel@b...>
    wrote:
    > Hello Stampers,
    > Is there an easy way to average large word numbers?. For example, I
    wish to
    > average 62319 and 62323, which averages at 62321. I cant add them
    together
    > obviously.
    >
    > Is there a way to "and" them together or something ?
    >
    > I would like to average a list of 10 words this way.
    >
    > Cheers,
    > Chris Anderson
    > Sunny Western Australia
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 03:51
    How about divide both by 2, take that average, then multiply by 2?

    average of 100 & 200 is 150

    average of 50 & 100 is 75 and 75 times 2 is 150.......
    Hello Stampers,
    Is there an easy way to average large word numbers?. For example, I wish to
    average 62319 and 62323, which averages at 62321. I cant add them together
    obviously.

    Is there a way to "and" them together or something ?


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 04:11
    If I remember correctly from High School, the average of a sequence
    of numbers is the sum of N numbers divided by N.
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 06:39
    >Hello Stampers,
    >Is there an easy way to average large word numbers?. For example, I wish to
    >average 62319 and 62323, which averages at 62321. I cant add them together
    >obviously.
    >Is there a way to "and" them together or something ?
    >I would like to average a list of 10 words this way.
    >Cheers,
    >Chris Anderson
    >Sunny Western Australia


    Another way to do this, when all the numbers are close together, is
    to work with the differences instead of the absolute numbers. For
    example, take 62319 and 62323. The difference is 62323-62319 = 4.
    Divide that by 2. You get 2. Add 2 to 62319. You get 62321, which
    is the average. Or subtract 2 from 62323, same difference.

    The algebra for two numbers goes like this:
    (A+B)/2
    = {A+A+(B-A)}/2
    = A +(B-A)/2
    In general (B-A)/2 could be either positive or negative, which just
    means you end up either adding or subtracting from A, depending on
    whether A>B or vice versa.

    The same algebra applies to a long list of numbers:
    (A+B+C+D+E+F+G+H+I+J)/10
    = {N*A +(B-A)+(C-A)+(D-A)+...+(J-A)}/10
    = A + {(B-A)+(C-A)+(D-A)+...+(J-A)}/10
    On a microprocessor, the value A is the first one acquired, or it may
    be the average from previous readings, or, it really doesn't matter
    how you choose A, so long as the sum of the differences remains
    small. If the sample values are close to one another and to the
    initial value A, then the magnitude of the sum of the differences
    will not grow very fast. The numbers do have to be treated as twos
    complement values, but that is really no restriction at all. You do
    have to be careful on the stamp to respect the sign of the
    accumulation when you do the division at the end. Here is an example
    average of 10 numbers...

    result Var word ' randomized process variable
    A Var word ' initial value
    Acc Var word ' difference accumulator
    i Var nib
    average Var word

    GOSUB getvalue ' get initial value A
    A = result
    DO
    Acc = 0
    FOR i=1 to 9 ' get 9 more samples
    gosub getvalue
    Acc = (result - A) + Acc ' accumulate differences
    NEXT
    average = A + (-Acc.bit15^(ABS Acc/10)+1)
    IF Acc.bit15 THEN ' respect the sign, bit 15 of Acc
    average = A - (ABS Acc / 10) ' negative
    ELSE
    average = A + (Acc / 10) ' positive
    ENDIF
    debug ? average ' show it
    A = average ' make this the first sample of the next round
    LOOP


    It is also possible to do the average using double precision. That
    is, you would add numbers like 62323 and 62319 together directly and
    generate a carry to a high word. At the end, a double precision
    division is necessary, but it is not really that hard if the number
    of samples is less than 256.

    result Var word
    Acc0 Var word ' accumulator low word
    Acc1 Var byte ' accumulator high byte (less than 256 samples)
    i Var byte
    average Var word
    N Con 10 ' this many samples (N<256)

    DO
    Acc0 = 0 ' init accumulators
    Acc1= 0
    FOR i=1 to N ' get N samples
    gosub getvalue
    Acc0 = result + Acc0
    IF Acc0<result THEN Acc1=Acc1+1 ' carry
    NEXT
    i = Acc1//N ' temporary value
    Acc1 = 65535//N*i//N+i+(Acc0//N) ' temporary
    average = 65535/N*i+(65535//N*i/N)+(Acc0/N)+(Acc1/N) 'final
    debug ? average ' show it
    LOOP


    If the number of samples is fixed at N=10, then some terms in the
    double precision division formula can be precomputed. More info
    about that, and where the heck that division formula comes from, at
    <http://www.emesystems.com/BS2math6.htm>.

    -- Tracy








    '
    *division*
    r0=n1//dv ' remainder from high word
    q1=65535//dv*r0//dv+r0+(n0//dv) ' temporary computation
    q0=65535/dv*r0+(65535//dv*r0/dv)+(n0/dv)+(q1/dv) ' low
    word of quotient
    r0=q1//dv ' final remainder
    q1=n1/dv ' high word of quotient
    '
    *end of division*
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 06:59
    Oh, the following are two ways to compute the average from the
    accumulation, one using computed logic:

    > average = A + (-Acc.bit15^(ABS Acc/10)+1) '<--see below

    and the other using IF-THEN logic:

    > IF Acc.bit15 THEN ' respect the sign, bit 15 of Acc
    > average = A - (ABS Acc / 10) ' negative
    > ELSE
    > average = A + (Acc / 10) ' positive
    > ENDIF

    I put both of them in the program, because I started with the
    computed one, and then decided the IF-THEN version was more
    transparent, to see what is going on. then forgot to take the first
    one out.

    Anyway, the computed one should read like this, not as above:

    average = A + (-Acc.bit15^(ABS Acc/10)+Acc.bit15)

    The computed version divides the absolute value of the number by 10, and
    then either makes it negative or leaves it positive, depending on the
    value of the sign bit, Acc.bit15.

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 19:44
    If you only want to average two numbers, can't you just Right-shift
    each (halving them), and then add?

    On Nov 27, 2003, at 4:23 PM, Chris Anderson wrote:

    > Hello Stampers,
    > Is there an easy way to average large word numbers?. For example, I
    > wish to
    > average 62319 and 62323, which averages at 62321. I cant add them
    > together
    > obviously.
    >
    > Is there a way to "and" them together or something ?
    >
    > I would like to average a list of 10 words this way.
    >
    > Cheers,
    > Chris Anderson
    > Sunny Western Australia
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 20:48
    >If you only want to average two numbers, can't you just Right-shift
    >each (halving them), and then add?


    Suppose the numbers are 101 and 99. The average is clearly 100.
    However, the integer math of the Stamp comes up with,

    average= (101/2)+(99/2) ' = 50+49=99

    Not far off. But the problem is the remainders that were dropped.
    The stamp can pick up the sum of the remainders, too, and make the
    correction:
    average= (101/2)+(99/2)+((101//2)+(99//2)/2) ' = 50 + 49 + ((1+1)/2) = 100

    The // operator gives the remainder after the division by 2.

    Chris said he needed a list of 10 numbers. If you change
    average= (A+B+C+D+E+F+G+H+I+J)/10
    into

    average=(A/10)+(B/10)+(C/10)+(D/10)+(E/10)+(F/10)+(G/10)+(H/10)+(I/10)
    +(J/10)
    you would get the correct answer on a hand calculator, but the error
    due to integer rounding on the Stamp could get to be quite serious.
    It could be off by as much as 90. Again in this case you could make
    a correction by also adding up the remainders and dividing their sum
    by 10.
    average=
    (A/10)+(B/10)+(C/10)+(D/10)+(E/10)+(F/10)+(G/10)+(H/10)+(I/10)+(J/10)
    + (((A//10)+(B//10)+(C//10)+(D//10)+(E//10)+(F//10)+(G
    //10)+(H//10)+(I//10)+(J /10))/10)

    The second line is the sum of the remainders (// operator), divided
    by 10 at the end. There are so many ways to go about this!

    -- Tracy



    >
    >On Nov 27, 2003, at 4:23 PM, Chris Anderson wrote:
    >
    >> Hello Stampers,
    >> Is there an easy way to average large word numbers?. For example, I
    >> wish to
    >> average 62319 and 62323, which averages at 62321. I cant add them
    >> together
    >> obviously.
    >>
    >> Is there a way to "and" them together or something ?
    >>
    >> I would like to average a list of 10 words this way.
    >>
    >> Cheers,
    >> Chris Anderson
    > > Sunny Western Australia
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-28 22:30
    One way to do it -- that is not without error, but will prevent
    rolloevers -- is to divide first. You must, of course, know the number
    of samples in order to do this.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Chris Anderson [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=OscrqZPXiqG59Cx9RtfR6neDpXqlG1z7b_FgR6KoMerkrRZuaOb-rGybCOh1Qqeq-o-pm5D5mnpqM8vDmC8]fuel@b...[/url
    Sent: Thursday, November 27, 2003 3:24 PM
    To: Stamp Group
    Subject: [noparse][[/noparse]basicstamps] Averaging numbers...


    Hello Stampers,
    Is there an easy way to average large word numbers?. For example, I wish
    to average 62319 and 62323, which averages at 62321. I cant add them
    together obviously.

    Is there a way to "and" them together or something ?

    I would like to average a list of 10 words this way.

    Cheers,
    Chris Anderson
    Sunny Western Australia


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-29 01:25
    Gee, I'm a bit overwhelmed with all the information coming in, thanks heaps
    to all of you.

    My application doesnt mind if I am out by 1 at the end of averaging the
    numbers, as the numbers will vary by a reasonable amount anyway. But its
    good to get answers which will make the answer accurate.

    Thanks again everyone, I'll go thru them and work out which method will suit
    my application best, which is fluid level measurement with a pressure
    sensor. Because I am using a 12 bit ADC, the measurements vary slightly with
    very small voltage changes, and I therefore need to take a few samples and
    then average them.

    Cheers,
    Chris Anderson
    Western Australia


    Original Message
    From: "Jon Williams" <jwilliams@p...>
    To: <basicstamps@yahoogroups.com>
    Sent: Saturday, November 29, 2003 6:30 AM
    Subject: RE: [noparse][[/noparse]basicstamps] Averaging numbers...


    > One way to do it -- that is not without error, but will prevent
    > rolloevers -- is to divide first. You must, of course, know the number
    > of samples in order to do this.
    >
    > -- Jon Williams
    > -- Applications Engineer, Parallax
    > -- Dallas Office
    >
    >
    >
    Original Message
    > From: Chris Anderson [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=N31qx1G7i5BlqmkQTYVJ9p4PGd9GNtsuMlV-M8EhvZzJehzIIK9JkRjCigYbqYrbkiF5D6_a4by0DuU]fuel@b...[/url
    > Sent: Thursday, November 27, 2003 3:24 PM
    > To: Stamp Group
    > Subject: [noparse][[/noparse]basicstamps] Averaging numbers...
    >
    >
    > Hello Stampers,
    > Is there an easy way to average large word numbers?. For example, I wish
    > to average 62319 and 62323, which averages at 62321. I cant add them
    > together obviously.
    >
    > Is there a way to "and" them together or something ?
    >
    > I would like to average a list of 10 words this way.
    >
    > Cheers,
    > Chris Anderson
    > Sunny Western Australia
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    > This message has been scanned by WebShield. Please report SPAM to
    > abuse@p....
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-29 01:27
    Since you're using 12-bit values you can add up to 16 readings before a
    possible rollover error occurs ($FFFF / $FFF = 16). This is probably
    more samples than you need for your process so in the end, there are no
    worries mate.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Chris Anderson [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=-EgIARUSXuTfI_gZC3E77J7pvjvchuP_xZC98_Q2G3GehDpXAi3RfpmGyZtFj4YgNh2qG-dbbGeoIF_k99Y]fuel@b...[/url
    Sent: Friday, November 28, 2003 7:25 PM
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] Averaging numbers...


    Gee, I'm a bit overwhelmed with all the information coming in, thanks
    heaps to all of you.

    My application doesnt mind if I am out by 1 at the end of averaging the
    numbers, as the numbers will vary by a reasonable amount anyway. But its
    good to get answers which will make the answer accurate.

    Thanks again everyone, I'll go thru them and work out which method will
    suit my application best, which is fluid level measurement with a
    pressure sensor. Because I am using a 12 bit ADC, the measurements vary
    slightly with very small voltage changes, and I therefore need to take a
    few samples and then average them.

    Cheers,
    Chris Anderson
    Western Australia


    Original Message
    From: "Jon Williams" <jwilliams@p...>
    To: <basicstamps@yahoogroups.com>
    Sent: Saturday, November 29, 2003 6:30 AM
    Subject: RE: [noparse][[/noparse]basicstamps] Averaging numbers...


    > One way to do it -- that is not without error, but will prevent
    > rolloevers -- is to divide first. You must, of course, know the
    > number of samples in order to do this.
    >
    > -- Jon Williams
    > -- Applications Engineer, Parallax
    > -- Dallas Office
    >
    >
    >
    Original Message
    > From: Chris Anderson [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=-EgIARUSXuTfI_gZC3E77J7pvjvchuP_xZC98_Q2G3GehDpXAi3RfpmGyZtFj4YgNh2qG-dbbGeoIF_k99Y]fuel@b...[/url
    > Sent: Thursday, November 27, 2003 3:24 PM
    > To: Stamp Group
    > Subject: [noparse][[/noparse]basicstamps] Averaging numbers...
    >
    >
    > Hello Stampers,
    > Is there an easy way to average large word numbers?. For example, I
    > wish to average 62319 and 62323, which averages at 62321. I cant add
    > them together obviously.
    >
    > Is there a way to "and" them together or something ?
    >
    > I would like to average a list of 10 words this way.
    >
    > Cheers,
    > Chris Anderson
    > Sunny Western Australia
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    > This message has been scanned by WebShield. Please report SPAM to
    > abuse@p....
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-12-01 20:34
    You can use the following equivalency:

    (A+B)/2 = (A/2) + (B/2)

    or, basically, 62319/2 plus. 62321/2. Unfortunately, since you're dealing
    with integer math, you'll have to fake the .5's. In this example, you're
    always dropping a .5 if the number is odd, so you could do something like
    detect if the numbers are even or odd (using modulus). If there are zero
    odd numbers, it's accurate. If there are one, it's .5. if there are two,
    you'd have to add +1 to the amount. Of course, the fundamental problem is
    you're dealing with a possible decimal value within integer space.



    On Fri, 28 Nov 2003, Chris Anderson wrote:

    > Hello Stampers,
    > Is there an easy way to average large word numbers?. For example, I wish to
    > average 62319 and 62323, which averages at 62321. I cant add them together
    > obviously.
    >
    > Is there a way to "and" them together or something ?
    >
    > I would like to average a list of 10 words this way.
    >
    > Cheers,
    > Chris Anderson
    > Sunny Western Australia
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >

    Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
    Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma - Bremerton
    email: lamont@a... WWW: http://www.serv.net
    "Do not fear mistakes, There Are None" - Miles Davis
Sign In or Register to comment.