Shop OBEX P1 Docs P2 Docs Learn Events
Smoothing math — Parallax Forums

Smoothing math

ArchiverArchiver Posts: 46,084
edited 2002-03-26 19:21 in General Discussion
Smoothing problem.

For a tachometer I am building, I would like to experiment with mathematical
smoothing.

The tach monitors the rps of an underwater impeller on a model sailboat. It is
a Hall switch circuit, and works nicely.

But the numbers come fast and the trend jumps around a bit. With a VOM display,
the inertia inherent in the galvanometer movement tends to smooth out the trend.
But for the finished product I want to use an
LCD. No inertia.

Stock market data is often smoothed out with a simple moving average. But since
the stamp cannot divide faithfully, I was wondering if an exponential smoothing
function might work better.

I know these exponential smoothing functions exist, but can't remember how they
look or how they work. Or if they require division.

Thank you for any insights on how to smooth the stream of numbers coming in from
the impeller.

Michael

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-03-25 14:32
    What's the range of your data? If it is small enough you can do it all
    in the Stamp. Otherwise, check out a PAK-I, a PAK-II, or a PAK-IX (A/D
    built into the PAK-IX).

    Suppose your data ranges from 0 to 100. You can use a word and scale
    everything up by say, 10.

    So your data stream is 90,88,92,89...

    You do this:

    V1=90*10
    V1=V1+(88*10)
    V1=V1+(92*10)
    V1=V1+(89*10)
    ' now V1= 3590 which is really 359.0
    V1=V1/4 ' 4
    ' now V1=897
    V1=V1+5 ' round off V1=902
    V1=V1/10
    ' V1=90, very close to the correct 89.75

    Now to do a moving average, you'd deduct 900 from the total before
    adding the next sample (*10 of course). That means you have to remember
    4 samples.

    You can do the same trick with other scales. For example, rounding to a
    bit level is easy with the shift instructions or by bit manipulation.
    Consider this:

    Sample=90
    V1=Sample<<4 ' * 16
    Sample=88
    V1=V1+(Sample<<4)
    Sample=92
    V1=V1+(Sample<<4)
    Sample=89
    V1=V1+(Sample<<4)
    ' Now V1=5744
    V1=V1/4 ' V1=1436
    ' Now we want to round to the bit level so
    V1=V1+8 ' 16/2
    V1=V1>>4 ' V1=90

    You can also "keep" the decimal point if you prefer. Consider this
    quasi-code:
    V1=90*100
    V1=V1+(88*100)
    V1=V1+(92*100)
    V1=V1+(89*100)
    ' now V1= 35900 which is really 359.00
    V1=V1/4 ' 4
    ' now V1=8975
    V1=V1+5 ' round off V1=8980
    V1=V1/10
    Debug dec v1/10, ".", dec v1 // 10

    That will print out 89.8 which is awfuly close to 89.75. Also, you could
    skip the +5/10 steps and do something like:

    Ipart = V1/100
    Fpart = v1//100
    Debug dec Ipart, ".", dec2 Fpart (I think that works)

    In other words, if the Fpart is 9, you want .09 not .9!

    Of course, this only works because the maximum number is 100 and
    100*4*100=40000 which is <65535. If your maximum number was 200, you
    might overflow the 16-bit variable. Then you'd need to scale smaller or
    use fewer samples (200*3*100=60000).

    I'm sure Tracy can explain it better...


    Good luck!

    Al Williams
    AWC
    * Floating point math for the Stamp, PIC, SX, or any microcontroller
    http://www.al-williams.com/awce/pak1.htm

    >
    Original Message
    > From: Michael Gianturco [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=n8_74gD1k74ynj8S_GYE94fjKIH5LDd71fr1c7Svv1oJo-iofGDQLv2LEWRWwcHuxqY5fX4-yxva6ps]michcg@m...[/url
    > Sent: Monday, March 25, 2002 8:06 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Smoothing math
    >
    >
    > Smoothing problem.
    >
    > For a tachometer I am building, I would like to experiment
    > with mathematical smoothing.
    >
    > The tach monitors the rps of an underwater impeller on a
    > model sailboat. It is a Hall switch circuit, and works nicely.
    >
    > But the numbers come fast and the trend jumps around a bit.
    > With a VOM display, the inertia inherent in the galvanometer
    > movement tends to smooth out the trend. But for the
    > finished product I want to use an
    > LCD. No inertia.
    >
    > Stock market data is often smoothed out with a simple moving
    > average. But since the stamp cannot divide faithfully, I was
    > wondering if an exponential smoothing function might work better.
    >
    > I know these exponential smoothing functions exist, but
    > can't remember how they look or how they work. Or if they
    > require division.
    >
    > Thank you for any insights on how to smooth the stream of
    > numbers coming in from the impeller.
    >
    > Michael
    >
    >
    >
    >
    > 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 2002-03-25 14:54
    Stamp/serial LCD guru (and all-around nice guy) Scott Edwards taught me a
    simple technique a long time ago that I've used in these situations. You can
    create a simple digital filter by adding a fraction of the current reading to
    a fraction of the previous reading. Like this:

    GOSUB Get_Tach ' put current valu into thisReading

    thisReading = (thisReading */ $00B3) + (lastReading */ $004D)
    lastReading = thisReading

    You can change the response by changing the percentages (swayed toward
    current reading make it fast, toward last reading makes it slow).

    In the equation above, 70% of the current reading is added to 30% of the
    previous reading. If you've never used the */ (star-slash) operator, it
    works like this: it mutliplies by a fractional value expressed in 1/256. To
    find the parameter, multiply your fractional value by 256.

    0.70 * 256 = 179.2 --> $00B3
    0.30 * 256 = 76.8 --> $004D

    A great place to learn about Stamp math is Dr. Tracy Allen's web site:

    http://www.emesystems.com/BS2index.htm

    -- Jon Williams
    -- Applications Engineer, Parallax


    In a message dated 3/25/02 8:04:17 AM Central Standard Time,
    michcg@m... writes:


    > Smoothing problem.
    >
    > For a tachometer I am building, I would like to experiment with
    > mathematical smoothing.
    >
    > The tach monitors the rps of an underwater impeller on a model sailboat.
    > It is a Hall switch circuit, and works nicely.
    >
    > But the numbers come fast and the trend jumps around a bit. With a VOM
    > display, the inertia inherent in the galvanometer movement tends to smooth
    > out the trend. But for the finished product I want to use an
    > LCD. No inertia.
    >
    > Stock market data is often smoothed out with a simple moving average. But
    > since the stamp cannot divide faithfully, I was wondering if an exponential
    > smoothing function might work better.
    >
    > I know these exponential smoothing functions exist, but can't remember how
    > they look or how they work. Or if they require division.
    >
    > Thank you for any insights on how to smooth the stream of numbers coming in
    > from the impeller.
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-25 17:27
    >Smoothing problem.
    >
    >For a tachometer I am building, I would like to experiment with
    >mathematical smoothing.
    >... But since the stamp cannot divide faithfully, I was wondering
    >if an exponential smoothing function might work better.
    >
    > I know these exponential smoothing functions exist, but can't
    >remember how they look or how they work. Or if they require division.
    >
    >Thank you for any insights on how to smooth the stream of numbers
    >coming in from the impeller.
    >
    >Michael

    I have an example of exponential smoothing posted at
    http://www.emesystems.com/BS2math5.htm
    It is important to scale up your raw numbers by a factor of at least
    10 or 16 (the more the better) before they are input to the filter.
    Otherwise strange numerical problems result from the roundoff
    errors. For example, without the up-scaling, there will be
    hysteresis--the filter will "stick" at low values.

    I recently posted a double precision version that works even with
    word size variables. I needed that to filter the output of an
    altimeter.

    Another effective algorithm is the median filter. It is good as the
    first stage filter on data that occasionally has extreme outliers due
    to shot noise. Outliers have a relatively large effect on the
    smoothing and windowed average filters, but they completely fall out
    of the median filter. That is why it is such a good pre-filter. It
    is a table based, that is, you have to store several samples in
    memory and discard the oldest when you add the newest. It is kind of
    cumbersome on the Stamp. But the URL has a tutorial on how to do it.

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    mailto:tracy@e...
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-25 21:32
    I built a petrol engine governor and use a different method. If the
    governor is used on old and worn engines there is a lot of elctrical noise
    and the readings are all over the place. I solved it with the following
    method:
    Each sample consists of six readings. If a reading varies by more than say
    15% from the average of the last sample I substitute the last sample average
    plus/minus 15%.

    If there are large sudden variations in the engine speed there is a lag
    between the output to the lcd and the actual reading.

    I tried many other methods but in my application this worked best
    espescially with a fast sampling rate
    Regards

    Peter Hofman


    Original Message
    From: "Michael Gianturco" <michcg@m...>
    To: <basicstamps@yahoogroups.com>
    Sent: Tuesday, March 26, 2002 1:06 AM
    Subject: [noparse][[/noparse]basicstamps] Smoothing math


    > Smoothing problem.
    >
    > For a tachometer I am building, I would like to experiment with
    mathematical smoothing.
    >
    > The tach monitors the rps of an underwater impeller on a model sailboat.
    It is a Hall switch circuit, and works nicely.
    >
    > But the numbers come fast and the trend jumps around a bit. With a VOM
    display, the inertia inherent in the galvanometer movement tends to smooth
    out the trend. But for the finished product I want to use an
    > LCD. No inertia.
    >
    > Stock market data is often smoothed out with a simple moving average. But
    since the stamp cannot divide faithfully, I was wondering if an exponential
    smoothing function might work better.
    >
    > I know these exponential smoothing functions exist, but can't remember
    how they look or how they work. Or if they require division.
    >
    > Thank you for any insights on how to smooth the stream of numbers coming
    in from the impeller.
    >
    > Michael
    >
    >
    >
    >
    > 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 2002-03-26 19:21
    Thank to all of you for your insights and experience on this problem.
    Best, Michael
Sign In or Register to comment.