Shop Learn P1 Docs P2 Docs
find mean average of binary numbers — Parallax Forums

find mean average of binary numbers

I have a wind direction indicator that uses 16 8 bit binary numbers to represent directions. Each of the 16 has 1 or 2 bits set and all others at zero. To make this easier to read, I'd like to take multiple samples and display the average. I know numerical averaging does not work. What is another way to do this?
Aaron

Comments

  • JonnyMacJonnyMac Posts: 8,163

    I've had a long week and I'm wiped out, but here's an idea: after N accumulations, do a simple linear interpolation between the highest and next highest direction accumulators.

  • YanomaniYanomani Posts: 1,507
    edited 2022-07-30 01:51

    @AGCB said:
    I have a wind direction indicator that uses 16 8 bit binary numbers to represent directions. Each of the 16 has 1 or 2 bits set and all others at zero. To make this easier to read, I'd like to take multiple samples and display the average. I know numerical averaging does not work. What is another way to do this?
    Aaron

    You're talking about Cardinal directions, and since you didn't mention any wind speed measurements, they'll need to be treated as unit vectors.

    There will be a total of 16 meaningful possibilities for the angles, between 0 and 360 degrees, spaced 22.5º appart.

    Any mean value of such kind of dataset would only be possible to compute if you associate individual time stamps with each measurement, otherwise it'll be not possible to proceed any calculations meaningful reports, at all.

    webmet.com/met_monitoring/6.html

    Hope it helps

    Henrique

    P.S. Be sure to have a reference direction, such as North, checked with a good compass...

  • Each of the 16 has 1 or 2 bits set and all others at zero.

    When and how do those bits get set?

    -Phil

  • I’m curious to what are using as a sensor, like a wind vane mounted on a 360 continuous pot. Or several switches on multiple compass headings

  • AGCBAGCB Posts: 306

    The DIY sensor is a stationary ring of 8 magnetic switches. These are closed if a rotating magnet is over them or 2 are closed if magnet is between them.

    I think I've found a way to average by converting (with a case table) each binary to its equivalent number of degrees on a compass. Then normal numerical averaging works. I will use 'limit minimum/maximum' to covert back to cardinal directions to display on screen.

    This is just for fun with my home-made weather monitor and need not be exact (as if weather can ever be exact regardless of equipment).

    Thanks for your input which piqued my brain.

    Aaron

  • Oscillations about North (337.5° to 0°, for example) will be an exceptional case that you'll have to deal with, since simple numerical averaging won't work.

    -Phil

  • This is really quite simple, I'm guessing your doing this in spin. Set up 16 ea. pins for input, "Dira[0]:=0" for pin 0, as example. name 16 ea. long variable such as DI0,DI1, etc. In a loop assign each varible a bit weight, such as DI0:=1, DI1:=2, DI2:=4, DI3:=8, DI4:=16, etc. So the loop would say "IF ina[0]==1 DI0:=1" have variable named "Direction", then in the" If " loop write"Direction:=DI0+DI1+DI2 etc.". Now when ever the switch closes it will give you a total bit

  • AGCBAGCB Posts: 306

    @"Phil Pilgrim (PhiPi)" said:
    Oscillations about North (337.5° to 0°, for example) will be an exceptional case

    Thanks Phil. I've realized that but not thought about it too much yet

    @DigitalBob said:
    This is really quite simple, I'm guessing your doing this in spin.

    Yes, SPIN and I didn't mention that I'm using a 74HC165 in the antenna top unit to generate the 8 bit numbers. My wife (not a programmer had suggested a similar thing.

    I actually have 2 of the antenna top units. The one up there now uses a MCP 23017 port expander (I2C), but the result is the same.

    Thanks to all.
    Aaron

  • You kind of lost me, what is an antenna top unit? Best thing to would be to post your spin code so everyone can have a look. And any pictures you want to share.

  • You could try something like this

  • AGCBAGCB Posts: 306

    @DigitalBob said:
    You kind of lost me, what is an antenna top unit?

    The wind vane mounted on antenna tower

  • AGCBAGCB Posts: 306

    I've lost my cheat sheet on how to post code in a forum message. Was it something like this?
    [code] program [code/]

    Aaron

  • Used to be. Now precede and follow by three backticks (```).

    -Phil

  • Another way you could do this would be to associate a 2D vector with each of the direction switches, take the average of all of the vectors, and compute the angle of the resulting vector. This would take care of oscillations around north and such. The magnitude of the vector would be smaller when the uncertainty/noise is higher.

  • AGCBAGCB Posts: 306

    @Electrodude said:
    Another way you could do this would be to associate a 2D vector

    That must be a C++ thing. I only do SPIN :)

  • Anything you can do in C++, you can do in Spin.

    -Phil

  • One accepted way to compute average wind direction is to average the sine and cosine of angle separately, then take the arctangent of the two averages.
    https://en.wikipedia.org/wiki/Yamartino_method
    The tough cases arise when the wind blows from everywhere, or, say, half from the south and half from the north.

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,347
    edited 2022-07-31 21:23

    The more I think about it, the less the mean direction means without the velocities associated with each reading. For example, it the wind blows at 10 mph from the East, then quells to 2 MPH from the North, what is the mean direction of those two readings? It's certainly not Northeast, since the East intensity was so much higher than that from the North. So extending Tracy's formula, average (actually just sum -- no need to average) the sine and cosine of each angle multiplied by the velocity, then take the arctangent of the result.

    This is equivalent to the cumulative direction of the wind track over a period of time.

    -Phil

  • I didn’t see any mention of a wind speed indicator. Looks like direction only switches, when the wind vane points south, the south switch closes. So hook that switch to your 180 degree pin on your prop etc. I don’t see a need for the port expander or serial to parallel. Did I miss something. For speed you could use a propeller (not a P1 or P2 , wood or plastic one) with a magnet on the shaft that triggers a reed switch. Then in spin write a rate counter for knots.

  • You could write direction values to the eeprom or flash card

  • AGCBAGCB Posts: 306

    @DigitalBob said:
    I didn’t see any mention of a wind speed indicator.

    This is a hobby weather monitor. One of three that I've built.
    It does have wind speed along with recent gusts. Also, inside (4) and outside temperatures (5, including 2 ground temps @ 8 and 30") with hi's and lo's , and barometer all displayed on a VGA monitor.

    As I said, it's just for fun and learning how to use Propeller 1.

    It's now working to my satisfaction with the way I mentioned in post# 6 with a few tweaks.

    Thanks.
    Aaron

  • If the display permits it, a wind rose is most informative.
    https://en.m.wikipedia.org/wiki/Wind_rose
    That works whether it’s direction only or full vector velocity.
    When I lived in Battle Creek MI, we could tell the wind direction by looking at whether or not the old guy across the street was sitting on his porch. One way was Kelloggs corn flakes, the other way was Purina dog chow. Similar concerns affect public health, like, are we currently upwind or downwind of the volcano, or the refinery.

  • AGCBAGCB Posts: 306

    Thanks Tracy
    No room for something like that. On a previous program I did have a visual compass display. I sometime take these things apart just to start over and learn new things. ;)
    Aaron

Sign In or Register to comment.