在scipy(python)中执行B样条拟合时,如何更改基本函数的数量? [英] How can I change the number of basis functions when performing B-Spline fitting in scipy (python)?

查看:59
本文介绍了在scipy(python)中执行B样条拟合时,如何更改基本函数的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组离散点(x_n,y_n),我想将它们近似/表示为B样条曲线基函数的线性组合.我需要能够手动更改该方法使用的B样条基函数的数量,并且我正在尝试使用scipy在python中实现此功能.具体来说,以下是我正在使用的一些代码:

  import scipyspl = scipy.interpolate.splrep(x,y) 

但是,除非我误解或错过了文档中的某些内容,否则看来我无法更改scipy使用的B样条基函数的数量.这似乎是由x和y的大小设置的.所以,我的具体问题是:

  1. 我可以在上面使用的"splrep"函数中更改scipy使用的B样条基函数的数量吗?

  2. 执行完上面代码中所示的变换后,如何访问线性组合的系数?我认为这些系数存储在矢量spl [1]中是正确的吗?

  3. 我应该使用更好的方法/工具箱吗?

在此先感谢您提供的任何帮助/指导.

解决方案

通过提供带有 t 参数的结向量,可以更改B样条基函数的数量.由于存在连接结数=系数数+度+ 1 ,因此结数也将定义系数数(==基函数数).

t 参数的用法不是那么直观,因为给定的结应仅是内部结.因此,例如,如果要为三次样条曲线设置7个系数,则需要给出3个内部结.在函数内部,它使用 xb xe 填充第一个和最后一个(度+1)结(固定的结束条件请参见例如

  xx = np.linspace(x [0],x [-1],101)#个采样点yy = scipy.interpolate.splev(xx,spl)#计算原始样条yy3 = scipy.interpolate.splev(xx,spl3)#评估新样条plot(x,y,'b.')#绘制原始插值点情节(xx,yy,'r-',label ='spl')情节(xx,yy3,'g-',label ='spl3') 

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. 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. 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. That seems to be set by the size of x and y. So, my specific questions are:

  1. Can I change the number of B-spline basis functions used by scipy in the "splrep" function that I used above?

  2. 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]?

  3. Is there a better method/toolbox that I should be using?

Thanks in advance for any help/guidance you can provide.

解决方案

You can change the number of B-spline basis functions, by supplying a knot vector with the t parameter. 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).

The usage of the t parameter is not so intuitive since the given knots should be only the inner knots. So, for example, if you want 7 coefficients for a cubic spline you need to give 3 inner knots. Inside the function it pads the first and last (degree+1) knots with the xb and xe (clamped end conditions see for example here). Furthermore, as the documentation says, the knots should satisfy the Schoenberg-Whitney conditions.

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]. However, note that (as the documentation says) the last (degree+1) values are zero-padded and should be ignored.

In order to evaluate the resulting B-spline you can use the function splev or the class 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')

这篇关于在scipy(python)中执行B样条拟合时,如何更改基本函数的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