0

I have a GPS track log, I have registered the heading of navigation (i.e. for every logged point I have trace the heading). As far as I observed, this heading vary between 0 and 360. So, in log we can find:

  1. 0.002
  2. 0.1
  3. 359.2
  4. 0.01

Consider now point 3 and 4. Obviously, the car hasn't perform a 360 rotation. It just moves few degree (0.81) on another heading trajectory. So the margin is not abs(359.2-0.01) or abs(0.01-359.2). I clearly need a more sophisticated way to compute the changing. I think that I need to compute abs(359.2-0.01)=359.19 and next 360-359.19=0.81. Can I consider this as my standard modus operandi to compute direction change? I'm on matlab, so maybe there's a way to do this directly?

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110

2 Answers2

2

This might do what you want:

mod(a-b+180, 360) - 180

Example:

>> f = @(a,b) mod(a-b+180, 360) - 180;
>> f(359.2, 0.01)
ans = -0.81000
>> f(0.01, 359.2)
ans =  0.81000
>> f(358.2, 359.2)
ans = -1
>> f(0.01, 1.01)
ans = -1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

The answer of Oli Chalesworth does what you want. There is also the matlab function unwrap that adds or subtracts 2π to minimize the relative angle between two points. The downside is that it expects the angle to be in radians.

original_angles=[.002 .1 359.2 .001];
smooth_angles=180/pi*unwrap(original_angles*pi/180)

gives smooth_angles =

    0.0020    0.1000   -0.8000    0.0010
mars
  • 774
  • 1
  • 13
  • 17