python - Scipy b 样条基函数

标签 python scipy bspline

Scipy 中是否有更有效的方法来生成类似于 b 样条函数示例中给出的递归代码的 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但无法生成与函数“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 样条。上面的代码生成(在本例中)四个基本样条函数:

enter image description here

希望这就是你想要的。

关于python - Scipy b 样条基函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321662/

相关文章:

python - 对 pandas DataFrame 中每一行的操作

python - 如何将集合附加到数据库?

python - PANDAS - 重命名并合并类似的列

c++ - 使用 GNU 科学库 (GSL) 使用不均匀分布的点绘制 2D B 样条路径

python - scipy BSpline 在 python 中的拟合

c++ - 任意数量控制点的 B 样条曲线

python - 返回目录和子目录中的文件总数

python - Optimize.fmin 没有找到行为良好的连续函数的最小值

python - 正弦计算比余弦慢几个数量级

python - 在 Scipy 中使用 splev 评估样条的导数