11

我想通过 scipy 计算样条插值的系数。在 MATLAB 中:

x=[0:3];
y=[0,1,4,0];
spl=spline(x,y);
disp(spl.coefs);

它会返回:

ans =

   -1.5000    5.5000   -3.0000         0
   -1.5000    1.0000    3.5000    1.0000
   -1.5000   -3.5000    1.0000    4.0000

但是我不能通过 scipy 中的 interpolate.splrep 来做到这一点。能告诉我怎么计算吗?

4

4 回答 4

8

我不确定有什么方法可以从 scipy.xml 获得这些系数。给你scipy.interpolate.splrep的是b样条的结系数。Matlab 的样条曲线给你的似乎是描述连接你传入的点的三次方程的部分多项式系数,这让我相信 Matlab 样条曲线是基于控制点的样条曲线,例如 Hermite 或 Catmull-Rom 而不是b样条。

但是,scipy.interpolate.interpolate.spltopp确实提供了一种获取 b 样条的部分多项式系数的方法。不幸的是,它似乎不太好用。

>>> import scipy.interpolate
>>> x = [0, 1, 2, 3]
>>> y = [0, 1, 4, 0]
>>> tck = scipy.interpolate.splrep(x, y)
>>> tck
Out: 
    (array([ 0.,  0.,  0.,  0.,  3.,  3.,  3.,  3.]),
    array([  3.19142761e-16,  -3.00000000e+00,   1.05000000e+01,
        0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
        0.00000000e+00,   0.00000000e+00]),
    3)

>>> pp = scipy.interpolate.interpolate.spltopp(tck[0][1:-1], tck[1], tck[2])

>>> pp.coeffs.T
Out: 
    array([[ -4.54540394e-322,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000],
        [ -4.54540394e-322,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000],
        [ -4.54540394e-322,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000]])

请注意,每个结有一组系数,而不是每个传入的原始点都有一个系数。此外,将系数乘以 b 样条基矩阵似乎不是很有帮助。

>>> bsbm = array([[-1,  3, -3,  1], [ 3, -6,  3,  0], [-3,  0,  3,  0], 
                 [ 1,  4,  1,  0]]) * 1.0/6
Out: 
    array([[-0.16666667,  0.5       , -0.5       ,  0.16666667],
        [ 0.5       , -1.        ,  0.5       ,  0.        ],
        [-0.5       ,  0.        ,  0.5       ,  0.        ],
        [ 0.16666667,  0.66666667,  0.16666667,  0.        ]])

>>> dot(pp.coeffs.T, bsbm)
Out: 
    array([[  7.41098469e-323,  -2.27270197e-322,   2.27270197e-322,
           -7.41098469e-323],
        [  7.41098469e-323,  -2.27270197e-322,   2.27270197e-322,
           -7.41098469e-323],
        [  7.41098469e-323,  -2.27270197e-322,   2.27270197e-322,
           -7.41098469e-323],
        [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
           0.00000000e+000]])

FORTRAN 分段多项式包PPPack有一个bsplpp从 B 样条转换为分段多项式形式的命令,可以满足您的需求。不幸的是,目前还没有 PPPack 的 Python 包装器。

于 2012-11-15T15:12:20.973 回答
2

如果您安装了 scipy 版本 >= 0.18.0,则可以使用 scipy.interpolate 中的 CubicSpline 函数进行三次样条插值。

您可以通过在 python 中运行以下命令来检查 scipy 版本:

#!/usr/bin/env python3
import scipy
scipy.version.version

如果您的 scipy 版本 >= 0.18.0,您可以运行以下示例代码进行三次样条插值:

#!/usr/bin/env python3

import numpy as np
from scipy.interpolate import CubicSpline

# calculate 5 natural cubic spline polynomials for 6 points
# (x,y) = (0,12) (1,14) (2,22) (3,39) (4,58) (5,77)
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([12,14,22,39,58,77])

# calculate natural cubic spline polynomials
cs = CubicSpline(x,y,bc_type='natural')

