Á. Márton
Á. Márton

Reputation: 547

How to interpret the result of scipy.interpolate.splrep?

I need to fit splines consisting of 3rd order polynomials on a 1D line defined in the x-y plane. The derivative of the polynomials has to be equal at the joints. The expected output is a list of 3rd order polynomials, defined by their starting point (spline knots) and their polynomial coefficients.

I figured the splrep function of scipy would be ideal for this. However I have trouble understanding the result of the function. For example, the following code produces these results:

x = np.linspace(0, 10, 100)
y = np.sin(x)
sp1 = scipy.interpolate.splrep(x, y, k = 3, t = [2, 6])

result ("knot" and "coefficient" array from sp1):

knots: [ 0.,  0.,  0.,  0.,  2.,  6., 10., 10., 10., 10.]
coefficients: [-0.32946251,  1.55647594,  0.19883333, -2.08984459,  2.79531098,
   -1.14372454,  0.        ,  0.        ,  0.        ,  0.        ]

From the documentation and from my mathematical understanding I expect that the underlying code fit 3 splines on the points: one from 0 to 2, one from 2 to 6, and one from 6 to 10. What are the equations of these splines then? Why do I have only 6 coefficients? Shouldn't I have 12 (3*4)?

Note: I know that there are functions such as splev to evaluate the result - I don't need that. I need only the parameters of the splines.

Upvotes: 1

Views: 2149

Answers (1)

newkid
newkid

Reputation: 1458

This is a bit tricky to interpret.

Notes

See splev for evaluation of the spline and its derivatives. The number of dimensions N must be smaller than 11. The number of coefficients in the c array is k+1 less then the number of knots, len(t). This is in contrast with splrep, which zero-pads the array of coefficients to have the same length as the array of knots. These additional coefficients are ignored by evaluation routines, splev and BSpline.

Since these set of spline interpolation function wrap around FITPACK's subroutines splev, you'll have to see what the coefficients mean there. In the subroutine's documentation:

c    t    : array,length n, which contains the position of the knots.
c    n    : integer, giving the total number of knots of s(x).
c    c    : array,length n, which contains the b-spline coefficients.

Coming back full circle to scipy. It elucidates the BSpline formula while also indicating how it uses the coefficients and what their expected length is.

To make sure you are giving the correct inputs, it is always important to correlate this to the mathematical definition of BSplines to identify how may knots and control points you need for a given order of the BSpline.

Upvotes: 1

Related Questions