我有一组离散的点(x_n,y_n),我想将它们近似/表示为B样条基函数的线性组合。我需要能够手动更改该方法使用的B样条基函数的数量,我正在尝试使用scipy在python中实现这一点。具体地说,下面是我使用的一些代码:
import scipy
spl = scipy.interpolate.splrep(x, y)
但是,除非我误解或遗漏了文档中的某些内容,否则我似乎无法更改scipy使用的B样条基函数的数量。这似乎是由x和y的大小设置的。所以,我的具体问题是:
提前感谢您所能提供的任何帮助/指导。
发布于 2018-07-10 07:35:05
是的,spl1是系数,spl包含结向量。
但是,如果希望获得更好的控制,可以操纵BSpline对象并使用make_interp_spline或make_lsq_spline构建它们,这将接受节点向量并确定要使用的b样条线基础函数。
发布于 2018-07-11 22:16:30
通过使用t
参数提供节点向量,可以更改B样条基函数的数量。由于存在连接number of knots = number of coefficients + degree + 1
,因此结的数量也将定义系数的数量(==基函数)。
t
参数的用法不是很直观,因为给定的结应该只是内部结。因此,例如,如果你想要一个三次样条的7个系数,你需要给出3个内结。在函数内部,它用xb
和xe
填充第一个和最后一个(degree+1)结(钳制结束条件,例如参见here)。此外,正如文档所说,纽结应该满足舍恩伯格-惠特尼条件。
以下是执行此操作的示例代码:
# 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)
关于你的第二个问题,你是对的,系数确实存储在spl[1]
中。但是,请注意(正如文档所述)最后一个(degree+1)值是零填充的,应该忽略。
为了计算得到的B样条曲线,可以使用函数splev
或类BSpline
。下面是一些示例代码,用于计算和绘制上面的样条线(生成下图):
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')
https://stackoverflow.com/questions/51195964
复制相似问题