[SciPy-user] equivalent to Matlab's interp1

Zachary Pincus zachary.pincus@yale....
Mon Jun 15 13:00:46 CDT 2009


There's also numpy.interp:

Definition:       numpy.interp(x, xp, fp, left=None, right=None)
Docstring:
     One-dimensional linear interpolation.

     Returns the one-dimensional piecewise linear interpolant to a  
function
     with given values at discrete data-points.

     Parameters
     ----------
     x : array_like
         The x-coordinates of the interpolated values.

     xp : 1-D sequence of floats
         The x-coordinates of the data points, must be increasing.

     fp : 1-D sequence of floats
         The y-coordinates of the data points, same length as `xp`.

     left : float, optional
         Value to return for `x < xp[0]`, default is `fp[0]`.

     right : float, optional
         Value to return for `x > xp[-1]`, defaults is `fp[-1]`.

     Returns
     -------
     y : {float, ndarray}
         The interpolated values, same shape as `x`.

     Raises
     ------
     ValueError
         If `xp` and `fp` have different length

     Notes
     -----
     Does not check that the x-coordinate sequence `xp` is increasing.
     If `xp` is not increasing, the results are nonsense.
     A simple check for increasingness is::

         np.all(np.diff(xp) > 0)


     Examples
     --------
     >>> xp = [1, 2, 3]
     >>> fp = [3, 2, 0]
     >>> np.interp(2.5, xp, fp)
     1.0
     >>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
     array([ 3. ,  3. ,  2.5,  0.56,  0. ])
     >>> UNDEF = -99.0
     >>> np.interp(3.14, xp, fp, right=UNDEF)
     -99.0

     Plot an interpolant to the sine function:

     >>> x = np.linspace(0, 2*np.pi, 10)
     >>> y = np.sin(x)
     >>> xvals = np.linspace(0, 2*np.pi, 50)
     >>> yinterp = np.interp(xvals, x, y)
     >>> import matplotlib.pyplot as plt
     >>> plt.plot(x, y, 'o')
     >>> plt.plot(xvals, yinterp, '-x')
     >>> plt.show()






On Jun 15, 2009, at 1:47 PM, Pauli Virtanen wrote:

> On 2009-06-15, Nils Wagner <nwagner@iam.uni-stuttgart.de> wrote:
>> Hi all,
>>
>> Is there an equivalent function to Matlab's
>> interp1(x,y,x_new) in scipy.interpolate ?
>
> Yes.
>
>>>> scipy.interpolate.interp1d([1,2,3],[4,5,6])([1.5, 2.5])
> array([ 4.5,  5.5])
>
>
> -- 
> Pauli Virtanen
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list