我正在尝试执行以下操作(从维基百科中提取的图像)
#!/usr/bin/env python
from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt
# sampling
x = np.linspace(0, 10, 10)
y = np.sin(x)
# spline trough all the sampled points
tck = interpolate.splrep(x, y)
x2 = np.linspace(0, 10, 200)
y2 = interpolate.splev(x2, tck)
# spline with all the middle points as knots (not working yet)
# knots = x[1:-1] # it should be something like this
knots = np.array([x[1]]) # not working with above line and just seeing what this line does
weights = np.concatenate(([1],np.ones(x.shape[0]-2)*.01,[1]))
tck = interpolate.splrep(x, y, t=knots, w=weights)
x3 = np.linspace(0, 10, 200)
y3 = interpolate.splev(x2, tck)
# plot
plt.plot(x, y, 'go', x2, y2, 'b', x3, y3,'r')
plt.show()
代码的第一部分是从主要参考中提取的代码,但没有解释如何使用这些点作为控制结。
这段代码的结果如下图。
点是样本,蓝线是考虑到所有点的样条。红线是不适合我的。我试图将所有中间点考虑为控制结,但我不能。如果我尝试使用knots=x[1:-1]
它就是行不通。我会很感激任何帮助。
简而言之:如何在样条函数中使用所有中间点作为控制结?
注意:这最后一张图片正是我需要的,它是我所拥有的(样条通过所有点)和我需要的(带控制结的样条)之间的区别。有任何想法吗?