Discussion:
what is the dist function's mean?
(too old to reply)
lixiaoyao
20 years ago
Permalink
Is there anybody who can explain the dist for me?
I know dist(100) generate a 100X100 matrix,but what is the value for
it
come from? In the help file,they said it is the frequency,but I do not
understand.
also,what is the purpose for this function?
Thanks a lot
Haje Korth
20 years ago
Permalink
Maybe we should start copying from the manual. So here we go:

The DIST function creates an array in which each array element value is
proportional to its frequency. This array may be used for a variety of
purposes, including frequency-domain filtering.

This routine is written in the IDL language. Its source code can be found in
the file dist.pro in the lib subdirectory of the IDL distribution.

(I would post the code too, but it is copyrighted, so you will have to look
at RSI\IDLxx\lib\dist.pro yourself. Sorry for the inconvenience)

H.
Post by lixiaoyao
Is there anybody who can explain the dist for me?
I know dist(100) generate a 100X100 matrix,but what is the value for
it
come from? In the help file,they said it is the frequency,but I do not
understand.
also,what is the purpose for this function?
Thanks a lot
David Fanning
20 years ago
Permalink
When I went to graduate school I lived in the Amish
country near Lancaster, PA. I grew to greatly admire
the Amish people. They have a practice called "shunning"
that they use to put pressure on one of the flock that
has strayed from the straight and narrow. It's used
as a way of gently nudging the wayward one back into
accord with the larger community and its norms and
values.

It's something to think about. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Kenneth Bowman
20 years ago
Permalink
Post by David Fanning
When I went to graduate school I lived in the Amish
country near Lancaster, PA. I grew to greatly admire
the Amish people. They have a practice called "shunning"
that they use to put pressure on one of the flock that
has strayed from the straight and narrow. It's used
as a way of gently nudging the wayward one back into
accord with the larger community and its norms and
values.
In modern psychobabble there is a name for the opposite phenomenon:
enabling. ;-)

Ken Bowman
David Fanning
20 years ago
Permalink
Post by Kenneth Bowman
enabling. ;-)
Yeah, but you don't get the same kind of personal satisfaction. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Dick Jackson
20 years ago
Permalink
You know, I never quite understood that explanation from the manual either
(does it really make sense to anyone?), so I think of it this way:

In the array resulting from Dist(), each element is the straight-line
"distance" from either element [0, 0] or the element just *beyond* each
other corner, whichever is closest.

In these examples I add "*******" for the other hypothetical corner
elements:

IDL> print,dist(4)
0.000000 1.00000 2.00000 1.00000 *******
1.00000 1.41421 2.23607 1.41421
2.00000 2.23607 2.82843 2.23607
1.00000 1.41421 2.23607 1.41421
******* *******
IDL> print,dist(5)
0.000000 1.00000 2.00000 2.00000 1.00000 *******
1.00000 1.41421 2.23607 2.23607 1.41421
2.00000 2.23607 2.82843 2.82843 2.23607
2.00000 2.23607 2.82843 2.82843 2.23607
1.00000 1.41421 2.23607 2.23607 1.41421
******* *******

What's nice is that you can roll the rows and columns around and get a nice
array with distances from the centre point:

IDL> print,Shift(dist(5),2,2)
2.82843 2.23607 2.00000 2.23607 2.82843
2.23607 1.41421 1.00000 1.41421 2.23607
2.00000 1.00000 0.000000 1.00000 2.00000
2.23607 1.41421 1.00000 1.41421 2.23607
2.82843 2.23607 2.00000 2.23607 2.82843

Peace,
--
-Dick

Dick Jackson / ***@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
...
lixiaoyao
20 years ago
Permalink
thank you,it is really a good example to let me know what the value is?
I will check the FFT again to see what is the use of this function.
Benjamin Hornberger
20 years ago
Permalink
Post by lixiaoyao
Is there anybody who can explain the dist for me?
I know dist(100) generate a 100X100 matrix,but what is the value for
it
come from? In the help file,they said it is the frequency,but I do not
understand.
also,what is the purpose for this function?
Thanks a lot
Maybe we should admit that the manual doesn't do a good job of
explaining this function.

