gpt4 book ai didi

python - python中的有界圆插值

转载 作者:太空狗 更新时间:2023-10-29 21:37:05 25 4
gpt4 key购买 nike

我的问题类似于问题 here .简单来说,我有一个时间序列角度数据,它在 [0, 360] 之间。我需要计算测量之间的迭代。目前,我正在使用 scipy.interpolate.interp1d .为了使我的问题清楚,这里有一个例子,

import numpy as np
from scipy import interpolate
data = np.array([[0, 2, 4], [1, 359, 1]]) # first row time index, second row angle measurements
f = interpolate.interp1d(data[0, :], data[1, :], kind='linear', bounds_error=False, fill_value=None)
f([1, 3])

这将导致 [ 180., 180.]。然而,在时间 2 和时间 4 之间,角度从 359 变为 1,即只有 2 度的变化,在 3 处的内插值应该为 0。角度随时间沿 CCW 方向变化。

最后,我的问题是,

是否有任何标准模块可用于实现此目的?

只是因为我想尽可能避免自定义方法!

最佳答案

每次检测到跳跃时只需添加 360° 补码,然后使用模运算恢复到第一个 360 度。例如:

In [1]: import numpy as np

In [2]: from scipy import interpolate

In [3]: data = np.array([[0, 2, 4, 6, 8], [1, 179, 211, 359, 1]])

In [4]: complement360 = np.rad2deg(np.unwrap(np.deg2rad(data[1])))

In [5]: complement360
Out[5]: array([ 1., 179., 211., 359., 361.])

In [6]: f = interpolate.interp1d(data[0], complement360, kind='linear', bounds_error=False, fill_value=None)

In [7]: f(np.arange(9))
Out[7]: array([ 1., 90., 179., 195., 211., 285., 359., 360., 361.])

In [8]: f(np.arange(9))%360
Out[8]: array([ 1., 90., 179., 195., 211., 285., 359., 0., 1.])

注意,我确实在这里添加了一些额外的值,否则 np.unwrap 没有现实的方法来知道角度在哪个方向增加,这可能也是你知道的它以这种方式增加(连续值之间的差异小于 180°,除非存在实际的不连续性)。

但是,如果您确实拥有使两个连续项目之间的角度跳跃大于 180° 的数据,但您知道角度变化的方向(例如 CCW)并且它正在单调变化,那么您可以检测到它所以:

In [31]: data = np.array([1, 359, 1, 60, 359, 177, 2])  # mock-data

In [32]: jumps = np.diff(data)<0 # assumptions: angle increases stricly monotonously CCW

In [33]: np.hstack((data[0], data[1:] + np.cumsum(np.sign(d)<0)*360))
Out[33]: array([ 1, 359, 361, 420, 719, 897, 1082])

关于python - python中的有界圆插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27295494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com