medians for degree measurements
Bas
wegwerp at gmail.com
Mon Jan 25 11:16:28 EST 2010
On Jan 23, 1:09 am, Steve Howell <showel... at yahoo.com> wrote:
[snip problem with angle data wrapping around at 360 degrees]
Hi,
This problem is trivial to solve if you can assume that you that your
data points are measured consecutively and that your boat does not
turn by more than 180 degrees between two samples, which seems a
reasonable use case. If you cannot make this assumption, the answer
seems pretty arbitrary to me anyhow. The standard trick in this
situation is to 'unwrap' the data (fix > 180 deg jumps by adding or
subtracting 360 to subsequent points), do your thing and then 'rewrap'
to your desired interval ([0-355] or [-180,179] degrees).
In [1]: from numpy import *
In [2]: def median_degree(degrees):
...: return mod(rad2deg(median(unwrap(deg2rad(degrees)))),360)
...:
In [3]: print(median_degree([1, 2, 3, 4, 5, 6, 359]))
3.0
In [4]: print(median_degree([-179, 174, 175, 176, 177, 178, 179]))
177.0
If the deg2rad and rad2deg bothers you, you should write your own
unwrap function that handles data in degrees.
Hope this helps,
Bas
P.S.
Slightly off-topic rant against both numpy and matlab implementation
of unwrap: They always assume data is in radians. There is some option
to specify the maximum jump size in radians, but to me it would be
more useful to specify the interval of a complete cycle, so that you
can do
unwrapped_radians = unwrap(radians)
unwrapped_degrees = unwrap(degrees, 360)
unwrapped_32bit_counter = unwrap(overflowing_counter, 2**32)
More information about the Python-list
mailing list