source: trunk/STATISTICS/c2_correlate.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1; $Id$
2;
3; Copyright (c) 1995-1997, Research Systems, Inc.  All rights reserved.
4;       Unauthorized reproduction prohibited.
5;+
6; NAME:
7;       c2_correlate
8;
9; PURPOSE:
10;       This function computes the cross correlation Pxy(L) or cross
11;       covariance Rxy(L) of two sample populations X and Y as a function
12;       of the lag (L).
13;
14; CATEGORY:
15;       Statistics.
16;
17; CALLING SEQUENCE:
18;       Result = c2_correlate(X, Y, Lag)
19;
20; INPUTS:
21;       X:    An n-element vector of type integer, float or double.
22;
23;       Y:    An n-element vector of type integer, float or double.
24;
25;     LAG:    A scalar or n-element vector, in the interval [-(n-2), (n-2)],
26;             of type integer that specifies the absolute distance(s) between
27;             indexed elements of X.
28;
29; KEYWORD PARAMETERS:
30;       COVARIANCE:    If set to a non-zero value, the sample cross
31;                      covariance is computed.
32;
33;       DOUBLE:        If set to a non-zero value, computations are done in
34;                      double precision arithmetic.
35;
36; EXAMPLE
37;       Define two n-element sample populations.
38;         x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
39;         y = [2.31, 2.76, 3.02, 3.13, 3.72, 3.88, 3.97, 4.39, 4.34, 3.95]
40;
41;       Compute the cross correlation of X and Y for LAG = -5, 0, 1, 5, 6, 7
42;         lag = [-5, 0, 1, 5, 6, 7]
43;         result = c2_correlate(x, y, lag)
44;
45;       The result should be:
46;         [-0.428246, 0.914755, 0.674547, -0.405140, -0.403100, -0.339685]
47;
48; PROCEDURE:
49;       See computational formula published in IDL manual.
50;
51; REFERENCE:
52;       INTRODUCTION TO STATISTICAL TIME SERIES
53;       Wayne A. Fuller
54;       ISBN 0-471-28715-6
55;
56; MODIFICATION HISTORY:
57;       Written by:  GGS, RSI, October 1994
58;       Modified:    GGS, RSI, August 1995
59;                    Corrected a condition which excluded the last term of the
60;                    time-series.
61;       Modified:    GGS, RSI, April 1996
62;                    Simplified CROSS_COV function. Added DOUBLE keyword.
63;                    Modified keyword checking and use of double precision.
64;       Modified:    W. Biagiotti,  Advanced Testing Technologies Inc., Hauppauge, NY, July 1997
65;                    Moved all constant calculations out of main loop for greatly
66;                    reduced processing time.
67;
68;
69; DISCLAIMER:  This routine has been modified from its original form as it was
70;              supplied by Research Systems, Inc (RSI).  As such, RSI is not responsible
71;              for any errors existing in this code.
72;-
73
74FUNCTION Cross_Cov, X, Y, M, nX, Double = Double
75  ;Sample cross covariance function.
76
77  Xmean = TOTAL(X, Double = Double) / nX
78  Ymean = TOTAL(Y, Double = Double) / nX
79  RETURN, TOTAL((X[0:nX - M - 1L] - Xmean) * (Y[M:nX - 1L] - Ymean), $
80                 Double = Double)
81
82END
83
84FUNCTION c2_correlate, X, Y, Lag, Covariance = Covariance, Double = Double
85
86  ;Compute the sample cross correlation or cross covariance of
87  ;(Xt, Xt+l) and (Yt, Yt+l) as a function of the lag (l).
88
89  ON_ERROR, 2
90
91  TypeX = SIZE(X)
92  TypeY = SIZE(Y)
93  nX = TypeX[TypeX[0]+2]
94  nY = TypeY[TypeY[0]+2]
95
96  if nX ne nY then $
97    MESSAGE, "X and Y arrays must have the same number of elements."
98
99  ;Check length.
100  if nX lt 2 then $
101    MESSAGE, "X and Y arrays must contain 2 or more elements."
102
103  ;If the DOUBLE keyword is not set then the internal precision and
104  ;result are identical to the type of input.
105  if N_ELEMENTS(Double) eq 0 then $
106    Double = (TypeX[TypeX[0]+1] eq 5 or TypeY[TypeY[0]+1] eq 5)
107
108  nLag = N_ELEMENTS(Lag)
109
110  if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
111
112  if Double eq 0 then Cross = FLTARR(nLag) else Cross = DBLARR(nLag)
113
114  if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Cross Correlation.
115
116    denom = SQRT(Cross_Cov(X, X, 0L, nX, Double = Double) * $
117                   Cross_Cov(Y, Y, 0L, nY, Double = Double))
118
119    for k = 0, nLag-1 do begin
120      if Lag[k] ge 0 $
121        then Cross[k] = Cross_Cov(x, y, Lag[k], nX, Double = Double) / denom  $
122        else Cross[k] = Cross_Cov(Y, X, ABS(Lag[k]), nY, Double = Double) / denom
123
124    endfor
125  endif else begin ;Compute Cross Covariance.
126    for k = 0, nLag-1 do begin
127      if Lag[k] ge 0 then $
128        Cross[k] = Cross_Cov(X, Y, Lag[k], nX, Double = Double) / nX $
129      else Cross[k] = Cross_Cov(Y, X, ABS(Lag[k]), nX, Double = Double) / nX
130    endfor
131  endelse
132
133  if Double eq 0 then RETURN, FLOAT(Cross) else $
134  RETURN, Cross
135
136END
137
Note: See TracBrowser for help on using the repository browser.