How to specify the number of knot points when using scipy.splprep
Asked Answered
I

1

6

I have the following piece of code. It generates the 3-D cubic spline of the given 3-D function given in parametric form. I pretty much adapted this to my case using the online documentation for splprep and splev.

But I have some things I don't understand. Here is the code:

%matplotlib inline
from numpy import arange, cos, linspace, pi, sin, random
from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt

# make ascending spiral in 3-space
t=linspace(0,1.75*2*pi,100)

x = sin(t)
y = cos(t)
z = t


# spline parameters
s=3.0 # smoothness parameter
k=3 # spline order
nest=-1 # estimate of number of knots needed (-1 = maximal)

# find the knot points
tck,u = splprep([x,y,z],s=s,k=k,nest=-1)

# evaluate spline, including interpolated points
xnew,ynew,znew = splev(linspace(0,1,400),tck)

I have a few questions regarding this implementation.

  1. What exactly does the (t,c,k) tuple return in this case?. I read the documentation and it says it returns the knot points, the coefficients and the degree of the spline. Doesn't knot points have to be coordinates of the form (x, y, z)?. So we have to have "number of knots" such coordinate points. But that's not what gets returned. We simply get returned an array of length 11.

  2. What does u return? (Documentation says it returns the values of the parameter. What does that mean?. The values of the parameter t?

  3. When I use nest = -1 (This is the default) it uses the maximal number of knot points needed (in this case they use 11 knot points). But how do I specify my own number of knot points, let's say 50 or 80 etc?

I am completely misunderstanding the documentation here. Can someone enlighten me may be using examples?

Inefficacious answered 15/1, 2017 at 8:5 Comment(0)
D
5

Values of parameter, u

The idea is that your points [x,y,z] are values of some parameterized curve, the original parameter being t in your example. Knowing the values of the parameter t helps in figuring out how to interpolate between these points. For this reason, you are given an option to pass the values of parameter as optional argument u (it would be u=t in this example). But if you choose not to do so, the method will make a guess for the values of the parameter, based on the distances between given points (the parameter interval will be from 0 to 1). This guess is then returned to you as variable u, so you know how the data was interpreted. If you do pass u=t as the argument, the u you get back is exactly the same.

You don't really need this u to use the spline. However, if it is desirable to compare the location of original [x,y,z] points to the values of the spline, then you may want to pass u as an argument to splev. A shorter way to explain the meaning of u is: it's what splev would need to reproduce the [x,y,z] coordinates that you started with, with some deviations due to smoothing.

tck values

The knots of a spline, t, are the points in the parameter interval, not in the 3D space. Since in your example the parameter interval is [0,1], chosen by default, the values of t are in this range. A knot is a place on the parameter interval where some coefficients of the spline change. The endpoints 0 and 1 are technically multiple knots, so they are listed several times.

The 3D nature of the curve is expressed by coefficients c. You may notice it's a list of three arrays, one for each coordinate.

Number of knot points

With this interpolation routine, you have two choices:

  • tell it exactly what the knot points are (by giving task=-1 and providing t argument with the knots). To avoid confusion: this t is not necessarily the t from which you got the points [x,y,z]. One doesn't necessarily want every sample point to be a knot point.
  • leave the determination of the knots up to the routine, including their number.

However, the number of knots depends on the value of smoothness parameter s, so it can be influenced indirectly. For example, with your data there are 11 knots with s=3, but 12 knots with s=1 and 14 knots with s=0.1.

Dottie answered 15/1, 2017 at 16:13 Comment(1)
Hi, in case of miss-understanding, I just wanted to specify that c values are not the power coefficients of the overall curve. They are the control points. That is why "The 3D nature of the curve is expressed by coefficients c. " sentence might lead to miss-understanding of the concept. In order to get power coefficients of the curve i.e., ax^2 + bx + c ( or maybe cubic coeff.), you need to use these control points and apply some linear regression or interpolation.Laquitalar

© 2022 - 2024 — McMap. All rights reserved.