g13cc calculates the smoothed sample cross spectrum of a bivariate time series using one of four lag windows: rectangular, Bartlett, Tukey or Parzen.

# Syntax

C#
public static void g13cc(
int nxy,
int mtxy,
double pxy,
int iw,
int mw,
int ish,
int ic,
int nc,
double[] cxy,
double[] cyx,
int kc,
int l,
double[] xg,
double[] yg,
out int ng,
out int ifail
)
Visual Basic
Public Shared Sub g13cc ( _
nxy As Integer, _
mtxy As Integer, _
pxy As Double, _
iw As Integer, _
mw As Integer, _
ish As Integer, _
ic As Integer, _
nc As Integer, _
cxy As Double(), _
cyx As Double(), _
kc As Integer, _
l As Integer, _
xg As Double(), _
yg As Double(), _
<OutAttribute> ByRef ng As Integer, _
<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void g13cc(
int nxy,
int mtxy,
double pxy,
int iw,
int mw,
int ish,
int ic,
int nc,
array<double>^ cxy,
array<double>^ cyx,
int kc,
int l,
array<double>^ xg,
array<double>^ yg,
[OutAttribute] int% ng,
[OutAttribute] int% ifail
)
F#
static member g13cc :
nxy : int *
mtxy : int *
pxy : float *
iw : int *
mw : int *
ish : int *
ic : int *
nc : int *
cxy : float[] *
cyx : float[] *
kc : int *
l : int *
xg : float[] *
yg : float[] *
ng : int byref *
ifail : int byref -> unit

#### Parameters

nxy
Type: System..::..Int32
On entry: $n$, the length of the time series $x$ and $y$.
Constraint: ${\mathbf{nxy}}\ge 1$.
mtxy
Type: System..::..Int32
On entry: if cross-covariances are to be calculated by the method (${\mathbf{ic}}=0$), mtxy must specify whether the data is to be initially mean or trend corrected.
${\mathbf{mtxy}}=0$
For no correction.
${\mathbf{mtxy}}=1$
For mean correction.
${\mathbf{mtxy}}=2$
For trend correction.
If cross-covariances are supplied $\left({\mathbf{ic}}\ne 0\right)$, mtxy is not used.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{mtxy}}=0$, $1$ or $2$.
pxy
Type: System..::..Double
On entry: if cross-covariances are to be calculated by the method (${\mathbf{ic}}=0$), pxy must specify the proportion of the data (totalled over both ends) to be initially tapered by the split cosine bell taper. A value of $0.0$ implies no tapering.
If cross-covariances are supplied $\left({\mathbf{ic}}\ne 0\right)$, pxy is not used.
Constraint: if ${\mathbf{ic}}=0$, $0.0\le {\mathbf{pxy}}\le 1.0$.
iw
Type: System..::..Int32
On entry: the choice of lag window.
${\mathbf{iw}}=1$
Rectangular.
${\mathbf{iw}}=2$
Bartlett.
${\mathbf{iw}}=3$
Tukey.
${\mathbf{iw}}=4$
Parzen.
Constraint: $1\le {\mathbf{iw}}\le 4$.
mw
Type: System..::..Int32
On entry: $M$, the ‘cut-off’ point of the lag window, relative to any alignment shift that has been applied. Windowed cross-covariances at lags $\left(-{\mathbf{mw}}+{\mathbf{ish}}\right)$ or less, and at lags $\left({\mathbf{mw}}+{\mathbf{ish}}\right)$ or greater are zero.
Constraints:
• ${\mathbf{mw}}\ge 1$;
• ${\mathbf{mw}}+\left|{\mathbf{ish}}\right|\le {\mathbf{nxy}}$.
ish
Type: System..::..Int32
On entry: $S$, the alignment shift between the $x$ and $y$ series. If $x$ leads $y$, the shift is positive.
Constraint: $-{\mathbf{mw}}<{\mathbf{ish}}<{\mathbf{mw}}$.
ic
Type: System..::..Int32
On entry: indicates whether cross-covariances are to be calculated in the method or supplied in the call to the method.
${\mathbf{ic}}=0$
Cross-covariances are to be calculated.
${\mathbf{ic}}\ne 0$
Cross-covariances are to be supplied.
nc
Type: System..::..Int32
On entry: the number of cross-covariances to be calculated in the method or supplied in the call to the method.
Constraint: ${\mathbf{mw}}+\left|{\mathbf{ish}}\right|\le {\mathbf{nc}}\le {\mathbf{nxy}}$.
cxy
Type: array<System..::..Double>[]()[][]
An array of size [nc]
On entry: if ${\mathbf{ic}}\ne 0$, cxy must contain the nc cross-covariances between values in the $y$ series and earlier values in time in the $x$ series, for lags from $0$ to $\left({\mathbf{nc}}-1\right)$.
If ${\mathbf{ic}}=0$, cxy need not be set.
On exit: if ${\mathbf{ic}}=0$, cxy will contain the nc calculated cross-covariances.
If ${\mathbf{ic}}\ne 0$, the contents of cxy will be unchanged.
cyx
Type: array<System..::..Double>[]()[][]
An array of size [nc]
On entry: if ${\mathbf{ic}}\ne 0$, cyx must contain the nc cross-covariances between values in the $y$ series and later values in time in the $x$ series, for lags from $0$ to $\left({\mathbf{nc}}-1\right)$.
If ${\mathbf{ic}}=0$, cyx need not be set.
On exit: if ${\mathbf{ic}}=0$, cyx will contain the nc calculated cross-covariances.
If ${\mathbf{ic}}\ne 0$, the contents of cyx will be unchanged.
kc
Type: System..::..Int32
On entry: if ${\mathbf{ic}}=0$, kc must specify the order of the fast Fourier transform (FFT) used to calculate the cross-covariances. kc should be a product of small primes such as ${2}^{m}$ where $m$ is the smallest integer such that ${2}^{m}\ge n+{\mathbf{nc}}$.
If ${\mathbf{ic}}\ne 0$, that is if covariances are supplied, kc is not used.
Constraint: ${\mathbf{kc}}\ge {\mathbf{nxy}}+{\mathbf{nc}}$. The largest prime factor of kc must not exceed $19$, and the total number of prime factors of kc, counting repetitions, must not exceed $20$. These two restrictions are imposed by the internal FFT algorithm used.
l
Type: System..::..Int32
On entry: $L$, the frequency division of the spectral estimates as $\frac{2\pi }{L}$. Therefore it is also the order of the FFT used to construct the sample spectrum from the cross-covariances. l should be a product of small primes such as ${2}^{m}$ where $m$ is the smallest integer such that ${2}^{m}\ge 2M-1$.
Constraint: ${\mathbf{l}}\ge 2×{\mathbf{mw}}-1$. The largest prime factor of l must not exceed $19$, and the total number of prime factors of l, counting repetitions, must not exceed $20$. These two restrictions are imposed by the internal FFT algorithm used.
xg
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
Note: dim1 must satisfy the constraint:
• if ${\mathbf{ic}}=0$, $\mathbf{_nxyg}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{kc}},{\mathbf{l}}\right)$;
• otherwise $\mathbf{_nxyg}\ge {\mathbf{l}}$.
On entry: if the cross-covariances are to be calculated, then xg must contain the nxy data points of the $x$ series. If covariances are supplied, xg need not be set.
On exit: contains the real parts of the ng complex spectral estimates in elements ${\mathbf{xg}}\left[0\right]$ to ${\mathbf{xg}}\left[{\mathbf{ng}}-1\right]$, and ${\mathbf{xg}}\left[{\mathbf{ng}}\right]$ to ${\mathbf{xg}}\left[\mathbf{_nxyg}-1\right]$ contain $0.0$. The $y$ series leads the $x$ series.
yg
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
Note: dim1 must satisfy the constraint:
• if ${\mathbf{ic}}=0$, $\mathbf{_nxyg}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{kc}},{\mathbf{l}}\right)$;
• otherwise $\mathbf{_nxyg}\ge {\mathbf{l}}$.
On entry: if cross-covariances are to be calculated, yg must contain the nxy data points of the $y$ series. If covariances are supplied, yg need not be set.
On exit: contains the imaginary parts of the ng complex spectral estimates in elements ${\mathbf{yg}}\left[0\right]$ to ${\mathbf{yg}}\left[{\mathbf{ng}}-1\right]$, and ${\mathbf{yg}}\left[{\mathbf{ng}}\right]$ to ${\mathbf{yg}}\left[\mathbf{_nxyg}-1\right]$ contain $0.0$. The $y$ series leads the $x$ series.
ng
Type: System..::..Int32%
On exit: the number, $\left[{\mathbf{l}}/2\right]+1$, of complex spectral estimates, whose separate parts are held in xg and yg.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

