[SciPy-User] Replace duplicates in a monotonically increasing array with linearly interpolated values.

Gökhan Sever gokhansever@gmail....
Fri Sep 10 14:48:34 CDT 2010


On Fri, Sep 10, 2010 at 1:26 PM, Dharhas Pothina <
Dharhas.Pothina@twdb.state.tx.us> wrote:

> Hi,
>
> I have an monotonically increasing array with duplicates in it. e.g.
>
> x = np.array([1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,5.0,5.0,5.0,5.0,6.0])
>
> I need a new array of the same size with linearly interpolated values i.e
> something like
>
> np.array([1.0, 1.33, 1.67, 2.0, 2.5, 3.0, 3.33, 3.67, 4.0, 4.5, 5.0, 5.25,
> 5.5, 5.75, 6.0])
>
> Is there an efficient way to do this. Right now I'm looping through the
> array and maintaining position flags of when the value changes and then
> doing linear interpolation between the start and end flags before resetting
> the flags moving to the next section. This is pretty slow.
>
> I realize there will be a problem on how to deal with duplicates at the end
> of the array.
>
> thanks
>
> - dharhas
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

Hello again,

How did you choose your interpolation method? Are required to go through
each switch point? If not you can try the approach below. (Try in ipython
-pylab)

from scipy.interpolate import interp1d
x = np.array([1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,5.0,5.0,5.0,5.0,6.0])
f = interp1d(range(len(np.unique(x))), np.unique(x))
xnew = np.linspace(0,len(np.unique(x))-1,len(x)); f(xnew)
plt.figure(); plt.plot(range(len(x)), x, 'o', range(len(x)), f(xnew), '-')

#Comparing to your case
plt.figure(); plt.plot(x, "o", y, "-")


-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100910/c147a3bd/attachment.html 


More information about the SciPy-User mailing list