[SciPy-User] 2d interpolation
Jochen
cycomanic at gmail.com
Thu Aug 6 00:51:40 EDT 2009
Hi Johann,
my current code looks like this:
def resample(I, t, f, N=256, fcentre=None, tcentre=None):
"""
Resample the spectrogramm so that we are on a dt=1/(N*df) grid.
Returns the resampled spectrogramm, time and frequency array.
Parameters:
M -- the spectrogramm [array(len(t),len(f))]
t -- the original time vector of the spectrogram
f -- the original frequency vector of the spectrogram
N -- number of points to resample to to [int] (default=256)
fcenter -- index of the centre frequency of the spectrogramm
if None take len(f)/2 (default=None)
tcentre -- index of the centre time of the spectrogramm
if None take len(t)/2 (default=None)
"""
tmax = abs(t[-1]-t[0])
tnew = np.linspace(t[0], t[-1], N)
fnew = np.arange(-N/2,N/2)*(1./tmax)
if fcentre:
fnew = fnew + f[fcentre]
else:
fnew = fnew + f[len(f)/2]
if tcentre:
tnew = tnew - tnew[len(tnew)/2] + t[tcentre]
ind_tn = interp(tnew, t, np.arange(len(t)),left=-1,right=len(t)+1)
ind_fn = interp(fnew, f[::-1], np.arange(len(f)),left=-1, right=len(f)+1)
Ind_tn, Ind_fn = np.meshgrid(ind_tn,ind_fn)
Inew = map_coordinates(I, [Ind_tn, Ind_fn])
return Inew, tnew ,fnew[::-1]
I tried simply using something like
inter = interp2d(t,f,I)
Inew = inter(tnew,fnew)
but the creation of the interpolation object is so slow it's unusable.
Cheers
Jochen
On Thu, 06 Aug 2009 05:21:20 +0200
Johann Cohen-Tanugi <cohen at lpta.in2p3.fr> wrote:
> Hi Jochen,
> best would probably be to post your code somewhere. With the scarse
> decription below, it is hard to give you potentially useful
> feedback :) best,
> Johann
>
>
> Jochen wrote:
> > Hi all,
> >
> > I've got a 2D field z which is a function of arrays x and y. Now I
> > want to interpolate it to two new arrays xn and yn. I've tried using
> > scipy.interpolate.interp2d, but I get some weird artifacts for
> > smaller fields or for larger fields (200x200) it takes so long that
> > I have to kill it after a while. Is it expected to be this slow?
> > Currently I've hacked a solution using
> > scipy.ndimage.interpolation.map_coordinates which is fast but seems
> > quite hackish to me? Anybody have a better solution to do this?
> >
> > Cheers
> > Jochen
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list