The smoothed sample cross spectrum is a complex valued function of frequency $\omega$, ${f}_{xy}\left(\omega \right)=cf\left(\omega \right)+iqf\left(\omega \right)$, defined by its real part or co-spectrum
 $cfω=12π∑k=-M+1M-1wkCxyk+Scosωk$
and imaginary part or quadrature spectrum
 $qfω=12π∑k=-M+1M-1wkCxyk+Ssinωk$
where ${w}_{\mathit{k}}={w}_{-\mathit{k}}$, for $\mathit{k}=0,1,\dots ,M-1$, is the smoothing lag window as defined in the description of g13ca. The alignment shift $S$ is recommended to be chosen as the lag $k$ at which the cross-covariances ${c}_{xy}\left(k\right)$ peak, so as to minimize bias.
The results are calculated for frequency values
 $ωj=2πjL, j=0,1,…,L/2,$
where $\left[\right]$ denotes the integer part.
The cross-covariances ${c}_{xy}\left(k\right)$ may be supplied by you, or constructed from supplied series ${x}_{1},{x}_{2},\dots ,{x}_{n}$; ${y}_{1},{y}_{2},\dots ,{y}_{n}$ as
 $cxyk=∑t=1n-kxtyt+kn, k≥0$
 $cxyk=∑t=1-knxtyt+kn=cyx-k, k<0$
