[英]How can I change the number of basis functions when performing B-Spline fitting in scipy (python)?
I have a discrete set of points (x_n, y_n) that I would like to approximate/represent as a linear combination of B-spline basis functions. 我有一组离散点(x_n,y_n),我想将它们近似/表示为B样条曲线基函数的线性组合。 I need to be able to manually change the number of B-spline basis functions used by the method, and I am trying to implement this in python using scipy.
我需要能够手动更改该方法使用的B样条基函数的数量,并且我正在尝试使用scipy在python中实现此功能。 To be specific, below is a bit of code that I am using:
具体来说,以下是我正在使用的一些代码:
import scipy
spl = scipy.interpolate.splrep(x, y)
However, unless I have misunderstood or missed something in the documentation, it seems I cannot change the number of B-spline basis functions that scipy uses. 但是,除非我误解或错过了文档中的某些内容,否则看来我无法更改scipy使用的B样条基函数的数量。 That seems to be set by the size of x and y.
这似乎是由x和y的大小设置的。 So, my specific questions are:
因此,我的具体问题是:
Can I change the number of B-spline basis functions used by scipy in the "splrep" function that I used above? 我可以在上面使用的“ splrep”函数中更改scipy使用的B样条基函数的数量吗?
Once I have performed the transformation shown in the code above, how can I access the coefficients of the linear combination? 完成上面代码中所示的转换后,如何访问线性组合的系数? Am I correct in thinking that these coefficients are stored in the vector spl[1]?
我认为这些系数存储在矢量spl [1]中是否正确?
Is there a better method/toolbox that I should be using? 我应该使用更好的方法/工具箱吗?
Thanks in advance for any help/guidance you can provide. 在此先感谢您提供的任何帮助/指导。
Yes, spl[1] are the coefficients, and spl[0] contains the knot vector. 是的,spl [1]是系数,spl [0]包含结向量。
However, if you want to have a better control, you can manipulate the BSpline objects and construct them with make_interp_spline or make_lsq_spline, which accepts the knot vector and that determines the b-spline basis functions to use. 但是,如果想要更好的控制,则可以操纵BSpline对象,并使用make_interp_spline或make_lsq_spline构造它们,它们接受结向量并确定要使用的b样条基础函数。
You can change the number of B-spline basis functions, by supplying a knot vector with the t
parameter. 您可以通过为结向量提供
t
参数来更改B样条基函数的数量。 Since there is a connection number of knots = number of coefficients + degree + 1
, the number of knots will also define the number of coefficients (== the number of basis functions). 由于连接
number of knots = number of coefficients + degree + 1
,所以结数也将定义系数数(==基函数数)。
The usage of the t
parameter is not so intuitive since the given knots should be only the inner knots. t
参数的用法不是那么直观,因为给定的结应仅是内部结。 So, for example, if you want 7 coefficients for a cubic spline you need to give 3 inner knots. 因此,例如,如果要为三次样条曲线设置7个系数,则需要给出3个内部结。 Inside the function it pads the first and last (degree+1) knots with the
xb
and xe
(clamped end conditions see for example here ). 在函数内部,它用
xb
和xe
xb
第一个和最后一个(度+1)结(固定的结束条件,请参见此处 )。 Furthermore, as the documentation says, the knots should satisfy the Schoenberg-Whitney conditions. 此外,如文档所述,结应满足Schoenberg-Whitney条件。
Here is an example code that does this: 这是执行此操作的示例代码:
# Input:
x = np.linspace(0,2*np.pi, 9)
y = np.sin(x)
# Your code:
spl = scipy.interpolate.splrep(x, y)
t,c,k = spl # knots, coefficients, degree (==3 for cubic)
# Computing the inner knots and using them:
t3 = np.linspace(x[0],x[-1],5) # five equally spaced knots in the interval
t3 = t3[1:-1] # take only the three inner values
spl3 = scipy.interpolate.splrep(x, y, t=t3)
Regarding your second question, you're right that the coefficients are indeed stored in spl[1]
. 关于第二个问题,您是正确的,系数确实存储在
spl[1]
。 However, note that (as the documentation says) the last (degree+1) values are zero-padded and should be ignored. 但是,请注意(如文档所述),最后一个(度+1)值用零填充,因此应忽略。
In order to evaluate the resulting B-spline you can use the function splev
or the class BSpline
. 为了评估生成的B样条曲线,可以使用
splev
函数或BSpline
类。 Below is some example code that evaluates and draws the above splines (resulting in the following figure): 下面是一些示例代码,用于评估和绘制上述样条曲线(如下图所示):
xx = np.linspace(x[0], x[-1], 101) # sample points
yy = scipy.interpolate.splev(xx, spl) # evaluate original spline
yy3 = scipy.interpolate.splev(xx, spl3) # evaluate new spline
plot(x,y,'b.') # plot original interpolation points
plot(xx,yy,'r-', label='spl')
plot(xx,yy3,'g-', label='spl3')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.