[SciPy-user] Creating a 2D matrix with a gausian hump?

Favre-Nicolin Vincent vincefn at users.sourceforge.net
Thu Jun 22 10:24:29 CDT 2006


> What's a good way to create a 2d gaussian kernel with numpy?
>
> I'm trying to create a 9x9 matrix with 1.0 in the center, and 0.0 in
> the corners, and a gaussian distribution from the center out.

from scipy import *
x=arange(0,9,1,Float)
x=arange(0,9,1,Float)
y=x[:,NewAxis]
x0,y0=4,4
fwhm=3
g=exp(-4*log(2)*((x-x0)**2+(y-y0)**2)/fwhm**2)
print g

   If you want exactly 0 in the corners, just use g-g[0,0].

-- 
Vincent Favre-Nicolin
Université Joseph Fourier
http://v.favrenicolin.free.fr
ObjCryst & Fox : http://objcryst.sourceforge.net



More information about the SciPy-user mailing list