[157] | 1 | ;+ |
---|
| 2 | ; |
---|
| 3 | ; @file_comments |
---|
| 4 | ; |
---|
| 5 | ; |
---|
| 6 | ; @categories |
---|
| 7 | ; Statistics |
---|
| 8 | ; |
---|
| 9 | ; @param X {in}{required} |
---|
| 10 | ; An 2 dimension Array [nx,ny] |
---|
| 11 | ; |
---|
| 12 | ; @param LAG {in}{required} |
---|
| 13 | ; 2-element vector, in the intervals [-(nx-2), (nx-2)],[-(ny-2), (ny-2)], |
---|
| 14 | ; of type integer that specifies the absolute distance(s) between |
---|
| 15 | ; indexed elements of X. |
---|
| 16 | ; |
---|
| 17 | ; @keyword ZERO2NAN |
---|
| 18 | ; |
---|
| 19 | ; @keyword DOUBLE |
---|
| 20 | ; If set to a non-zero value, computations are done in double precision arithmetic. |
---|
| 21 | ; |
---|
| 22 | ; @history |
---|
| 23 | ; 28/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 24 | ; Based on the A_CORRELATE procedure of IDL |
---|
| 25 | ; |
---|
| 26 | ; @version |
---|
| 27 | ; $Id$ |
---|
| 28 | ; |
---|
| 29 | ;- |
---|
[2] | 30 | |
---|
[163] | 31 | FUNCTION auto_cov2d, X, Lag, Double = Double, zero2nan = zero2nan |
---|
[114] | 32 | ; |
---|
| 33 | compile_opt idl2, strictarrsubs |
---|
| 34 | ; |
---|
[2] | 35 | XDim = SIZE(X, /dimensions) |
---|
| 36 | nx = XDim[0] |
---|
| 37 | ny = XDim[1] |
---|
| 38 | ;Sample autocovariance function |
---|
| 39 | Xmean = TOTAL(X, Double = Double) / (1.*nx*ny) |
---|
| 40 | ; |
---|
| 41 | res = TOTAL( (X[0:nx-1-lag[0], 0:ny-1-lag[1]] - Xmean) * $ |
---|
| 42 | (X[lag[0]:nx-1, lag[1]:ny-1] - Xmean) $ |
---|
| 43 | , Double = Double ) |
---|
| 44 | if keyword_set(zero2nan) AND res EQ 0 then res = !values.f_nan |
---|
| 45 | RETURN, res |
---|
| 46 | |
---|
| 47 | END |
---|
[150] | 48 | ;+ |
---|
| 49 | ; |
---|
| 50 | ; @file_comments |
---|
| 51 | ; This function computes the autocorrelation Px(K,L) or |
---|
| 52 | ; autocovariance Rx(K,L) of a sample population X[nx,ny] as a |
---|
| 53 | ; function of the lag (K,L). |
---|
| 54 | ; |
---|
| 55 | ; @categories |
---|
[157] | 56 | ; Statistics |
---|
[150] | 57 | ; |
---|
| 58 | ; @param X {in}{required} |
---|
| 59 | ; An 2 dimension Array [nx,ny] |
---|
| 60 | ; |
---|
| 61 | ; @param LAG {in}{required} |
---|
| 62 | ; 2-element vector, in the intervals [-(nx-2), (nx-2)],[-(ny-2), (ny-2)], |
---|
| 63 | ; of type integer that specifies the absolute distance(s) between |
---|
| 64 | ; indexed elements of X. |
---|
| 65 | ; |
---|
| 66 | ; @keyword COVARIANCE |
---|
| 67 | ; If set to a non-zero value, the sample autocovariance is computed. |
---|
| 68 | ; |
---|
| 69 | ; @keyword DOUBLE |
---|
| 70 | ; If set to a non-zero value, computations are done in double precision arithmetic. |
---|
| 71 | ; |
---|
| 72 | ; @history |
---|
[157] | 73 | ; 28/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[150] | 74 | ; Based on the A_CORRELATE procedure of IDL |
---|
| 75 | ; |
---|
| 76 | ; @version |
---|
| 77 | ; $Id$ |
---|
| 78 | ; |
---|
| 79 | ;- |
---|
[2] | 80 | |
---|
[150] | 81 | |
---|
[163] | 82 | FUNCTION a_correlate2d, X, Lag, Covariance = Covariance, Double = Double |
---|
[114] | 83 | ; |
---|
| 84 | compile_opt idl2, strictarrsubs |
---|
| 85 | ; |
---|
[2] | 86 | |
---|
| 87 | ;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l) |
---|
| 88 | ;as a function of the lag (l). |
---|
| 89 | |
---|
| 90 | ON_ERROR, 2 |
---|
| 91 | |
---|
| 92 | XDim = SIZE(X, /dimensions) |
---|
| 93 | XNDim = SIZE(X, /n_dimensions) |
---|
| 94 | nx = XDim[0] |
---|
| 95 | ny = XDim[1] |
---|
| 96 | if XNDim NE 2 then $ |
---|
| 97 | MESSAGE, "X array must contain 2 dimensions." |
---|
| 98 | ;Check length. |
---|
| 99 | if nx lt 2 then $ |
---|
| 100 | MESSAGE, "first dimension of X array must contain 2 or more elements." |
---|
| 101 | if ny lt 2 then $ |
---|
| 102 | MESSAGE, "second dimension of X array must contain 2 or more elements." |
---|
| 103 | if n_elements(Lag) NE 2 THEN $ |
---|
| 104 | MESSAGE, "Lag array must contain 2 elements." |
---|
| 105 | |
---|
| 106 | ;If the DOUBLE keyword is not set then the internal precision and |
---|
| 107 | ;result are identical to the type of input. |
---|
| 108 | if N_ELEMENTS(Double) eq 0 then $ |
---|
| 109 | Double = (SIZE(X, /type) eq 5) |
---|
| 110 | |
---|
| 111 | if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation. |
---|
| 112 | Auto = Auto_Cov2d(X, ABS(Lag), Double = Double) / $ |
---|
| 113 | Auto_Cov2d(X, [0L, 0L], Double = Double, /zero2nan) |
---|
| 114 | endif else begin ;Compute Autocovariance. |
---|
| 115 | Auto = Auto_Cov2d(X, ABS(Lag), Double = Double) / n_elements(X) |
---|
| 116 | endelse |
---|
| 117 | |
---|
| 118 | if Double eq 0 then RETURN, FLOAT(Auto) else $ |
---|
| 119 | RETURN, Auto |
---|
| 120 | |
---|
| 121 | END |
---|