python中的有界圆弧插值 [英] bounded circular interpolation in python

查看:228
本文介绍了python中的有界圆弧插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My question is something similar to the question here. In simple term I have a time series angle data which is bounded between [0, 360]. I need to compute an iterpolation between measurements. Currently, I am using scipy.interpolate.interp1d. To make my question clear here is an example,

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方向变化.

this will result in [ 180., 180.]. However between time 2 and time 4 the angle changed from 359 to 1, that is only a 2 degree change and the interpolated value at 3 should have been 0. The angles are changing in CCW direction through time.

最后,我的问题是这个,

Finally, my question is this,

我可以使用任何标准模块来实现这一目标吗?

Is there any standard module that I can use to achieve this?

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

Just because I want to avoid custom method as much as possible!

推荐答案

每当您检测到跳跃时,只需添加360°补码,然后使用取模操作返回到第一个360度.例如:

Just add the 360° complement each time you detect there is a jump and revert back to the first 360 degrees by using the modulo operation. For example:

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°.

Remark, I did add a few extra values here, as otherwise there is no realistic way for np.unwrap to know in which direction the angle is increasing, and that is probably also how you know it is increasing in that way (the difference between consecutive values is less than 180° unless there's an actual discontinuity).

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

If however you really have data that makes angular jumps larger than 180° between 2 consecutive items, but you know the direction in which the angles are changing (e.g. CCW) and that it is changing monotonously, then you could detect it like so:

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中的有界圆弧插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