this convolution being carried out using the finite Fourier transform.
The supplied series may be mean and trend corrected and tapered before calculation of the cross-covariances, in exactly the manner described in g13ca for univariate spectrum estimation. The results are corrected for any bias due to tapering.
The bandwidth associated with the estimates is not returned. It will normally already have been calculated in previous calls of g13ca for estimating the univariate spectra of ${y}_{t}$ and ${x}_{t}$.

# References

Bloomfield P (1976) Fourier Analysis of Time Series: An Introduction Wiley
Jenkins G M and Watts D G (1968) Spectral Analysis and its Applications Holden–Day

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (NXYG) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nxy}}<1$, or ${\mathbf{mtxy}}<0$ and ${\mathbf{ic}}=0$, or ${\mathbf{mtxy}}>2$ and ${\mathbf{ic}}=0$, or ${\mathbf{pxy}}<0.0$ and ${\mathbf{ic}}=0$, or ${\mathbf{pxy}}>1.0$ and ${\mathbf{ic}}=0$, or ${\mathbf{iw}}\le 0$, or ${\mathbf{iw}}>4$, or ${\mathbf{mw}}<1$, or ${\mathbf{mw}}+\left|{\mathbf{ish}}\right|>{\mathbf{nxy}}$, or $\left|{\mathbf{ish}}\right|\ge {\mathbf{mw}}$, or ${\mathbf{nc}}<{\mathbf{mw}}+\left|{\mathbf{ish}}\right|$, or ${\mathbf{nc}}>{\mathbf{nxy}}$,
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{kc}}<{\mathbf{nxy}}+{\mathbf{nc}}$, or kc has a prime factor exceeding $19$, or kc has more than $20$ prime factors, counting repetitions.
This error only occurs when ${\mathbf{ic}}=0$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{l}}<2×{\mathbf{mw}}-1$, or l has a prime factor exceeding $19$, or l has more than $20$ prime factors, counting repetitions.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The FFT is a numerically stable process, and any errors introduced during the computation will normally be insignificant compared with uncertainty in the data.

# Parallelism and Performance

None.

g13cc carries out two FFTs of length kc to calculate the sample cross-covariances and one FFT of length $L$ to calculate the sample spectrum. The timing of g13cc is therefore dependent on the choice of these values. The time taken for an FFT of length $n$ is approximately proportional to $n\mathrm{log}\left(n\right)$ (but see [Further Comments] in c06pa for further details).

# Example

This example reads two time series of length $296$. It then selects mean correction, a 10% tapering proportion, the Parzen smoothing window and a cut-off point of $35$ for the lag window. The alignment shift is set to $3$ and $50$ cross-covariances are chosen to be calculated. The program then calls g13cc to calculate the cross spectrum and then prints the cross-covariances and cross spectrum.

Example program (C#): g13cce.cs

Example program data: g13cce.d

Example program results: g13cce.r