PDA

View Full Version : Math help - rolling average Wind Direction



wjsmarine
- 6th July 2007, 13:16
Hi Members,

I need some advice on how to create a rolling 3 second average of wind direction.

So far: a sample is taken each second (AD count) and added/averaged until an interrupt causes the value to be sent serially at which time the values are reset and the cycle continues.

Basic enough but the problem arises where prevailing conditions see the wind coming from a Northerly aspect where one sample may be less than 359 degrees while the next (or previous) is greater than 0. Normal averaging would see two consecutive samples (say 350 and 10 degrees) as 180 but this is obviously incorrect, it should be 0 (360) degrees.

I've pulled a great deal of hair out trying to nail this but have hit a brick wall - any suggestions or advice would be most welcome. PIC of choice is F88 so codespace isn't an issue at this stage.

Regards,
Bill

mackrackit
- 6th July 2007, 14:46
Hi,

In the example you gave do something like if a sample is greater than 330 and the other sample is less than 30. Add 180 to your final result.

There might be conditions where the above will not work. This is just a quick thought.

skimask
- 6th July 2007, 14:57
Basic enough but the problem arises where prevailing conditions see the wind coming from a Northerly aspect where one sample may be less than 359 degrees while the next (or previous) is greater than 0. Normal averaging would see two consecutive samples (say 350 and 10 degrees) as 180 but this is obviously incorrect, it should be 0 (360) degrees.
Regards,
Bill

Off the top of my head...how about something like taking the sin & cos of each angle sample, averaging those numbers, and then converting the sin & cos average result back to an angle. I don't remember what that's called at the moment, sum of the squares, something along those lines...

Melanie
- 6th July 2007, 16:24
You're thinking linearly... stop it!

Degrees are a circular problem, with those you have to treat zero (ie zero degrees) as a centre-point and then 0-180 is treated as Positive, and 359-181 degrees are treated as -1 thru to -179. Ignore the fact that 'theoretically' PBP can't handle negatives... it can... if you're still stuck with the clue I've left you, come back and we'll progress the logic as there's a heap of ways to approach this...

Darrel Taylor
- 6th July 2007, 16:56
Maybe I'm thinking linearly too, but doesn't that just move the problem from North to South?

At a little SE its 179 or less, and move a little SW and it's -179 or more, which averages to 0 or North.

Maybe there's another trick in that hint?
<br>

skimask
- 6th July 2007, 17:19
Maybe I'm thinking linearly too, but doesn't that just move the problem from North to South?
At a little SE its 179 or less, and move a little SW and it's -179 or more, which averages to 0 or North.
Maybe there's another trick in that hint?
<br>

What I'm thinkin...(without colons this time :) )
1st reading is 30 degrees, 2nd reading is 330 degrees, average wanted should be 0 degrees, obviously the AVERAGE is 180...no good...but...
if you take the sin and cos of 30, you get .5 and .866...
if you take the sin and cos of 330, you get -.5 and .866...
the average of the .5 and -.5 is 0, sin(0)=0 (average of .5 & -.5)...
but I'm stuck at what to do with the 2 cosine values. Maybe this example is just a 'special case' where the result is right one 'axis' lines of a graph...
More thought needed...I know the answer is right here...just have to put my finger on it...

Or maybe you could treat the first 30 degree reading as actually 390 (360 + 30), then do the averaging, which would give you the right answer of 360, subtract 360 from the result and you get the answer...

Melanie
- 6th July 2007, 17:32
Another prod...

