source: trunk/SRC/ToBeReviewed/STATISTICS/a_correlate2d.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6; Statistics
7;
8; @param X {in}{required}
9; An 2 dimension Array [nx,ny]
10;
11; @param LAG {in}{required}
12; 2-element vector, in the intervals [-(nx-2), (nx-2)],[-(ny-2), (ny-2)],
13; of type integer that specifies the absolute distance(s) between
14; indexed elements of X.
15;
16; @keyword ZERO2NAN
17;
18; @keyword DOUBLE
19; If set to a non-zero value, computations are done in double precision arithmetic.
20;
21; @history
22; 28/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
23; Based on the A_CORRELATE procedure of IDL
24;
25; @version
26; $Id$
27;
28;-
29FUNCTION auto_cov2d, x, lag, DOUBLE=double, ZERO2NAN=zero2nan
30;
31  compile_opt idl2, strictarrsubs
32;
33   XDim = SIZE(X, /dimensions)
34   nx = XDim[0]
35   ny = XDim[1]
36;Sample autocovariance function
37   Xmean = TOTAL(X, Double = Double) / (1.*nx*ny)
38;
39   res = TOTAL( (X[0:nx-1-lag[0], 0:ny-1-lag[1]] - Xmean) * $
40                (X[lag[0]:nx-1, lag[1]:ny-1] - Xmean) $
41                , Double = Double )
42   if keyword_set(zero2nan) AND res EQ 0 then res = !values.f_nan
43   RETURN, res
44
45END
46;+
47;
48; @file_comments
49; This function computes the autocorrelation Px(K,L) or
50; autocovariance Rx(K,L) of a sample population X[nx,ny] as a
51; function of the lag (K,L).
52;
53; @categories
54; Statistics
55;
56; @param X {in}{required}
57; An 2 dimension Array [nx,ny]
58;
59; @param LAG {in}{required}
60; 2-element vector, in the intervals [-(nx-2), (nx-2)],[-(ny-2), (ny-2)],
61; of type integer that specifies the absolute distance(s) between
62; indexed elements of X.
63;
64; @keyword COVARIANCE
65; If set to a non-zero value, the sample autocovariance is computed.
66;
67; @keyword DOUBLE
68; If set to a non-zero value, computations are done in double precision arithmetic.
69;
70; @history
71; 28/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
72; Based on the A_CORRELATE procedure of IDL
73;
74; @version
75; $Id$
76;
77;-
78FUNCTION a_correlate2d, x, lag, COVARIANCE=covariance, DOUBLE=double
79;
80  compile_opt idl2, strictarrsubs
81;
82
83;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
84;as a function of the lag (l).
85
86   ON_ERROR, 2
87
88   XDim = SIZE(X, /dimensions)
89   XNDim = SIZE(X, /n_dimensions)
90   nx = XDim[0]
91   ny = XDim[1]
92   if XNDim NE 2 then $
93    ras = report("X array must contain 2 dimensions.")
94;Check length.
95   if nx lt 2 then $
96    ras = report("first dimension of X array must contain 2 or more elements.")
97   if ny lt 2 then $
98    ras = report("second dimension of X array must contain 2 or more elements.")
99   if n_elements(Lag) NE 2 THEN $
100    ras = report("Lag array must contain 2 elements.")
101
102;If the DOUBLE keyword is not set then the internal precision and
103;result are identical to the type of input.
104   if N_ELEMENTS(Double) eq 0 then $
105    Double = (SIZE(X, /type) eq 5)
106
107   if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation.
108      Auto = Auto_Cov2d(X, ABS(Lag), Double = Double) / $
109          Auto_Cov2d(X, [0L, 0L], Double = Double, /zero2nan)
110   endif else begin             ;Compute Autocovariance.
111      Auto = Auto_Cov2d(X, ABS(Lag), Double = Double) / n_elements(X)
112   endelse
113
114   if Double eq 0 then RETURN, FLOAT(Auto) else $
115    RETURN, Auto
116
117END
Note: See TracBrowser for help on using the repository browser.