I am trying to use Python's SciPy package to make a spline through some data. In reference to the Question here, I think the reason why there are duplicate knots at the beginning and end of using interpolate.splrep is because it is making a clamped spline. I want the knots to go through the data (s=0). The output of splrep repeats the first and last knots 4x each (as in the question above), and it includes the interior data points as knots except the second point and the next to last point. Here is a MWE:
import math
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
x_dec = np.array([ 3.0289979374768700e-01, 3.0280333252410779e-01, 3.0270688524899364e-01,
3.0261045192336944e-01, 3.0251403254826026e-01, 3.0241762712469000e-01,
3.0232123565368257e-01, 3.0222485813626193e-01, 3.0212849457345037e-01])
# Make monotonically increase
x_inc = math.pi/2 - x_dec
print x_inc
y = np.array([0.914024505, 0.914049905, 0.914075289, 0.914100656, 0.914126008, 0.914151343,
0.914176662, 0.914201964, 0.91422725])
spl_tck = scipy.interpolate.splrep(x_inc,y,k=3,s=0.0)
# Print the knots
print spl_tck[0]
Here is the output:
[ 1.26789653 1.26799299 1.26808944 1.26818587 1.26828229 1.2683787 1.26847509 1.26857147 1.26866783]
[ 1.26789653 1.26789653 1.26789653 1.26789653 1.26808944 1.26818587 1.26828229 1.2683787 1.26847509 1.26866783 1.26866783 1.26866783 1.26866783]
The first line is the data, the second line is the knots. As you can see, the knots repeat the first and last data points (x_inc[0]
and x_inc[-1]
). Then data points x_inc[2]
through x_inc[-3]
are all knot points. However, data points x_inc[1]
and x_inc[-2]
are skipped as knot points.
I thought that specifying s=0 would force the spline through all the points. Is there something about B-splines and repeated the endpoints as knot points that precludes using the adjacent data points to the end points as knot points?