# [SciPy-User] Cubic splines - MATLAB vs Scipy.interpolate

Tue Sep 27 19:32:41 CDT 2011

```If you have regularly spaced data, scipy.ndimage.map_coordinates might also be worth a look.

cheers,
David

----- Original Message -----
From: Zachary Pincus <zachary.pincus@yale.edu>
To: SciPy Users List <scipy-user@scipy.org>
Cc:
Sent: Wednesday, 28 September 2011 9:27 AM
Subject: Re: [SciPy-User] Cubic splines - MATLAB vs Scipy.interpolate

scipy.signal has some cubic and quadratic spline functions:
cspline1d
cspline1d_eval
cspline2d

(and replace the c with q for the quadratic versions).

I have no idea how fast they are, or if they're at all drop-in replacements for the matlab ones. The stuff in scipy.interpolate is powerful, but the fitpack spline-fitting operations can be a bit input-sensitive and prone to strange ringing.

Zach

On Sep 27, 2011, at 3:32 PM, Charles R Harris wrote:

>
>
> On Tue, Sep 27, 2011 at 1:04 PM, Jaidev Deshpande <deshpande.jaidev@gmail.com> wrote:
> Hi
>
> The big question: Why does the MATLAB function spline operate faster than the cubic spline alternatives in Scipy, especially splrep and splev ?
>
> ------
>
> The context: I'm working on an algorithm that bottlenecks on spline interpolation.
>
> Some functions in Scipy return an interpolation object function depending on the input data which needs to be evaluated independently over the whole range.
>
> So I used 'lower order' functions like splrep and splev. Even that was too slow.
>
> Then I tried to write my own code for cubic splines, generating and solving a system of 4N simultaneous equations for interpolation between N+1 points.
>
> No matter what I do, the code is quite slow. How come the MATLAB function spline operate so fast? What am I missing? What can I do to speed it up?
>
>
> I suspect it is because the scipy routines you reference are based on b-splines, which are needed for least squares fits. Simple cubic spline interpolation through a give set of points tends to be faster and I believe that is what the Matlab spline function does. To get b-splines in Matlab you need one of the toolboxes, it doesn't come with the core. I don't think scipy has a simple cubic spline interpolation, but I may be wrong.
>
> Chuck
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
SciPy-User@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

```