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

Last change on this file since 258 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
RevLine 
[157]1;+
[232]2;
[157]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;-
[242]32;
[163]33FUNCTION timecross_cov, Xd, Yd, M, nT, Ndim, Double = Double, ZERO2NAN = zero2nan
[242]34;
[150]35  compile_opt hidden
[2]36;
[242]37;Sample cross covariance function.
[150]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
[2]52;
[150]53   RETURN, res
54
55END
[242]56;
[150]57;+
[242]58;
[150]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
[226]62; kind of c_correlate but for multidimensional arrays) as a
[150]63; function of the lag (L).
[2]64;
[150]65; @categories
[157]66; Statistics
[2]67;
[242]68; @param X {in}{required} {type=array}
69; An array which last dimension is the time dimension of
[150]70; size n, float or double.
[2]71;
[242]72; @param Y {in}{required} {type=array}
73; An array which last dimension is the time dimension of
[150]74; size n, float or double.
[2]75;
[163]76; @param LAG {in}{required}{type=scalar or vector}
[242]77; A scalar or n-elements vector, in the interval [-(n-2),(n-2)],
[150]78; of type integer that specifies the absolute distance(s) between
79; indexed elements of X.
[2]80;
[150]81; @keyword COVARIANCE
[226]82; If set to a non-zero value, the sample cross
[150]83; covariance is computed.
[2]84;
[150]85; @keyword DOUBLE
86; If set to a non-zero value, computations are done in
87; double precision arithmetic.
[2]88;
[150]89; @examples
[226]90;
[242]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]
[2]94;
[242]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)
[2]98;
[242]99; The result should be:
100; [-0.428246, 0.914755, 0.674547, -0.405140, -0.403100, -0.339685]
[2]101;
[150]102; @history
[157]103;       - 01/03/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
[2]104;       Based on the C_CORRELATE procedure of IDL
[226]105;       - August 2003 Sebastien Masson
[21]106;       update according to the update made in C_CORRELATE by
107;       W. Biagiotti and available in IDL 5.5
[2]108;
[150]109;       INTRODUCTION TO STATISTICAL TIME SERIES
110;       Wayne A. Fuller
111;       ISBN 0-471-28715-6
[2]112;
[150]113; @version
114; $Id$
115;
116;-
[242]117;
[163]118FUNCTION c_timecorrelate, X, Y, Lag, Covariance = Covariance, Double = Double
[242]119;
[2]120
[226]121;Compute the sample cross correlation or cross covariance of
[2]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 $
[242]132    ras = report("X and Y arrays must have the same size and the same dimensions")
[2]133
134;Check length.
135   if nt lt 2 then $
[242]136    ras = report("Time dimension of X and Y arrays must contain 2 or more elements.")
[226]137
[2]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
[21]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
[2]154   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
155
156   case NDim of
[21]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)
[2]163   endcase
164
[21]165   if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Cross  Crossation.
[2]166      for k = 0, nLag-1 do begin
[226]167         if Lag[k] ge 0 then BEGIN
[2]168            case NDim of
[21]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)
[226]172               4: Cross[*, *, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double)
[21]173             endcase
[226]174         ENDIF else BEGIN
[2]175            case NDim of
[21]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
[226]181         ENDELSE
[21]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))
[2]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
[21]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
[2]194            ENDCASE
[226]195         ENDIF else BEGIN
[2]196            case NDim of
[21]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
[2]201            ENDCASE
[226]202         ENDELSE
[2]203      endfor
204   endelse
205
[21]206   if Double eq 0 then RETURN, FLOAT(Cross) else RETURN,  Cross
[2]207
208END
Note: See TracBrowser for help on using the repository browser.