Next: Two-dimensionsal spline representation (interpolate.bisplrep) Up: Interpolation (interpolate) Previous: Linear 1-d interpolation (interpolate.linear_1d)

Spline interpolation in 1-d (interpolate.splXXX)

Spline interpolation requires two essential steps: (1) a spline representation of the curve is computed, and (2) the spline is evaluated at the desired points. In order to find the spline representation, there are two different was to represent a curve and obtain (smoothing) spline coefficients: directly and parametrically. The direct method finds the spline representation of a curve in a two-dimensional plane using the function interpolate.splrep. The first two arguments are the only ones required, and these provide the \( x \) and \( y \) components of the curve. The normal output is a 3-tuple, \( \left( t,c,k\right) \), containing the knot-points, \( t \), the coefficients \( c \) and the order \( k \) of the spline. The default spline order is cubic, but this can be changed with the input keyword, k.

For curves in \( N \)-dimensional space the function interpolate.splprep allows defining the curve parametrically. For this function only 1 input argument is required. This input is a list of \( N \)-arrays representing the curve in \( N \)-dimensional space. The length of each array is the number of curve points, and each array provides one component of the \( N \)-dimensional data point. The parameter variable is given with the keword argument, u, which defaults to an equally-spaced monotonic sequence between \( 0 \) and \( 1 \). The default output consists of two objects: a 3-tuple, \( \left( t,c,k\right) \), containing the spline representation and the parameter variable \( u. \)

The keyword argument, s, is used to specify the amount of smoothing to perform during the spline fit. The default value of \( s \) is \( s=m-\sqrt{2m} \) where \( m \) is the number of data-points being fit. Therefore, if no smoothing is desired a value of \( \mathbf{s}=0 \) should be passed to the routines.

Once the spline representation of the data has been determined, functions are available for evaluating the spline (interpolate.splev) and its derivatives (interpolate.splev, interpolate.splade) at any point and the integral of the spline between any two points (interpolate.splint). In addition, for cubic splines (\( k=3 \)) with 8 or more knots, the roots of the spline can be estimated (interpolate.sproot). These functions are demonstrated in the example that follows (see also Figure 3).

Figure 3: Examples of using cubic-spline interpolation.
[Cubic-spline (splrep)] \resizebox*{0.45\textwidth}{!}{\includegraphics{interp_cubic.epsi}} [Derivative of spline (splev)] \resizebox*{0.45\textwidth}{!}{\includegraphics{interp_cubic_der.epsi}}

[Integral of spline (splint)] \resizebox*{0.45\textwidth}{!}{\includegraphics{interp_cubic_int.epsi}} [Spline of parametric curve (splprep)] \resizebox*{0.45\textwidth}{!}{\includegraphics{interp_cubic_param.epsi}}


>>> # Cubic-spline
>>> x = arange(0,2*pi+pi/4,2*pi/8)
>>> y = sin(x)
>>> tck = interpolate.splrep(x,y,s=0)
>>> xnew = arange(0,2*pi,pi/50)
>>> ynew = interpolate.splev(xnew,tck,der=0)
>>> xplt.plot(x,y,'x',xnew,ynew,xnew,sin(xnew),x,y,'b')
>>> xplt.legend(['Linear','Cubic Spline', 'True'],['b-x','m','r'])
>>> xplt.limits(-0.05,6.33,-1.05,1.05)
>>> xplt.title('Cubic-spline interpolation')
>>> xplt.eps('interp_cubic')

>>> # Derivative of spline
>>> yder = interpolate.splev(xnew,tck,der=1)
>>> xplt.plot(xnew,yder,xnew,cos(xnew),'|')
>>> xplt.legend(['Cubic Spline', 'True'])
>>> xplt.limits(-0.05,6.33,-1.05,1.05)
>>> xplt.title('Derivative estimation from spline')
>>> xplt.eps('interp_cubic_der')

>>> # Integral of spline
>>> def integ(x,tck,constant=-1):
>>>     x = asarray_1d(x)
>>>     out = zeros(x.shape, x.typecode())
>>>     for n in xrange(len(out)):
>>>         out[n] = interpolate.splint(0,x[n],tck)
>>>     out += constant
>>>     return out
>>>
>>> yint = integ(xnew,tck)
>>> xplt.plot(xnew,yint,xnew,-cos(xnew),'|')
>>> xplt.legend(['Cubic Spline', 'True'])
>>> xplt.limits(-0.05,6.33,-1.05,1.05)
>>> xplt.title('Integral estimation from spline')
>>> xplt.eps('interp_cubic_int')

>>> # Roots of spline
>>> print interpolate.sproot(tck)
[ 0.      3.1416]

>>> # Parametric spline
>>> t = arange(0,1.1,.1)
>>> x = sin(2*pi*t)
>>> y = cos(2*pi*t)
>>> tck,u = interpolate.splprep([x,y],s=0)
>>> unew = arange(0,1.01,0.01)
>>> out = interpolate.splev(unew,tck)
>>> xplt.plot(x,y,'x',out[0],out[1],sin(2*pi*unew),cos(2*pi*unew),x,y,'b')
>>> xplt.legend(['Linear','Cubic Spline', 'True'],['b-x','m','r'])
>>> xplt.limits(-1.05,1.05,-1.05,1.05)
>>> xplt.title('Spline of parametrically-defined curve')
>>> xplt.eps('interp_cubic_param')



Next: Two-dimensionsal spline representation (interpolate.bisplrep) Up: Interpolation (interpolate) Previous: Linear 1-d interpolation (interpolate.linear_1d)
2001-07-27