Shop OBEX P1 Docs P2 Docs Learn Events
Averaging a Word sized array? — Parallax Forums

Averaging a Word sized array?

ArchiverArchiver Posts: 46,084
edited 2001-06-05 14:01 in General Discussion
I am using a bs2 to read a sensor that returns a word sized variable. I
would like to store several samples of this variable in an array and then
average the array in order to "clean up" my results. I have read the section
in the stamp manual 1.9 concerning arrays but I am still confused (as
usual).

Does anyone know of a good tutorial that I might find online?

Thanks,

Jeff

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-06-04 17:00
    Why do you need an array? Why not:

    samps con 4
    total var word
    i var nib
    reading var word ' from your subroutine
    total=0
    for i=1 to samps
    gosub read_value ' your routine sets reading
    total=total+reading
    next i
    total=total/samps
    ' total now has average reading

    Regards,

    Al Williams
    AWC
    * Help beta test new electronics classifieds FREE
    http://www.al-williams.com/classad



    >
    Original Message
    > From: Jeff & Julia [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=sjzL2FBTC3C4BqaPa8kpbIvrL9AMNXBLIH9mAwgm-AagGBXFtrO8bk0HvkroEF5Js2MooD0IkqBytiXX]EL-JEFE@P...[/url
    > Sent: Monday, June 04, 2001 10:34 AM
    > To: Basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Averaging a Word sized array?
    >
    >
    > I am using a bs2 to read a sensor that returns a word sized variable. I
    > would like to store several samples of this variable in an array and then
    > average the array in order to "clean up" my results. I have read
    > the section
    > in the stamp manual 1.9 concerning arrays but I am still confused (as
    > usual).
    >
    > Does anyone know of a good tutorial that I might find online?
    >
    > Thanks,
    >
    > Jeff
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed with. 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 2001-06-04 17:08
    > I am using a bs2 to read a sensor that returns a word sized variable. I
    >would like to store several samples of this variable in an array and then
    >average the array in order to "clean up" my results. I have read the section
    >in the stamp manual 1.9 concerning arrays but I am still confused (as
    >usual).
    >
    > Does anyone know of a good tutorial that I might find online?
    >
    >Thanks,
    >
    >Jeff


    Hi Jeff,

    No array is needed to compute a simple average. Make an accumulator
    to store up the sum of all the readings as they come in, and a
    variable to count the number of samples. At the end, divide the
    accumulator by the number of samples. (caveat: the accumulation has
    to stay less than 65536, else you have to plan for double precision
    accumulation.)

    If you do want to use an array, it would go something like the following:

    myArray var word(5) ' 5 samples
    result var word ' readings
    i var nib
    for i=0 to 4
    gosub getData ' not shown
    myArray(i)=result
    next
    ' have 5 readings in array
    ' now find average
    result=0
    for i=0 to 4
    result = result + myArray(i)
    next
    result = result/5 ' average
    debug ? result
    end


    There is a bit of a tutorial on data filters on my web page at this URL:
    http://www.emesystems.com/BS2math5.htm

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    mailto:tracy@e...
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-05 03:01
    Why?

    Just add the results together as they come in and divide the sum by the
    number of samples -- no array needed.

    Original Message


    > I am using a bs2 to read a sensor that returns a word sized variable. I
    > would like to store several samples of this variable in an array and then
    > average the array in order to "clean up" my results. I have read the
    section
    > in the stamp manual 1.9 concerning arrays but I am still confused (as
    > usual).
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-05 14:01
    Thanks Al, Tracy, and Rodent. your suggestions work beautifully (of course).

    Why does the stamp limit me to 15 samples for averaging in this situation?
    My peak reading is 1810 but when I try to average more than 15 samples the
    debug window goes blank. Just curious.

    Jeff


    Original Message
    From: Rodent <daweasel@s...>
    To: <basicstamps@yahoogroups.com>
    Sent: Monday, June 04, 2001 10:01 PM
    Subject: Re: [noparse][[/noparse]basicstamps] Averaging a Word sized array?


    > Why?
    >
    > Just add the results together as they come in and divide the sum by the
    > number of samples -- no array needed.
    >
    >
    Original Message
    >
    >
    > > I am using a bs2 to read a sensor that returns a word sized variable.
    I
    > > would like to store several samples of this variable in an array and
    then
    > > average the array in order to "clean up" my results. I have read the
    > section
    > > in the stamp manual 1.9 concerning arrays but I am still confused (as
    > > usual).
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed with. 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/
    >
    >
Sign In or Register to comment.