SciPy interp1d 结果与 MatLab interp1 不同

发布于 2024-12-17 09:43:50 字数 1236 浏览 2 评论 0原文

我正在将 MatLab 程序转换为 Python,但我无法理解为什么 scipy.interpolate.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 程序应该使用线性插值,所以这个问题可能没有实际意义。

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.

In MatLab the usage is slightly different:

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

SciPy:

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

For a trivial example the results are the same:
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])

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:

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

Any thoughts as to how I can get results that are consistent with 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2024-12-24 09:43:50

scipy.interpolate.interp1dinterp1 的底层插值方法不同。 Scipy 使用 netlib fitpack 例程,它生成标准的 C2 连续三次样条。 interp1 中的“cubic”参数使用分段三次 Hermite 插值多项式,该多项式不是 C2 连续的。有关 Matlab 功能的说明,请参阅此处

我怀疑这就是您所看到的差异的根源。

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.

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

差↓一点笑了 2024-12-24 09:43:50

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

因此,对于每个部分,(x,y)由您传递,pchip算法将计算(x,dy / dx),并且只有三次方经过2个点,并且在这些点处具有已知的导数。根据构造,它将继续具有连续的一阶导数。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文