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 and
components of the curve. The normal output
is a 3-tuple,
, containing the knot-points,
, the coefficients
and the order
of the
spline. The default spline order is cubic, but this can be changed
with the input keyword, k.
For curves in -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
-arrays
representing the curve in
-dimensional space. The length of
each array is the number of curve points, and each array provides
one component of the
-dimensional data point. The parameter
variable is given with the keword argument, u, which defaults
to an equally-spaced monotonic sequence between
and
.
The default output consists of two objects: a 3-tuple,
,
containing the spline representation and the parameter variable
The keyword argument, s, is used to specify the amount of smoothing
to perform during the spline fit. The default value of is
where
is the number of data-points being
fit. Therefore, if no smoothing is desired a value of
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 ()
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).
[Cubic-spline (splrep)]
![]() ![]()
[Integral of spline (splint)]
![]() ![]()
|
>>> # 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')