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

Last change on this file since 261 was 242, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers + replace some message by some report

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