Math help - rolling average Wind Direction


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116

    Default Math help - rolling average Wind Direction

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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.
    Dave
    Always wear safety glasses while programming.

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wjsmarine View Post
    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...

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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...

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    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...
    Last edited by skimask; - 6th July 2007 at 17:22.

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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?

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Melanie taps her fingers impatiently whilst eyeing up her whip coiled on the wall, then scribbles...

    Code:
    	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?

Similar Threads

  1. Replies: 10
    Last Post: - 8th April 2008, 21:07
  2. Math help please!!!
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th August 2007, 14:45

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts