Create BSpline from knots and coefficients

不想你离开。 提交于 2021-01-27 18:14:39

问题


How can a spline be created if only the points and the coefficients are known? I'm using scipy.interpolate.BSpline here, but am open to other standard packages as well. So basically I want to be able to give someone just those short arrays of coefficients for them to be able to recreate the fit to the data. See the failed red-dashed curve below.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import BSpline, LSQUnivariateSpline

x = np.linspace(0, 10, 50) # x-data
y = np.exp(-(x-5)**2/4)    # y-data

# define the knot positions

t = [1, 2, 4, 5, 6, 8, 9]

# get spline fit

s1 = LSQUnivariateSpline(x, y, t)

x2 = np.linspace(0, 10, 200) # new x-grid
y2 = s1(x2) # evaluate spline on that new grid

# FAILED: try to construct BSpline using the knots and coefficients

k = s1.get_knots()
c = s1.get_coeffs()
s2 = BSpline(t,c,2)

# plotting

plt.plot(x, y, label='original')
plt.plot(t, s1(t),'o', label='knots')
plt.plot(x2, y2, '--', label='spline 1')
plt.plot(x2, s2(x2), 'r:', label='spline 2') 
plt.legend()


回答1:


The fine print under get_knots says:

Internally, the knot vector contains 2*k additional boundary knots.

That means, to get a usable knot array from get_knots, one should add k copies of the left boundary knot at the beginning of array, and k copies of the right boundary knot at the end. Here k is the degree of the spline, which is usually 3 (you asked for LSQUnivariateSpline of default degree, so that's 3). So:

kn = s1.get_knots()
kn = 3*[kn[0]] + list(kn) + 3*[kn[-1]]
c = s1.get_coeffs()
s2 = BSpline(kn, c, 3)    # not "2" as in your sample; we are working with a cubic spline 

Now, the spline s2 is the same as s1:

Equivalently, kn = 4*[x[0]] + t + 4*[x[-1]] would work: your t list contains only interior knots, so x[0] and x[-1] are added, and then each repeated k times more.

The mathematical reason for the repetition is that B-splines need some room to get built, due to their inductive definition which requires (k-1)-degree splines to exist around every interval in which we define the kth degree spline.



来源:https://stackoverflow.com/questions/52393145/create-bspline-from-knots-and-coefficients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!