[SciPy-user] spline interpolation

Robert Kern robert.kern at gmail.com
Thu Nov 9 16:54:40 CST 2006

Jordan Dawe wrote:
> I've been looking at scipy's interpolation routines and I can't make
> heads or tails of them.  I just want to do a spline interp1d like matlab
> does.  Is there any way to do this?

I don't know exactly what features you want from Matlab's interp1d, but you
probably want scipy.interpolate.UnivariateSpline.

In [92]: from scipy import interpolate

In [93]: interpolate?
Type:           module
Base Class:     <type 'module'>
Namespace:      Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scipy-0.5.2.dev2310-py2.5-macosx-10.4-i386.egg
/scipy/interpolate/__init__.py
Docstring:
Interpolation Tools
===================

Wrappers around FITPACK functions:

splrep    -- find smoothing spline given (x,y) points on curve.
splprep   -- find smoothing spline given parametrically defined curve.
splev     -- evaluate the spline or its derivatives.
splint    -- compute definite integral of a spline.
sproot    -- find the roots of a cubic spline.
spalde    -- compute all derivatives of a spline at given points.
bisplrep   -- find bivariate smoothing spline representation.
bisplev   -- evaluate bivariate smoothing spline.

UnivariateSpline             -- A more recent, object-oriented wrapper;
finds a (possibly smoothed) interpolating
spline.
InterpolatedUnivariateSpline
LSQUnivariateSpline
BivariateSpline              -- A more recent, object-oriented wrapper;
finds a interpolating spline for a
bivariate function.

SmoothBivariateSpline

Interpolation Class

interp1d -- Create a class whose instances can linearly interpolate
to compute unknown values of a univariate function.
interp2d -- Create a class whose instances can interpolate
to compute unknown values of a bivariate function.

In [95]: interpolate.UnivariateSpline?
Type:           type
Base Class:     <type 'type'>
Namespace:      Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scipy-0.5.2.dev2310-py2.5-macosx-10.4-i386.egg/scipy/interpolate/fitpack2.py
Docstring:
Univariate spline s(x) of degree k on the interval
[xb,xe] calculated from a given set of data points
(x,y).

Can include least-squares fitting.

splrep, splev, sproot, spint, spalde - an older wrapping of FITPACK
BivariateSpline - a similar class for bivariate spline interpolation

In [99]: from numpy import *

In [100]: x = linspace(0, 2, num=201)

In [101]: x = linspace(0, 2, num=21)

In [102]: y = sin(x*pi)

In [103]: us = interpolate.UnivariateSpline(x, y, s=0.0)

In [104]: x2 = linspace(0, 2, num=201)

In [105]: y2 = us(x2)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma