簡體   English   中英

scipy:插值軌跡

[英]scipy: Interpolating trajectory

我有一個由一系列(x,y)對形成的軌跡。 我想使用樣條在這條軌跡上插入點。

我該怎么做呢? 使用scipy.interpolate.UnivariateSpline不起作用,因為xy都不是單調的。 我可以引入參數化(例如沿軌跡的長度d ),但是我有兩個因變量x(d)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,您可以在任何幾何圖形的曲線上進行插值。

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 參數。和你的一樣,只使用 x 和 y 點。

對不起,我的原始答案,我誤讀了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM