Why Does Matlab Interp1 Produce Different Results Than Numpy Interp?
EDIT: Code edited to produce results consistent with Matlab. See below. I am converting Matlab scripts to Python and the linear interpolation results are different in certain cases
Solution 1:
It appears as if Matlab includes an additional equality check in it's interpolation.
Linear 1-D interpolation is generally done by finding two x values which span the input value x
and then calculating the result as:
y = y1 + (y2-y1)*(x-x1)/(x2-x1)
If you pass in an x
value which is exactly equal to one of the input x coordinates, the routine will generally calculate the correct value since x-x1
will be zero. However, if your input array has a nan
as y1
or y2
these will propagate to the result.
Based on the code you posted, my best guess would be that Matlab's interpolation function has an additional check that is something like:
if x == x1:
return y1
and that the numpy function does not have this check.
To achieve the same effect in numpy you could do:
np.where(t == tin,xin,np.interp(t,tin,xin))
Post a Comment for "Why Does Matlab Interp1 Produce Different Results Than Numpy Interp?"