Creating arctan2 () continuously in 2pi

I am working with an acceleration dataset using python 2.7 to find the angle, I am using arctan2 (y, x). My problem is that although my data can rotate outside of pi, the output of arctan2 (y, x) is limited between pi and -pi. This means that whenever I go above pi, I suddenly have a sharp jump in my data and cycle to a negative pi.

I am trying to find an elegant solution to fix this and make my schedule continuous. I performed a simple check where, even if I jump more than 80% of the way, I assume that I went beyond and started adding 2 pi to each subsequent data point. It works, but it feels very awkward. Is there a more elegant way to implement this? Or is this the best I can do?

Thanks ^ _ ^

for index in range(1,(len(x_data))):
    new_angle = math.atan2((y_data[index]), (x_data[index]))
    if (new_angle - angle[index-1]) > 5:
        new_angle = new_angle - 6.28
    if (new_angle - angle[index-1]) < -5:
        new_angle = new_angle + 6.28
    angle.append(new_angle)

Green shows what I want, black shows what I get enter image description here

+4
source share
2 answers

What you are trying to do is usually called "lifting" ... the code that I usually use is

last = 0
out = []
for x, y in data:
    angle = math.atan2(y, x)
    while angle < last-math.pi: angle += 2*math.pi
    while angle > last+math.pi: angle -= 2*math.pi
    last = angle
    out.append(angle)

Two "while" loops can be compressed in a more mysterious

    angle = (angle-last + math.pi)%(2*math.pi)-math.pi + last
0
source

For plotting purposes, you can use the following algorithm:

  • Suppose you draw the last point in (x1, y1)

  • The new point must be at (x2,y2), where x2given, and y2 = atan2(...) + 2 * pi * kfor some integer k.

  • Find such kthat the distance from (x1,y1)to is (x2,y2)minimum

  • Use (x2,y2)as (x1,y1)for the next step.

+1
source

Source: https://habr.com/ru/post/1668160/


All Articles