You have two values, if the sum of the two values (each in the range 0-180, and ignoring if they're positive or negative) are equal to or greater than 180, then simply change the reference point.

Alternatively, how about this simplest solution... forget North, South or whatever, your reference point 'floats' - it's one of your two values (each 0-359)... ValueB is X degrees away from ValueA. So simply halve the difference and always add to ValueA. Note, that your choice of which value is ValueA is important... but then I'd be giving it away... once again it's something about if the difference is greater than 180...

Well there's two potential solutions... need any more?

Melanie
- 6th July 2007, 18:29
Melanie taps her fingers impatiently whilst eyeing up her whip coiled on the wall, then scribbles...



ValueA var Word
ValueB var Word
ValueC var Word
Result var Word
If ValueA < ValueB then Swap ValueA,ValueB
ValueC=ValueA-ValueB
If ValueC < 180 then
ValueC=ValueC/2
Result=ValueB+ValueC
else
ValueC=360-ValueC
ValueC=ValueC/2
Result=ValueA+ValueC
endif
If Result = > 360 then Result=Result-360


There is only one 'anomaly' which could be adjusted whichever way you choose (although there is no true definitive answer). If ValueA and ValueB are exactly 180 degrees apart, you have two potential answers both of which are correct... eg ValueA is Zero and ValueB is 180, you have two answers - one being 90 and the other being 270. My little ditty above always selects the higher - 270 in this case. But what the hell do you expect for 30 seconds work?

Darrel Taylor
- 6th July 2007, 18:58
Or, here's another possibility...
Direction VAR WORD ; Holds the Averaged wind direction
Sample VAR WORD
AvgCount CON 3 ; Number of samples to average

; Get New Sample here 0-359

If (Direction > 270) AND (Sample < 90) then
Sample = Sample + 360
else
if (Direction < 90) AND (Sample > 270) then
Direction = Direction + 360
endif
endif

Direction = ((Direction * (AvgCount-1) + Sample) / AvgCount) // 360
Which treats anything in the range of 0-90 as if it's 360-450. But only if the other value is on the other side of the zero point.

Then after averaging, it gets the Modulus of the result, which brings it back into the 0-359 range.

DavidK
- 7th July 2007, 18:33
If it is any help. The boys from Berkeley have an routine for averaging the anemometer direction. It is somewhat specific to their product, but the explanation may be of help. Has links to the math routines.

http://www.emesystems.com/OL2wind.htm#Wind_vane

Melanie
- 8th July 2007, 09:39
ooooh... I don't like bashing other folks work, but that is very amateurish... "Hello Mr Customer. Here's a fine wind direction guage for your airfield - oh, by the way, (at best) it doesn't work 3% of the time..."

RussMartin
- 14th July 2007, 07:34
I had to deal with aspects of this when I was working with weather and water instrumentation back in 1976. At the time, I was with the Wyoming Water Resources Research Institute. We dealt with an enormous volume of meteorological and hydrological data.

A side note for/about the "Boys from Berkeley"--there's no reason for a "dead band" on a weather vane and hasn't been for several decades. (That isn't "bashing", is it, Melanie?)

For clarity, let's agree that a vane tells us direction and an anemometer tells us that air is moving, and how fast--that there is in fact wind!

A wind direction change of exactly 180 degrees in that short a period (3 seconds) is frankly unlikely unless the vane is spinning wildly and sampled at the wrong instant, or just turning lazily, moving almost at random. Most occurrences will be in very light, almost insignificant breezes (hence little or no wind speed), so direction doesn't really obtain.

I would "trap" such a condition by looking at the absolute (i.e., unsigned) value of the difference of every successive pair of readings. If it is 180, it's a null (i.e., useless and/or anomalous); discard the earliest value and use the second against the incoming third, and so forth--truly "rolling" the values through:

If |oldest - next| = 180, then oldest = next and next = newest, then start over; else, average the data and continue. (Melanie can probably write the code in about 15 seconds!)

What is overlooked in this discussion is the fact that wind "direction" means nothing if there is no air movement, i.e., no wind speed. Much more important than a possible but unlikely 180-degree anomaly is if the vane is simply sitting there in absolutely still air. It will continue to report a direction! This would be like your television weather reporter saying, "The wind is coming from the east at zero miles per hour." Huh?

RussMartin
- 15th July 2007, 07:21
For the curious, wind data is often expressed graphically, in the form of a wind rose.

The compass is divided into 8, 16, 32, or sometimes more, equal segments. 16 is the most common.

<IMG SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1857&d=1184480481">

wjsmarine
- 16th July 2007, 13:22
Hi Folks,

Thanks to all for comments and suggestions, I'm still trying various ways to come up with one solution for all.

Russ, you make good points with your Direction without Wind speed comments. I also have a long history of Oceanographic and Meteorological experience - but as a hardware guy. My venture into writing code has only been recent by comparison so I have the problem of putting the hardware experience into the Black Art... For the record, Tracey ("Boys from Berkeley") Allen's website has been a mine of useful information for me particularly with the math side of things and would encourage anyone regardless of their qualifications to spend some time reading some of his writings.

What I didn't mention in my opening question was that I am also measuring Wind Gust, which by definition, is the highest successive wind speed in a 3 second window - hence my need to also measure the direction over 3 seconds (my opening question). You can read between the lines here to see I am also deriving Highest wind speed, Average wind speed, Wind Gust direction, Average direction and Direction deviation (Sigma Theta) - so I have a number of things to juggle and think about, all within a continuously sliding 3 second window which is moving every 1 second.

I'm making progress but it isn't pretty code! If anyone has some more to input it will be warmly welcomed otherwise I'll keep banging away to streamline things.

Kind regards to all,
Bill

wjsmarine
- 24th July 2007, 11:59
Hi Folks,

Darrel, am I doing something wrong here with your math example?

When I substitute 350 for Direction and 20 for Sample (a compass difference of 30 resulting in an average of 5) I get:


Direction VAR WORD ; Holds the Averaged wind direction
Sample VAR WORD
AvgCount CON 3 ; Number of samples to average

; Get New Sample here 0-359

If (Direction > 270) AND (Sample < 90) then
Sample = Sample + 360
; so Sample now 380
else
if (Direction < 90) AND (Sample > 270) then
Direction = Direction + 360
endif
endif

Direction = ((Direction * (AvgCount-1) + Sample) / AvgCount) // 360
; becomes
= ((350*(3-1)+380)/3)//360
= ((350*2+380)/3)//360
= (700+380/3)//360
= (1080/3)//360
= 360//360
= 1

; and if swapped so Direction is 20 and Sample is 350
; then Direction now 380
; becomes
= ((380*(3-1)+350)/3)//360
= ((380*2+350)/3)//360
= (760+350/3)//360
= (1110/3)//360
= 370//360
= 1

Or am I missing something obvious..? Math is not my strong point :)

Cheers,
Bill

Darrel Taylor
- 24th July 2007, 12:18
Missed the Modulas.

360 // 360 = 0

and

370 // 360 = 10

Also, keep in mind it's using a 3 sample running average.
So starting at 350, and a sample of 20, the difference is 30 and it will move 1/3 of that, or 10.
So it ends up at 360(0) after 1 sample.
<br>

paul borgmeier
- 24th July 2007, 14:09
Just curious a bit ... Bill, what are you using to measure this direction 0 - 360?
Thanks

wjsmarine
- 24th July 2007, 15:14
Thanks Darrel, I'll take another look later (time for my beauty sleep).

Paul, it's a 360 degree precision linearity potentiometer fed with a regulated voltage. I use the PIC A2D to derive a count and then process the count for degrees.

Cheers,
Bill

Darrel Taylor
- 23rd July 2009, 00:08
Hi,

Just updating this thread with a possible solution to the whole "rolling average Wind Direction" problem...

http://www.picbasic.co.uk/forum/showthread.php?p=76205#post76205

Cheers,