Dist will always return a 2-d array. "Frequency" is meant in the context
of Fourier Transforms (and maybe others). If you F.T. a 2-d array, you
can use DIST() to create an array with the spatial frequencies
corresponding to the F.T. array. The division into quadrants comes from
the way the FFT() function arranges its result. Check the help entry for
FFT for more information.

If you SHIFT() the result of DIST() by (n/2, n/2), you get an array
where each pixel value is equal to the distance from the center. This is
useful to create arrays with circular symmetry (like circular apertures).

Benjamin
lixiaoyao
20 years ago
Permalink
I still a little bit confusing about the use of it.I will read the
manual again.thank you.
David Fanning
20 years ago
Permalink
Post by lixiaoyao
I still a little bit confusing about the use of it.I will read the
manual again.thank you.
Here is an example of how it is used to build a frequency filter:

http://www.dfanning.com/ip_tips/freqfiltering.html

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Benjamin Hornberger
20 years ago
Permalink
Post by lixiaoyao
I still a little bit confusing about the use of it.I will read the
manual again.thank you.
Chapter 21, "Signal Processing", in the "Using IDL" manual is quite
helpful. It sais that if you use the FFT() function on some sampled
data, the actual frequency associated with a frequency index m is

f(m) = m / (N delta)

where N is the number of data points (in that dimension) and delta is
the sampling interval. The online help to FFT() explains which array
index corresponds to which frequency index:

0, 1, 2, ..., N/2-1, N/2, -(N/2-1), ..., -1

if you have an even number of data points. So, if you do a 2-dimensional
F.T. (say, of an N x N array), DIST() will give you the frequency
indices. Then you can create an array with spatial frequencies by

freq = dist(N, N) / (N * delta)

where delta is your real space sampling interval.

If this is what you have to work on, you should fully understand
discrete Fourier transforms before you try to do calculations in IDL. I
can recommend the book "The Fast Fourier Transform" by E. Oran Brigham.

Benjamin
lixiaoyao
20 years ago
Permalink
yes,this is what I wanted,thank you! I am in the process to study IDL,I
want to be an expert in IDL. I have order David Fanning's book,but it
still does not reach! Thank you for youe help!
except the book,any suggestions about the how to study IDL?
Thanks
Benjamin Hornberger
20 years ago
Permalink
Post by lixiaoyao
yes,this is what I wanted,thank you! I am in the process to study IDL,I
want to be an expert in IDL. I have order David Fanning's book,but it
still does not reach! Thank you for youe help!
except the book,any suggestions about the how to study IDL?
www.dfanning.com

Look at the "tips and tricks" page. Download his full program library.
Skim through it, plus the "tips" page, every once in a while. As you
learn IDL, each time you'll understand more and find another useful
helper program. Other IDL websites with tips and more program libraries
are linked from David's.

Go to the help directory of your IDL installation, open each of the PDF
files and skim the table of contents. And remember, the first place to
look at is the help index. Type in a few keywords which are related to
your problem.

If you ask questions on this newsgroup, people are happy to help if they
see you showed some effort to read the available documentation and if
the problem is described well. Remember, they all do it voluntarily in
their free time!

Benjamin
Benjamin Hornberger
20 years ago
Permalink
Post by Benjamin Hornberger
if you do a 2-dimensional
F.T. (say, of an N x N array), DIST() will give you the frequency
indices. Then you can create an array with spatial frequencies by
freq = dist(N, N) / (N * delta)
where delta is your real space sampling interval.
As a side note, I realized now that the IDL-provided DIST function has a
limitation in that it can't handle cases which are not symmetric in X
and Y. Even though you can specify N and M separately, if you want to
create an array of frequencies by scaling DIST's result by a factor, you
can't do it unless the real-space field of view is a square. In other
words, N * dx = M * dy is required (dx, dy are the real-space sampling
intervals).

I wrote a replacement which can do this. It can also return the
frequencies (rather than frequency indices) directly if you pass
sampling intervals or the Nyquist frequency in X and Y. In case
somebody's interested ...

Benjamin

