scipy中样条插值的系数

时间:2012-11-14 18:20:50

标签: python matlab scipy

我想通过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 个答案:

答案 0 :(得分:7)

我不确定是否有办法从scipy获得那些系数。 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包装器。

答案 1 :(得分: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

答案 2 :(得分:1)

scipy.interpolate.splrep上的docs表示你可以得到系数:

Returns:

  tck : tuple

  (t,c,k) a tuple containing the vector of knots, the B-spline coefficients, and the degree of the spline.

答案 3 :(得分:1)

以下是我可以获得类似于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]])