简体   繁体   English

scipy.interpolate.splprep 函数中的系数是如何导出的

[英]How are the coefficients derived in the scipy.interpolate.splprep function

Pardon this long post.原谅这篇长文。

I am new to BSpline and is struggling to understand few things.我是 BSpline 的新手,并且很难理解一些事情。 I have a set of data points for which I need to construct a BSpline curve.我有一组数据点,需要为其构建 BSpline 曲线。 The Datapoints are as follows:数据点如下:

x = [150 130 148]
y = [149 114  79]

After running the following function:运行以下函数后:

from scipy.interpolate import splprep, splev
tck, u = splprep([x, y], k =2, s = 0)

I am getting我正进入(状态

parameters u = [0.参数 u = [0. 0.505987 1.] 0.505987 1.]

knots t = [0, 0, 0, 1, 1, 1]节 t = [0, 0, 0, 1, 1, 1]

coefficients c = [array([150. , 111.01850233, 148. ]), array([149. , 114.83829958, 79. ])]系数 c = [array([150., 111.01850233, 148.]), array([149., 114.83829958, 79.])]

k = 2 (this is the degree of curve I have used as an input for splprep). k = 2(这是我用作 splprep 输入的曲线度数)。

I now need to test whether the t,c,k values generated are correct or not.我现在需要测试生成的 t,c,k 值是否正确。

I ran the following function -我运行了以下功能 -

newPts = splev(u, tck)

This is giving me back the x and y data points I used in slprep.这将返回我在 slprep 中使用的 x 和 y 数据点。

newPts[0] = x newPts[0] = x

newPts[1] = y新点[1] = y

Plotting newPts[0]againt newPts[1] gives me the following spline绘制 newPts[0]againt newPts[1] 给我以下样条

spline evlaluation 1样条评估 1

The second test I ran was changing the parameters value to我运行的第二个测试是将参数值更改为

u = np.linspace(0,1,5)

then ran the following然后运行以下

newPts = splev(u, tck)

This time my spline curve looks like the following这次我的样条曲线如下所示

spline evaluation 2样条评估 2

From the following links computing the parameters , knot vector generation , I deduced that my parameter(u) and knots(t) are derived correctly.从以下链接计算参数节点向量生成,我推断我的参数(u)和节点(t)是正确导出的。 However, the computation of coeffcients look complicated.然而,系数的计算看起来很复杂。 But from the Global Curve interpolation formula, found here, coefficient matrix , I can see the coefficient matrix is an nXn, matrix that is in my case it has to be a 3X3 matrix.但是从全局曲线插值公式,在这里找到, 系数矩阵,我可以看到系数矩阵是一个 nXn,矩阵在我的情况下它必须是一个 3X3 矩阵。 But the coefficient matrix that I am getting is 2X3 that too the first array are the coefficients of x and the last array are the coefficients of y.但是我得到的系数矩阵是 2X3,第一个数组也是 x 的系数,最后一个数组是 y 的系数。

I really need a concrete way to prove if the coefficients derived from the splprep library are correct or not.我真的需要一个具体的方法来证明从 splprep 库中导出的系数是否正确。

Really appreciate the help.真的很感谢帮助。

Yes, the values are correct.是的,这些值是正确的。 Let me show you how I have checked them using the Wolfram language (AKA Mathematica):让我向您展示我如何使用 Wolfram 语言(AKA Mathematica)检查它们:

First, I take the control points (what you saved as c )首先,我拿控制点(你保存为c

cps={{150,149},{111.01850233,114.83829958},{148,79}};

Since there are no internal knots (ie, t=[0, 0, 0, 1, 1, 1] ), your B-spline actually reduces to a Bézier curve.由于没有内部结(即t=[0, 0, 0, 1, 1, 1] ),您的 B 样条实际上减少为贝塞尔曲线。 Let's create it:让我们创建它:

curve:=BezierFunction[cps]

Now we can evaluate it in the parameters u and check that it interpolates your data.现在我们可以在参数u评估它并检查它是否插入了您的数据。

 In[23]:= curve[0]
Out[23]=  {150.,149.}

 In[24]:= curve[0.505987]
Out[24]=  {130.,114.}

 In[25]:= curve[1]
Out[25]=  {148.,79.}

We can even plot the entire curve:我们甚至可以绘制整条曲线:

data={{150,149}, {130,114},{148,79}};
Graphics[{PointSize[Large],BezierCurve[cps], Green, Line[cps],Red,Point[cps],Blue, Point[data]}]

曲线图

The curve is black, its control polygon red and the data points blue;曲线为黑色,其控制多边形为红色,数据点为蓝色; clearly, the curve passes through all the three data points.显然,曲线通过了所有三个数据点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM