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

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

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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