logo       

Re: rebin: msg#00073

python.numeric.general

Subject: Re: rebin

At 9:14 AM -0400 2004-08-30, Perry Greenfield wrote:
On Aug 27, 2004, at 8:34 PM, Russell E. Owen wrote:

Any suggestions on an efficient means to bin a 2-d array?
...
For example, to 2x2 bin a two-dimensional image, one would:
average (0,0), (0,1), (1,0), (1,1) to form (0,0)
average (0,2), (0,3), (1,2), (1,3) to form (0,1)
...

Note that a boxcar smoothing costs no more than doing the above averaging.
So in numarray, you could do the following:

from numarray.convolve import boxcar
b = boxcar(a, (n,n))
rebinnedarray = b[::n,::n].copy()

or something like this (I haven't tried to figure out the correct offset
for the slice) where one wants to rebin by a factor of n in both dimensions.
We should probably add a built in function to do this.

Thanks! Great suggestion!

I think the polished version (for nxm binning) is:

from numarray.convolve import boxcar
b = boxcar(a, (n,m), mode='constant', cval=0)
rebinnedarray = b[n//2::n,m//2::m].copy()

A rebin function would be handy since using boxcar is a bit tricky.

-- Russell


-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise