2013-01-09 105 views
9

我有一個由(x,y)對的序列組成的軌跡。我想用spline在這個軌跡上插入點。scipy:內插軌跡

我該怎麼做?使用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() 

example data

+0

@alinsoar:示例中的(x,y)數據沒有做到? – Nikratio

回答

11

使用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分和不阿爾法和r paramters。 Same as yours only using x and y points.

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