Re: realizing the formula in idl [message #45399] |
Fri, 09 September 2005 07:44 |
pravesh.subramanian
Messages: 21 Registered: August 2005
|
Junior Member |
|
|
Hello,
Thanks for all the replies. they were really helpful
i did mean that it is a gaussian kernel , actually of size 192 * 192
where x and y will be the indices of this kernel
hence, i need to realize a 2D gaussian of size 192 filled at all
positions.
I need to use to in k-space for a smoothing operation. From the
responses, I gather that suggestions 1 and 2 could be appropriate.
Does that sound ok?
My sincere thanks,
pravs
|
|
|
Re: realizing the formula in idl [message #45400 is a reply to message #45399] |
Fri, 09 September 2005 07:14  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
pravesh.subramanian@gmail.com wrote:
> Hello,
> How do I realize this formula in IDL?
>
> matrix(x, y) = 1/(s^2 * 2 * pi) * Exp (-(x^2 + y^2)/2 * s^2)
>
> let's say x = y = 300
The other people who've responded have assumed that what you mean is
that matrix is a 300x300 array, and that you want to fill in the entire
array. That seems likely, but there's an alternative interpretation of
your message, for which the answer is different. If you merely want to
set one element of the array to this value, then what you've already
written is almost exactly the correct IDL translation:
matrix[x, y] = 1/(s^2 * 2 * !dpi) * Exp (-(x^2 + y^2)/2 * s^2)
Even the use of the square brackets "[]" isn't strictly necessary, I
just think it's clearer.
|
|
|
Re: realizing the formula in idl [message #45403 is a reply to message #45400] |
Fri, 09 September 2005 00:57  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
pravesh.subramanian@gmail.com writes:
> Hello,
> How do I realize this formula in IDL?
>
> matrix(x, y) = 1/(s^2 * 2 * pi) * Exp (-(x^2 + y^2)/2 * s^2)
>
> let's say x = y = 300
Greetings, your question is not quite complete. Are X and Y indices,
or are they the independent variables of the function?
Assuming you have a 2D function, and you have the grid positions that
you want to sample as 1D arrays in X and Y, then first I typically
make a 2D array of grid positions,
XX = X # (Y*0+1)
YY = (X*0+1) # Y
Then it's a simple matter to compute the function almost exactly as
you wrote it,
MATRIX = 1/(s^2 * 2 * !dpi) * Exp (-(xx^2 + yy^2)/2 * s^2)
If you want to avoid underflow errors, then there are two extra steps
to mask them out,
U = -(xx^2 + yy^2)/2 * s^2
MASK = ABS(U) LT 60 ;; May need to tweak this number
MATRIX = MASK/(s^2 * 2 * !dpi) * Exp (-(xx^2 + yy^2)/2 * s^2 * MASK)
Good luck,
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: realizing the formula in idl [message #45405 is a reply to message #45403] |
Fri, 09 September 2005 01:40  |
Timm Weitkamp
Messages: 66 Registered: August 2002
|
Member |
|
|
pravesh.subramanian@gmail.com wrote:
> How do I realize this formula in IDL?
>
> matrix(x, y) = 1/(s^2 * 2 * pi) * Exp (-(x^2 + y^2)/2 * s^2)
>
> let's say x = y = 300
I assume that by "x = y = 300" you actually mean that x and y are 1D
arrays with approximately 300 elements each, right? The formula then
describes a 2D Gaussian with width "s". Here is a way of doing it:
s = 100.0 ; or whatever other value you want for the width
nx = 300 ; number of elements of x
ny = 300 ; number of elements of y
x = FINDGEN(nx) ; your x values
y = FINDGEN(ny) ; your y values
; Blow x and y up to two dimensions
xx = x # (1.0 + FLTARR(ny))
yy = y ## (1.0 + FLTARR(nx))
; ... and then the calculation of the Gaussian is as easy as this:
matrix = 1.0 / (s^2 * 2 * !PI) * EXP(-(xx^2 + yy^2) / (2.0 * s^2))
Hope this helps,
Timm
Timm Weitkamp
ESRF, Grenoble, France
|
|
|