繁体   English   中英

Scipy b 样条基函数

[英]Scipy b spline basis functions

在 Scipy 中是否有更有效的方法来生成类似于 b 样条 function 示例中给出的递归代码的 b 样条基函数:

scipy.interpolate.BSpline

def B(x, k, i, t):
   if k == 0:
      return 1.0 if t[i] <= x < t[i+1] else 0.0
   if t[i+k] == t[i]:
      c1 = 0.0
   else:
      c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t)
   if t[i+k+1] == t[i+1]:
      c2 = 0.0
   else:
      c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t)
   return c1 + c2

我尝试使用scipy.interpolate.BSpline.basis_element但无法生成与 function “B”相同的结果。

我以前这样做的方式如下:

import scipy.interpolate as si
import numpy as np
import matplotlib.pyplot as plt

nt = 3 # the number of knots
x = np.linspace(0,24,10)
y = np.ones((x.shape)) # the function we wish to interpolate (very trivial in this case)
t = np.linspace(x[0],x[-1],nt)
t = t[1:-1]

tt = np.linspace(0.0, 24, 100)
y_rep = si.splrep(x, y, t=t, k=2)

y_i = si.splev(tt, y_rep)

spl = np.zeros((100,))

plt.figure()
for i in range(nt+1):
    vec = np.zeros(nt+1)
    vec[i] = 1.0
    y_list = list(y_rep)
    y_list[1] = vec.tolist()
    y_i = si.splev(tt, y_list) # your basis spline function
    spl = spl + y_i*y_rep[1][i] # the interpolated function
    plt.plot(tt, y_i)

您可以查看两个函数scipy.interpolate.splrepscipy.interpolate.splev 他们分别进行 B 样条表示并评估 B 样条。 上面的代码生成(在这种情况下)四个基本样条函数:

在此处输入图像描述

我希望这是你想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM