scipy:插补轨迹 [英] scipy: Interpolating trajectory

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

问题描述

我有一个由一系列(x,y)对形成的轨迹.我想使用样条线在此轨迹上插值.

I have a trajectory formed by a sequence of (x,y) pairs. I would like to interpolate points on this trajectory using splines.

我该怎么做?使用scipy.interpolate.UnivariateSpline不起作用,因为 x y 都不是单调的.我可以引入参数化(例如,沿着轨迹的长度 d ),但是我有两个因变量 x(d) y(d).

How do I do this? Using scipy.interpolate.UnivariateSpline doesn't work because neither x nor y are monotonic. I could introduce a parametrization (e.g. length d along the trajectory), but then I have two dependent variables x(d) and y(d).

示例:

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()

推荐答案

使用splprep,您可以在任何几何形状的曲线上进行插值.

Using splprep you can interpolate over curves of any geometry.

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)

这将产生与给定的图类似的图,但仅使用x和y点,而不使用alpha和r参数.

Which produces a plot like the one given, but only using the x and y points and not the alpha and r paramters.

很抱歉,我原来的答案是我读错了问题.

Sorry about my original answer, I misread the question.

这篇关于scipy:插补轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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