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

Last change on this file since 150 was 150, checked in by navarro, 18 years ago

english and nicer header (3a)

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