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

James Carroll mrmaple at gmail.com
Thu Jun 22 12:22:35 CDT 2006


Fantastic, Thanks Favre-Nicolin and Gerald!

I generalized this just a bit to:

def makeGaussian(size, fwhm = 3):
    """ Make a square gaussian kernel.

    size is the length of a side of the square
    fwhm is full-width-half-maximum, which
    can be thought of as an effective radius.
    """

    x = arange(0, size, 1, Float32)
    y = x[:,NewAxis]
    x0 = y0 = size // 2
    return exp(-4*log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2)
#

which suits my newbie need to spell out what fwhm is..


On 6/22/06, Favre-Nicolin Vincent <vincefn at users.sourceforge.net> wrote:
> > 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