;+
; NAME:
;
; BH_DIST
;
;
; PURPOSE:
;
; This function is a more versatile replacement for the
; IDL-provided DIST() function. If a real-space sampling interval
; or a maximum (Nyquist) frequency is given, it can calculate the
; frequency array directly. The number of pixels and the sampling
; intervals can be different in the x and y directions.
;
; Note: If you only specify nx, and possibly ny, this function
; does exactly the same as the IDL-provided DIST() function.
;
;
; AUTHOR:
;
; Benjamin Hornberger
; ***@stonybrook.edu
;
;
; CATEGORY:
;
; General programming, frequency analysis
;
;
; CALLING SEQUENCE:
;
; Result = BH_DIST(nx, ny, dx, dy)
;
;
; RETURN VALUE:
;
; Returns a rectangular array in which the value of each element is
; equal to its frequency index. If dx, and optionally dy, are
; passed, the array will contain frequencies rather than frequency
; indices.
;
;
; INPUT PARAMETERS:
;
; nx: Number of pixels in the X direction.
;
;
; OPTIONAL INPUT PARAMETERS:
;
; ny: Number of pixels in the Y direction. If not passed, it will
; be set equal to nx.
;
; dx: Real-space sampling interval in the X direction. If this
; parameter is passed, the function will return an array of
; frequencies rather than frequency indices. If additionally the
; keyword FMAX is set, dx is interpreted as maximum frequency in
; the X direction (Nyquist frequency).
;
; dy: Real-space sampling interval in the Y direction. If dy is not
; passed, but dx is, it is assumed to be equal to dx. If
; additionally the keyword FMAX is set, dy is interpreted as
; maximum frequency in the Y direction (Nyquist frequency).
;
;
; INPUT KEYWORDS:
;
; CENTER: If this keyword is set, the result will be shifted so
; that the zero frequency is at nx/2, ny/2 (which means right in
; the center for odd numbers of pixels).
;
; FMAX: If this keyword is set, dx and dy are interpreted as
; maximum (Nyquist) frequencies in the X or Y direction, rather
; than real space sampling intervals. This keyword has no effect
; if neither dx nor dy are given.
;
;
; OUTPUTS KEYWORDS:
;
; FX, FY: Set these keywords to named variables which will contain
; one-dimensional arrays of frequencies in the X and Y
; directions.
;
;
; EXAMPLE:
;
; To calculate a 100 x 120 array of spatial frequencies, with
; real-space sampling intervals of 1.2 and 1.4 (arbitrary units --
; the units for the frequencies will just be the inverse):
;
; Freq = BH_DIST(100, 120, 1.2, 1.4)
;
;
; MODIFICATION HISTORY:
; Written: BH 2005-05-16
;-

FUNCTION bh_dist, nx, ny, dx, dy, $
fx=fx, fy=fy, $
fmax=fmax, $
center=center

compile_opt idl2
on_error, 2

IF n_elements(nx) EQ 0 THEN message, 'must specify at least nx'
IF n_elements(ny) EQ 0 THEN ny = nx

nx1 = long(nx)
ny1 = long(ny)

;; shift parameters
sx = keyword_set(center) ? 0 : -nx1/2
sy = keyword_set(center) ? 0 : -ny1/2

;; 1d frequency arrays
fx = shift(findgen(nx1)-nx1/2, sx)
fy = shift(findgen(ny1)-ny1/2, sy)

;; If sampling intervals are passed, we calculate frequencies rather
;; than frequency indices. If only dx has been passed, dy is assumed
;; to be the same. If the keyword FMAX is set, we take dx and dy as
;; maximum (Nyquist) frequencies.
IF n_elements(dx) GT 0 THEN BEGIN
IF n_elements(dy) EQ 0 THEN dy = dx
fx *= (keyword_set(fmax) ? (1.*dx/(nx/2)) : (1./(nx1*dx)))
fy *= (keyword_set(fmax) ? (1.*dy/(ny/2)) : (1./(ny1*dy)))
ENDIF

;; 2d frequency arrays
fx2d = rebin(fx, nx1, ny1)
fy2d = rebin(reform(fy, 1, ny1), nx1, ny1)

return, sqrt(fx2d^2.+fy2d^2.)

END

Continue reading on narkive:
Loading...