Numpy帮助计算旋转体的角速度

时间:2015-07-28 12:33:55

标签: python arrays numpy rotation scipy

我正在尝试计算旋转物体的角速度。根据数据,我在每个时间间隔都有对象的角度。例如。

  

Numpy Array = [5,85,185,270,355,10,75,170,250,345,25,...]

用于顺时针旋转,类似用于

  

逆时针旋转Numpy Array = [25,345,250,170,75,10,   355,270,185,85,5,......]。

因此,当我尝试计算角度之间的差异时,对于0到360的范围,反之亦然,我可以使用numpy.diff(),在这种情况下效果很好。

当我碰巧从360到0或者例如达到差异时。如图所示。 A,从355度到5度,我再也不能使用numpy.diff()了。 我必须使用条件语句,如:

        if ang[i+1]>280 and theta<80:
            new_theta=-1*((theta-ang[i+1])%360)

类似地,当旋转运动处于逆时针方向时,我使用类似于上面的条件来获得角度差(dTheta):

    elif ang[i+1]<80 and theta>280:
        new_theta=360%(theta-ang[i+1])

Fig. A shows rotating object in clockwise direction and Fig. B shows counter clockwise rotation.

对于一般情况,角度既不接近极限,即0或360.

new_theta=ang[i+1]-theta

那么是否有更好的方法来计算限制,从而快速有效地提供更好的结果?

我无法处理范围限制。而且我不确定scipy norm是否可以帮助到这里。

欢迎任何有关改善问题的建议。

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以将测量值转换为弧度,并在使用np.diff之前使用np.unwrap

例如,这是您的数据,以度为单位:

In [93]: d = np.array([5, 85, 185, 270, 355, 10, 75, 170, 250, 345, 25])

转换为弧度:

In [94]: theta = (np.pi/180) * d

In [95]: theta
Out[95]: 
array([ 0.08726646,  1.48352986,  3.22885912,  4.71238898,  6.19591884,
        0.17453293,  1.30899694,  2.96705973,  4.36332313,  6.02138592,
        0.43633231])

展开:

In [96]: u = np.unwrap(theta)

In [97]: u
Out[97]: 
array([  0.08726646,   1.48352986,   3.22885912,   4.71238898,
         6.19591884,   6.45771823,   7.59218225,   9.25024504,
        10.64650844,  12.30457123,  13.00270293])

计算差异:

In [98]: delta_theta = np.diff(u)

In [99]: delta_theta
Out[99]: 
array([ 1.3962634 ,  1.74532925,  1.48352986,  1.48352986,  0.26179939,
        1.13446401,  1.65806279,  1.3962634 ,  1.65806279,  0.6981317 ])

转换回度:

In [100]: delta_degrees = (180/np.pi) * delta_theta

In [101]: delta_degrees
Out[101]: array([ 80., 100.,  85.,  85.,  15.,  65.,  95.,  80.,  95.,  40.])

答案 1 :(得分:2)

如果您知道旋转总是在一个方向上,您可以计算差异,然后通过并修复错误。所以,例如:

>>> arr = np.array([5,85,185,270,355,10,75, 170, 250,345, 25])
>>> darr = np.diff(arr)
>>> print(darr)
[  80  100   85   85 -345   65   95   80   95 -320]
>>> darr[darr<0] += 360
[ 80 100  85  85  15  65  95  80  95  40]

这可能是最快的方法。

另一种方法是使用numpy.unwrap,它试图弄清楚你绕圈子去哪里。但是,它只适用于弧度,因此您需要将其转换为弧度,解包,然后将其转换为度:

>>> warr = np.rad2deg(np.unwrap(np.deg2rad(arr)))
>>> print(warr)
[   5.   85.  185.  270.  355.  370.  435.  530.  610.  705.  745.]
>>> dwarr = np.diff(warr)
>>> print(dwarr)
[  80.  100.   85.   85.   15.   65.   95.   80.   95.   40.]

在这个玩具示例中,它给出了相同的结果,但它并不总是如此,因为它并不假设旋转是在特定的方向上。这意味着如果旋转总是与我之前给出的简单方法方向相同,则更容易出错。

如果你不能假设轮换总是在一个特定的方向,那么就没有好的客观方法来做到这一点。 np.unwrap可能是你最好的选择,但这只是猜测。没有确切的方法可以告诉另一个方向的大跳跃在一个方向上的小跳跃。