How to subtract wind direction between 0-359 degrees?

7 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Adam Danz
Adam Danz il 26 Ago 2020
Modificato: Adam Danz il 28 Ago 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

Più risposte (0)

Prodotti


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by