2012-08-09 80 views
5

我寫的執行樣條插補代碼:的樣條插值的Python守點

x1 = [ 0., 13.99576991, 27.99153981, 41.98730972, 55.98307963, 69.97884954, 83.97461944, 97.97038935, 111.9661593, 125.9619292, 139.9576991, 153.953469 ] 
y1 = [ 1., 0.88675318, 0.67899118, 0.50012243, 0.35737022, 0.27081293, 0.18486778, 0.11043095, 0.08582272, 0.04946131, 0.04285015, 0.02901567] 

x = np.array(x1) 
y = np.array(y1) 

# Interpolate the data using a cubic spline to "new_length" samples 
new_length = 50 
new_x = np.linspace(x.min(), x.max(), new_length) 
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x) 

但在新的數據集生成new_xnew_y原始分被淘汰,只有第一個和最後一個值保持。我想保留原來的觀點。

+1

爲什麼你不做那麼'new_y = sp.interpolate.interp1d(x,y,kind ='立方體')(x)' – 2012-08-09 13:18:15

回答

6

Right,linspace將不會生成x中的任何值,但您傳遞給它的值(x.min()x.max())除外。

我沒有很大的活潑的答案,但這裏是一個辦法做到這一點:

# Interpolate the data using a cubic spline to "new_length" samples 
new_length = 50 
interpolated_x = np.linspace(x.min(), x.max(), new_length - len(x) + 2) 
new_x = np.sort(np.append(interpolated_x, x[1:-1])) # include the original points 
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x) 

該代碼使用:

  • np.linspace爲我們需要創造儘可能多的加分
  • np.append將加分陣列與原始點的組合x
  • np.sort把按順序組合陣列
+0

非常感謝!有效。 – Hellfish 2012-08-09 17:13:11