# show values of interpolation function at x=1.25
print('S(1.25) = ', cs(1.25))

## Aditional - find polynomial coefficients for different x regions

# if you want to print polynomial coefficients in form
# S0(0<=x<=1) = a0 + b0(x-x0) + c0(x-x0)^2 + d0(x-x0)^3
# S1(1< x<=2) = a1 + b1(x-x1) + c1(x-x1)^2 + d1(x-x1)^3
# ...
# S4(4< x<=5) = a4 + b4(x-x4) + c5(x-x4)^2 + d5(x-x4)^3
# x0 = 0; x1 = 1; x4 = 4; (start of x region interval)

# show values of a0, b0, c0, d0, a1, b1, c1, d1 ...
cs.c

# Polynomial coefficients for 0 <= x <= 1
a0 = cs.c.item(3,0)
b0 = cs.c.item(2,0)
c0 = cs.c.item(1,0)
d0 = cs.c.item(0,0)

# Polynomial coefficients for 1 < x <= 2
a1 = cs.c.item(3,1)
b1 = cs.c.item(2,1)
c1 = cs.c.item(1,1)
d1 = cs.c.item(0,1)

# ...

# Polynomial coefficients for 4 < x <= 5
a4 = cs.c.item(3,4)
b4 = cs.c.item(2,4)
c4 = cs.c.item(1,4)
d4 = cs.c.item(0,4)

# Print polynomial equations for different x regions
print('S0(0<=x<=1) = ', a0, ' + ', b0, '(x-0) + ', c0, '(x-0)^2  + ', d0, '(x-0)^3')
print('S1(1< x<=2) = ', a1, ' + ', b1, '(x-1) + ', c1, '(x-1)^2  + ', d1, '(x-1)^3')
print('...')
print('S5(4< x<=5) = ', a4, ' + ', b4, '(x-4) + ', c4, '(x-4)^2  + ', d4, '(x-4)^3')

# So we can calculate S(1.25) by using equation S1(1< x<=2)
print('S(1.25) = ', a1 + b1*0.25 + c1*(0.25**2) + d1*(0.25**3))

# Cubic spline interpolation calculus example
    #  https://www.youtube.com/watch?v=gT7F3TWihvk
于 2018-02-11T12:40:59.047 回答
2

以下是我如何获得类似于 MATLAB 的结果:

>>> from scipy.interpolate import PPoly, splrep
>>> x = [0, 1, 2, 3]
>>> y = [0, 1, 4, 0]
>>> tck = splrep(x, y)
>>> tck
Out: (array([ 0.,  0.,  0.,  0.,  3.,  3.,  3.,  3.]),
 array([  3.19142761e-16,  -3.00000000e+00,   1.05000000e+01,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00]),
 3)

>>> pp = PPoly.from_spline(tck)
>>> pp.c.T
Out: array([[ -1.50000000e+00,   5.50000000e+00,  -3.00000000e+00,
      3.19142761e-16],
   [ -1.50000000e+00,   5.50000000e+00,  -3.00000000e+00,
      3.19142761e-16],
   [ -1.50000000e+00,   5.50000000e+00,  -3.00000000e+00,
      3.19142761e-16],
   [ -1.50000000e+00,   5.50000000e+00,  -3.00000000e+00,
      3.19142761e-16],
   [ -1.50000000e+00,  -8.00000000e+00,  -1.05000000e+01,
      0.00000000e+00],
   [ -1.50000000e+00,  -8.00000000e+00,  -1.05000000e+01,
      0.00000000e+00],
   [ -1.50000000e+00,  -8.00000000e+00,  -1.05000000e+01,
      0.00000000e+00]])
于 2015-12-25T22:19:02.097 回答
1

scipy.interpolate.splrep上的文档说您可以获得系数:

Returns:

  tck : tuple

  (t,c,k) a tuple containing the vector of knots, the B-spline coefficients, and the degree of the spline.
于 2012-11-15T02:40:07.127 回答