1 x=[1.3889, 2.7778, 4.1667, 5.5556, 6.9444, 8.3333, 9.7222, 11.1111,12.500]; 2 y=[1.25, 2.40, 3.25, 4.00, 4.50, 5.50, 6.50, 7.00, 8.00]; 3 x_new=0:0.1:15; 4 y_1=spline(x,y,x_new); 5 plot(x,y,'o',x_new,y_1);title('spline fit');
样本插值
除了样本插值,还有nearest, zero,quadratic,cubic
都可以用numpy进行实现
用numpy实现的图像与matlab差不多,但是视觉上感觉numpy好看一些,而且用起来顺手
1 import numpy as np 2 from scipy import interpolate 3 import pylab as pl 4 5 x = np.array([ 6 1.3889, 2.7778, 4.1667, 5.5556, 6.9444, 8.3333, 9.7222, 11.1111,12.5000 7 ]) 8 y = np.array([ 9 1.25, 2.40, 3.25, 4.00, 4.50, 5.50, 6.50, 7.00, 8.00 10 ]) 11 12 xnew = np.linspace(0, 15, 101) 13 pl.plot(x, y, "ro") 14 15 for kind in ["nearest", "zero", "spline", "quadratic", "cubic"]: # 插值方式 16 # "nearest","zero"为阶梯插值 17 # "spline" 样条插值 18 # "quadratic","cubic" 为2阶、3阶B样条曲线插值 19 f = interpolate.interp1d(x, y, kind=kind, fill_value = 'extrapolate') 20 ynew = f(xnew) 21 pl.plot(xnew, ynew, label=str(kind)) 22 pl.legend(loc="lower right") 23 pl.show()
但是用numpy实现插值存在一个问题,无法采样!!!
所以在最后还是只好用matlab进行插值!
command windows:
>>pp = spline(x,y)