简体   繁体   English

scipy:插值轨迹

[英]scipy: Interpolating trajectory

I have a trajectory formed by a sequence of (x,y) pairs.我有一个由一系列(x,y)对形成的轨迹。 I would like to interpolate points on this trajectory using splines.我想使用样条在这条轨迹上插入点。

How do I do this?我该怎么做呢? Using scipy.interpolate.UnivariateSpline doesn't work because neither x nor y are monotonic.使用scipy.interpolate.UnivariateSpline不起作用,因为xy都不是单调的。 I could introduce a parametrization (eg length d along the trajectory), but then I have two dependent variables x(d) and y(d) .我可以引入参数化(例如沿轨迹的长度d ),但是我有两个因变量x(d)y(d)

Example:例子:

import numpy as np
import matplotlib.pyplot as plt
import math

error = 0.1
x0 = 1
y0 = 1
r0 = 0.5

alpha = np.linspace(0, 2*math.pi, 40, endpoint=False)
r = r0 + error * np.random.random(len(alpha))
x = x0 + r * np.cos(alpha)
y = x0 + r * np.sin(alpha)
plt.scatter(x, y, color='blue', label='given')

# For this special case, the following code produces the
# desired results. However, I need something that depends
# only on x and y:
from scipy.interpolate import interp1d
alpha_i = np.linspace(alpha[0], alpha[-1], 100)
r_i = interp1d(alpha, r, kind=3)(alpha_i)
x_i = x0 + r_i * np.cos(alpha_i)
y_i = x0 + r_i * np.sin(alpha_i)
plt.plot(x_i, y_i, color='green', label='desired')

plt.legend()
plt.show()

示例数据

Using splprep you can interpolate over curves of any geometry.使用 splprep,您可以在任何几何图形的曲线上进行插值。

from scipy import interpolate
tck,u=interpolate.splprep([x,y],s=0.0)
x_i,y_i= interpolate.splev(np.linspace(0,1,100),tck)

Which produces a plot like the one given, but only using the x and y points and not the alpha and r paramters.这会产生一个像给定的图,但只使用 x 和 y 点而不是 alpha 和 r 参数。和你的一样,只使用 x 和 y 点。

Sorry about my original answer, I misread the question.对不起,我的原始答案,我误读了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM