SciPy interp1d结果与MatLab interp1不同

时间:2021-07-31 18:15:23

I'm converting a MatLab program to Python, and I'm having problems understanding why scipy.interpolate.interp1d is giving different results than MatLab interp1.

我正在把一个MatLab程序转换成Python,我在理解为什么scipy. insert。interp1d的结果与MatLab的interp1不同。

In MatLab the usage is slightly different:

在MatLab中用法略有不同:

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

SciPy:

SciPy:

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

For a trivial example the results are the same: MatLab:

对于一个微不足道的例子,结果是相同的: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:

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])

But for a real-world example they are not the same:

但在现实世界中,它们是不一样的:

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:

Matlab:

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

SciPy:

SciPy:

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

Any thoughts as to how I can get results that are consistent with MatLab?

对于如何得到与MatLab一致的结果有什么想法吗?

Edit: I understand that there is some latitude in implementation for cubic interpolation algorithms which probably accounts for the differences I'm seeing. It also seems that the original MatLab program that I am converting should have used linear interpolation, so the question is probably moot.

编辑:我理解在实现三次插值算法中有一定的*度,这可能是我所看到的不同之处。我正在转换的原来的MatLab程序似乎也应该使用线性插值,所以这个问题可能没有意义。

2 个解决方案

#1


11  

The underlying interpolation method that scipy.interpolate.interp1d and interp1 are different. Scipy uses the netlib fitpack routines, which yields standard, C2 continuous cubic splines. The "cubic" argument in interp1 uses piecewise cubic hermite interpolating polynomials, which are not C2 continuous. See here for an explanation of what Matlab does.

插补法与插补法是不同的。Scipy使用netlib fitpack例程,生成标准的C2连续三次样条。interp1中的“立方”参数使用分段立方埃尔米特插值多项式,它不是C2连续的。请看这里,了解Matlab的功能。

I suspect that is the source of the difference you are seeing.

我怀疑这就是你所看到的不同之处。

#2


0  

In current scipy use http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PchipInterpolator.html This will create monotonic cubic interpolation of the y=f(x) passed, and uses the pchip algorithm to determine the slopes in the points.

在当前的scipy中,使用http://docs.scipy.org/doc/scipy/reference/generated/scipy.interrupate.pchipator.html这将创建传递的y=f(x)的单调三次插值,并使用pchip算法确定点中的斜率。

So for every section, (x,y) is passed by you, the pchip algorithm will calculate (x,dy/dx), and there is only cubic going through 2 points with known derivative at these points. Per construction, it will continuous with continuous first derivative.

所以对于每一部分,(x,y)都经过你,pchip算法会计算(x,dy/dx),在这些点上只有三次经过两个已知导数的点。每构造一次,它都是连续的一阶导数。

#1


11  

The underlying interpolation method that scipy.interpolate.interp1d and interp1 are different. Scipy uses the netlib fitpack routines, which yields standard, C2 continuous cubic splines. The "cubic" argument in interp1 uses piecewise cubic hermite interpolating polynomials, which are not C2 continuous. See here for an explanation of what Matlab does.

插补法与插补法是不同的。Scipy使用netlib fitpack例程,生成标准的C2连续三次样条。interp1中的“立方”参数使用分段立方埃尔米特插值多项式,它不是C2连续的。请看这里,了解Matlab的功能。

I suspect that is the source of the difference you are seeing.

我怀疑这就是你所看到的不同之处。

#2


0  

In current scipy use http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PchipInterpolator.html This will create monotonic cubic interpolation of the y=f(x) passed, and uses the pchip algorithm to determine the slopes in the points.

在当前的scipy中,使用http://docs.scipy.org/doc/scipy/reference/generated/scipy.interrupate.pchipator.html这将创建传递的y=f(x)的单调三次插值,并使用pchip算法确定点中的斜率。

So for every section, (x,y) is passed by you, the pchip algorithm will calculate (x,dy/dx), and there is only cubic going through 2 points with known derivative at these points. Per construction, it will continuous with continuous first derivative.

所以对于每一部分,(x,y)都经过你,pchip算法会计算(x,dy/dx),在这些点上只有三次经过两个已知导数的点。每构造一次,它都是连续的一阶导数。