rth*_*rth 4 python gis interpolation numpy scipy
给定一组描述 2D 平面中某些轨迹的点,我想通过局部高阶插值提供该轨迹的平滑表示。
例如,假设我们在下图中定义了一个具有 11 个点的 2D 圆。我想按顺序在每对连续点之间添加点或产生平滑的轨迹。在每个线段上添加点很容易,但它会产生“局部线性插值”典型的斜率不连续性。当然这不是经典意义上的插值,因为
y
对于给定的函数可以有多个值x
所以我不确定什么是合适的词汇。
生成该图的代码可以在下面找到。使用该lin_refine_implicit
函数执行线性插值。我正在寻找一个更高阶的解决方案来产生平滑的轨迹,我想知道是否有一种方法可以使用 Scipy 中的经典函数来实现它?我尝试使用各种一维插值,但scipy.interpolate
没有取得太大成功(同样是因为y
给定的多个值x
)。
最终目标是使用这种方法通过离散测量提供平滑的 GPS 轨迹,所以我认为这应该在某个地方有一个经典的解决方案。
import numpy as np
import matplotlib.pyplot as plt
def lin_refine_implicit(x, n):
"""
Given a 2D ndarray (npt, m) of npt coordinates in m dimension, insert 2**(n-1) additional points on each trajectory segment
Returns an (npt*2**(n-1), m) ndarray
"""
if n > 1:
m = 0.5*(x[:-1] + x[1:])
if x.ndim == 2:
msize = (x.shape[0] + m.shape[0], x.shape[1])
else:
raise NotImplementedError
x_new = np.empty(msize, dtype=x.dtype)
x_new[0::2] = x
x_new[1::2] = m
return lin_refine_implicit(x_new, n-1)
elif n == 1:
return x
else:
raise ValueError
n = 11
r = np.arange(0, 2*np.pi, 2*np.pi/n)
x = 0.9*np.cos(r)
y = 0.9*np.sin(r)
xy = np.vstack((x, y)).T
xy_highres_lin = lin_refine_implicit(xy, n=3)
plt.plot(xy[:,0], xy[:,1], 'ob', ms=15.0, label='original data')
plt.plot(xy_highres_lin[:,0], xy_highres_lin[:,1], 'dr', ms=10.0, label='linear local interpolation')
plt.legend(loc='best')
plt.plot(x, y, '--k')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('GPS trajectory')
plt.show()
Run Code Online (Sandbox Code Playgroud)
这称为参数插值。
scipy.interpolate.splprep为此类曲线提供样条近似值。这假设您知道曲线上点的顺序。
如果您不知道曲线上哪个点在哪个点之后,问题就会变得更加困难。我认为在这种情况下,问题称为流形学习,scikit-learn 中的一些算法可能对此有所帮助。