user-2147482637
user-2147482637

Reputation: 2163

Matlab spline function in python

I am in the process of converting some matlab code to python when I ran into the spline function in matlab. I assumed that numpy would have something similar but all I can find on google is scipy.interpolate, which has so many options I dont even know where to start. http://docs.scipy.org/doc/scipy/reference/interpolate.html Is there an exact equivalent to the matlab spline? Since I need it to run for various cases there is not one single test case, so in the worst case I need to recode the function and that will take unnecessary amounts of time.

Thanks

Edit:

So i have tried the examples of the answers so far, but i dont see how they are similar, for example spline(x,y) in matlab returns:

>> spline(x,y)

ans = 

      form: 'pp'
    breaks: [0 1 2 3 4 5 6 7 8 9]
     coefs: [9x4 double]
    pieces: 9
     order: 4
       dim: 1

Upvotes: 2

Views: 3294

Answers (1)

exogeographer
exogeographer

Reputation: 359

SciPy:

scipy.interpolate.UnivariateSpline

http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline

Note that it returns an interpolator (function) not interpolated values. You have to make a call to the resulting function:

spline = UnivariateSpline(x, y)
yy = spline(xx)

Upvotes: 3

Related Questions