Saturday, July 14, 2007

scipy spline example

# 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)

# 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’)

PIC
(a) Cubic-spline (splrep)
PIC
(b) Derivative of spline (splev)

PIC
(c) Integral of spline (splint)
PIC
(d) Spline of parametric curve (splprep)

Figure 3: Examples of using cubic-spline interpolation.

2 comments:

Shrividya said...

Great post! Enormously useful. This has really helped me understand spline interpolation.
Thanks a lot.

f_lima said...

This post is a copy of the official Scipy tutorial : http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

please acknowledge source.