0

I am trying to write a script in python to solve the following issue:

when I plot some angle trajectory, I get jumps due to the fact that the point I am tracking sometimes it 'jumps' outside the range [0, 360]. Given that these jumps are fairly easy to identify (see plot in the link at the end of the post), I was trying to write a function in python that takes as input the list of angles, and when it identifies jumps >300 it will subtract 360 from the following values of the list until it finds the second jump. Here is my attempt to code this, but unfortunately I am running an infinite loop. Does anyone have any suggestions?

EDIT: I added a list with some of the values just as an example

alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80,  2.74, 354.55, 354.13, 357.82,  3.86, 2.42, 3.57, 1.57, 357, 358]


#take derivative of list of angles alpha
alpha_diff = [0, np.diff(alpha)]
#iterate over each item in alpha_diff and when it finds jumps <-360 you will add 360 until it finds the next jump
for i in alpha_diff:
    if alpha_diff[i]<-300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]+360
#similarly if finds jumps > 360 you will subtract 360 until it finds the next jump
    if alpha_diff[i]>300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]-360 
    else:
        alpha[i] = alpha

enter image description here

Leos313
  • 3,729
  • 4
  • 29
  • 61
Lukas_Skog
  • 27
  • 4
  • easy to be solved with the function modulo `%`. – Leos313 May 13 '20 at 07:51
  • I tried to use modulo function as you recommended, but I don't think it can work in this case, in fact it returns me exactly the same values of angles without correcting for the jumps – Lukas_Skog May 13 '20 at 13:30
  • None of the values in your example exceed the `-360 .. 360` range you're testing for... And why are you testing for `-360` instead of `0` like your textual description? – Attie May 13 '20 at 13:36
  • 1
    You'll also find that `alpha_diff` is `[ 0, array([ ... ]) ]` not `[ 0, ... ]`... try `alpha_diff = [ 0, *np.diff(alpha) ]` – Attie May 13 '20 at 13:37
  • there you have your example. You were right, the modulo did not work properly. However, there is the built-in function `unwrap`, like in MatLab! :) This works 100% because I have tested it! – Leos313 May 14 '20 at 08:39
  • remember also to vote up if the answer was useful – Leos313 May 14 '20 at 11:00
  • `alpha_diff` is a list with two elements, and `for i in alpha_diff:` is not how you iterate over it by index – Mad Physicist May 18 '20 at 12:42
  • @MadPhysicist, the solution to the question does not involve `alpha_diff`. The author of the question was trying to propose a solution to the stated problem. However, a built-in solution already exists in Python and Matlab as well. You are right, however, the question should be re-open for me. It makes sense to edit the question and deleting the solution proposed by the author as an example: it does not work – Leos313 May 18 '20 at 15:16

1 Answers1

1

Ok, you are looking for unwrap. Before you need a degree-to-radiant conversion. Here an example using your data:

import matplotlib.pyplot as plt
import numpy as np
from math import pi

alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80,  2.74, 354.55, 354.13, 357.82,  3.86, 2.42, 3.57, 1.57, 357, 358]
alpha_rad = [x*pi/180 for x in alpha]
alpha_unwrap= np.unwrap(alpha_rad)

Then, if you plt.plot(alpha) you obtain:

enter image description here

When plt.plot(alpha_rad)

enter image description here

Finally, when plt.plot(alpha_unwrap):

enter image description here

Leos313
  • 3,729
  • 4
  • 29
  • 61