[Numpy-discussion] Nieghbourhood functions

Scott Ransom ransom at cfa.harvard.edu
Sun Apr 8 20:46:45 CDT 2001


Hi Robert,

Isn't this just a convolution?  For your first order case you are
convolving the array with a kernel that looks like:

0 1 0
1 0 1
0 1 0

and for second order:

1 1 1
1 0 1
1 1 1

If this is what you are looking for, there is a convolve function built
in to Numeric but it is for rank-1 arrays only.  You can use 2D FFTs for
a 2D case -- although the efficiency won't be the greatest unless you
can use the FFT of the kernel array and/or the data array over and over
again (since in general a convolution by FFTs takes 3 FFTs -- 1 for the
data, 1 for the kernel, and 1 inverse one after you have multiplied the
first two together).  It would work well for higher order cases...(but
beware of the wrapping that goes on accross the boundaries!)

For lower order cases or when you can't re-use the FFTs, you'll probably
want a brute force technique -- which I'll leave for someone else...

Scott

Robert.Denham at dnr.qld.gov.au wrote:
> 
> I am looking for efficient ways to code neighbourhood functions.  For
> example a neighbourhod add for an element in an array will simply be the sum
> of the neighbours:
> 
> 1 0 2
> 3 x 3   , then x becomes 7 (first order neighbour), 11 (2nd order) etc.
> 1 1 0
> 
> I would be interested in efficient ways of doing this for a whole array,
> something like  a_nsum = neighbour_sum(a, order=1), where each element in
> a_nsum is the sum of the corresponding element in a.

-- 
Scott M. Ransom                   Address:  Harvard-Smithsonian CfA
Phone:  (617) 495-4142                      60 Garden St.  MS 10 
email:  ransom at cfa.harvard.edu              Cambridge, MA  02138
GPG Fingerprint: 06A9 9553 78BE 16DB 407B  FFCA 9BFA B6FF FFD3 2989




More information about the Numpy-discussion mailing list