How to subtract wind direction between 0-359 degrees?
6 views (last 30 days)
Show older comments
Say I have two wind sensor measuring wind speed and direction. For speed, I can easily subtract sensor A from sensor B to determine how far apart they are. However, wind direction I find tricky because the “scale” wraps around when 359 goes to 360 (also 0 degrees). If sensor A gets a wind direction of 10 degrees and sensor B measures 350 degrees, they’re really only apart by 20 degrees. A simple subtraction would’ve called them apart by 340 degrees. How can I communicate to Matlab to perform a comparison of wind direction with this kind of compass scale?
0 Comments
Accepted Answer
Adam Danz
on 26 Aug 2020
Edited: Adam Danz
on 28 Aug 2020
If you're frequently working with circular value, I recommend using the Circular Statistics Toolbox from the file exchange.
For example, the distance between 15 deg and 350 deg is
>> rad2deg(circ_dist(deg2rad(15), deg2rad(350)))
ans =
25
>> rad2deg(circ_dist(deg2rad(350), deg2rad(15)))
ans =
-25
If you have Matlab's Robotics Systems Toolbox, you could use angdiff(). Note the opposite sign of the outputs compared to circ_dist().
>> rad2deg(angdiff(deg2rad(15),deg2rad(350)))
ans =
-25
>> rad2deg(angdiff(deg2rad(350),deg2rad(15)))
ans =
25
Compare these to Matlab's wrapTo360() which only wraps values between [0,360] so 350-15 would not indicate the closest circular distance.
>> wrapTo360(15-350)
ans =
25
>> wrapTo360(350-15)
ans =
335
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!