2011-11-21 152 views
9

我正在將MatLab程序轉換爲Python,並且遇到問題,理解爲什麼scipy.interpolate.interp1d給出的結果與MatLab interp1不同。SciPy interp1d結果不同於MatLab interp1

在Matlab中使用稍有不同:

yi = interp1(x,Y,xi,'cubic') 

SciPy的:

f = interp1d(x,Y,kind='cubic') 
yi = f(xi) 

對於一個簡單的例子的結果是相同的: MatLab的:

interp1([0 1 2 3 4], [0 1 2 3 4],[1.5 2.5 3.5],'cubic') 
    1.5000 2.5000 3.5000 

的Python:

interp1d([1,2,3,4],[1,2,3,4],kind='cubic')([1.5,2.5,3.5]) 
    array([ 1.5, 2.5, 3.5]) 

但對於一個真實的例子,他們是不一樣的:

x = 0.0000e+000 2.1333e+001 3.2000e+001 1.6000e+004 2.1333e+004 2.3994e+004 
Y = -6 -6 20 20 -6 -6 
xi = 0.00000 11.72161 23.44322 35.16484... (2048 data points) 

Matlab的:

-6.0000e+000 
-1.2330e+001 
-3.7384e+000 
    ... 
7.0235e+000 
7.0028e+000 
6.9821e+000 

SciPy的:

array([[ -6.00000000e+00], 
     [ -1.56304101e+01], 
     [ -2.04908267e+00], 
     ..., 
     [ 1.64475576e+05], 
     [ 8.28360759e+04], 
     [ -5.99999999e+00]]) 

有什麼想法,我怎麼能得到與MatLab一致的結果?

編輯:據我所知,在實現三維插值算法時有一些緯度,這可能是我看到的差異的原因。似乎我正在轉換的原始MatLab程序應該使用線性插值,所以問題可能是沒有意義的。

回答

11

scipy.interpolate.interp1dinterp1不同的基礎插值方法。 Scipy使用netlib fitpack例程,該例程產生標準的C2連續立方樣條。 interp1中的「立方體」參數使用分段立方厄米插值多項式,它不是C2連續的。請參閱here以瞭解Matlab所做的工作。

我懷疑這是你所看到的差異的根源。