source: trunk/SRC/ToBeReviewed/STATISTICS/c_timecorrelate.pro @ 325

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6; Statistics
7;
8; @param XD
9;
10; @param YD
11;
12; @param M
13;
14; @param NT
15;
16; @param NDIM
17;
18; @keyword ZERO2NAN
19;
20; @keyword DOUBLE
21; If set to a non-zero value, computations are done in
22; double precision arithmetic.
23;
24; @examples
25;
26; @history
27;
28; @version
29; $Id$
30;
31;-
32FUNCTION timecross_cov, xd, yd, m, nt, ndim, DOUBLE = double, ZERO2NAN = zero2nan
33;
34  compile_opt hidden
35;
36;Sample cross covariance function.
37   case Ndim OF
38      1:res = TOTAL(Xd[0:nT - M - 1L] * Yd[M:nT - 1L] $
39                    , Double = Double)
40      2:res = TOTAL(Xd[*, 0:nT - M - 1L] * Yd[*, M:nT - 1L] $
41                    , Ndim, Double = Double)
42      3:res = TOTAL(Xd[*, *, 0:nT - M - 1L] * Yd[*, *, M:nT - 1L] $
43                    , Ndim, Double = Double)
44      4:res = TOTAL(Xd[*, *, *, 0:nT - M - 1L] * Yd[*, *, *, M:nT - 1L] $
45                    , Ndim, Double = Double)
46   ENDCASE
47   if keyword_set(zero2nan) then begin
48      zero = where(res EQ 0)
49      if zero[0] NE -1 then res[zero] = !values.f_nan
50   ENDIF
51;
52   RETURN, res
53
54END
55;
56;+
57;
58; @file_comments
59; This function computes the "time cross correlation" Pxy(L) or
60; the "time cross covariance" between 2 arrays (this is some
61; kind of c_correlate but for multidimensional arrays) as a
62; function of the lag (L).
63;
64; @categories
65; Statistics
66;
67; @param X {in}{required} {type=array}
68; An array which last dimension is the time dimension of
69; size n, float or double.
70;
71; @param Y {in}{required} {type=array}
72; An array which last dimension is the time dimension of
73; size n, float or double.
74;
75; @param LAG {in}{required}{type=scalar or vector}
76; A scalar or n-elements vector, in the interval [-(n-2),(n-2)],
77; of type integer that specifies the absolute distance(s) between
78; indexed elements of X.
79;
80; @keyword COVARIANCE
81; If set to a non-zero value, the sample cross
82; covariance is computed.
83;
84; @keyword DOUBLE
85; If set to a non-zero value, computations are done in
86; double precision arithmetic.
87;
88; @examples
89;
90; Define two n-elements sample populations.
91; IDL> x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
92; IDL> y = [2.31, 2.76, 3.02, 3.13, 3.72, 3.88, 3.97, 4.39, 4.34, 3.95]
93;
94; Compute the cross correlation of X and Y for LAG = -5, 0, 1, 5, 6, 7
95; IDL> lag = [-5, 0, 1, 5, 6, 7]
96; IDL> result = c_timecorrelate(x, y, lag)
97;
98; The result should be:
99; [-0.428246, 0.914755, 0.674547, -0.405140, -0.403100, -0.339685]
100;
101; @history
102;       - 01/03/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
103;       Based on the C_CORRELATE procedure of IDL
104;       - August 2003 Sebastien Masson
105;       update according to the update made in C_CORRELATE by
106;       W. Biagiotti and available in IDL 5.5
107;
108;       INTRODUCTION TO STATISTICAL TIME SERIES
109;       Wayne A. Fuller
110;       ISBN 0-471-28715-6
111;
112; @version
113; $Id$
114;
115;-
116FUNCTION c_timecorrelate, x, y, lag, COVARIANCE = covariance, DOUBLE = double
117;
118
119;Compute the sample cross correlation or cross covariance of
120;(Xt, Xt+l) and (Yt, Yt+l) as a function of the lag (l).
121
122   ON_ERROR, 2
123
124   xsize = SIZE(X)
125   ysize = SIZE(Y)
126   nt = float(xsize[xsize[0]])
127   NDim = xsize[0]
128
129   if total(xsize[0:xsize[0]] NE ysize[0:ysize[0]]) NE 0 then $
130    ras = report("X and Y arrays must have the same size and the same dimensions")
131
132;Check length.
133   if nt lt 2 then $
134    ras = report("Time dimension of X and Y arrays must contain 2 or more elements.")
135
136;If the DOUBLE keyword is not set then the internal precision and
137;result are identical to the type of input.
138   if N_ELEMENTS(Double) eq 0 then $
139    Double = (Xsize[Xsize[0]+1] eq 5 or ysize[ysize[0]+1] eq 5)
140
141   if n_elements(lag) EQ 0 then lag = 0
142   nLag = N_ELEMENTS(Lag)
143
144;Deviations
145   if double then one = 1.0d ELSE one = 1.0
146   Ndim = size(X, /n_dimensions)
147   Xd = TOTAL(X, Ndim, Double = Double) / nT
148   Xd = X - Xd[*]#replicate(one,  nT)
149   Yd = TOTAL(Y, Ndim, Double = Double) / nT
150   Yd = Y - Yd[*]#replicate(one,  nT)
151
152   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
153
154   case NDim of
155      1:if Double eq 0 then  Cross = FLTARR(nLag) else  Cross = DBLARR(nLag)
156      2:if Double eq 0 then  Cross = FLTARR(Xsize[1], nLag) else  Cross = DBLARR(Xsize[1], nLag)
157      3:if Double eq 0 then  Cross = FLTARR(Xsize[1], Xsize[2], nLag) $
158      else  Cross = DBLARR(Xsize[1], Xsize[2], nLag)
159      4:if Double eq 0 then  Cross = FLTARR(Xsize[1], Xsize[2], Xsize[3], nLag) $
160      else  Cross = DBLARR(Xsize[1], Xsize[2], Xsize[3], nLag)
161   endcase
162
163   if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Cross  Crossation.
164      for k = 0, nLag-1 do begin
165         if Lag[k] ge 0 then BEGIN
166            case NDim of
167               1: Cross[k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double)
168               2: Cross[*, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double)
169               3: Cross[*, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double)
170               4: Cross[*, *, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double)
171             endcase
172         ENDIF else BEGIN
173            case NDim of
174               1: Cross[k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double)
175               2: Cross[*, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double)
176               3: Cross[*, *, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double)
177               4: Cross[*, *, *, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double)
178             endcase
179         ENDELSE
180       ENDFOR
181       div = sqrt(TimeCross_Cov(Xd, Xd, 0L, nT, Ndim, Double = Double, /zero2nan) * $
182                  TimeCross_Cov(Yd, Yd, 0L, nT, Ndim, Double = Double, /zero2nan))
183       Cross = temporary(Cross)/((temporary(div))[*]#replicate(one, nLag))
184   endif else begin             ;Compute Cross Covariance.
185      for k = 0, nLag-1 do begin
186         if Lag[k] ge 0 then BEGIN
187            case NDim of
188               1: Cross[k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT
189               2: Cross[*, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT
190               3: Cross[*, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT
191               4: Cross[*, *, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT
192            ENDCASE
193         ENDIF else BEGIN
194            case NDim of
195               1: Cross[k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT
196               2: Cross[*, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT
197               3: Cross[*, *, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT
198               4: Cross[*, *, *, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT
199            ENDCASE
200         ENDELSE
201      endfor
202   endelse
203
204   if Double eq 0 then RETURN, FLOAT(Cross) else RETURN,  Cross
205
206END
Note: See TracBrowser for help on using the repository browser.