G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13CCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G13CCF ( NXY, MTXY, PXY, IW, MW, ISH, IC, NC, CXY, CYX, KC, L, NXYG, XG, YG, NG, IFAIL)
 INTEGER NXY, MTXY, IW, MW, ISH, IC, NC, KC, L, NXYG, NG, IFAIL REAL (KIND=nag_wp) PXY, CXY(NC), CYX(NC), XG(NXYG), YG(NXYG)

## 3  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+1 M-1wkCxyk+Scosωk$
and imaginary part or quadrature spectrum
 $qfω=12π ∑k=-M+ 1 M- 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 G13CAF. 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=1 n-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 G13CAF 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 G13CAF for estimating the univariate spectra of ${y}_{t}$ and ${x}_{t}$.
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

## 5  Parameters

1:     NXY – INTEGERInput
On entry: $n$, the length of the time series $x$ and $y$.
Constraint: ${\mathbf{NXY}}\ge 1$.
2:     MTXY – INTEGERInput
On entry: if cross-covariances are to be calculated by the routine (${\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$.
3:     PXY – REAL (KIND=nag_wp)Input
On entry: if cross-covariances are to be calculated by the routine (${\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$.
4:     IW – INTEGERInput
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$.
5:     MW – INTEGERInput
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}}$.
6:     ISH – INTEGERInput
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}}$.
7:     IC – INTEGERInput
On entry: indicates whether cross-covariances are to be calculated in the routine or supplied in the call to the routine.
${\mathbf{IC}}=0$
Cross-covariances are to be calculated.
${\mathbf{IC}}\ne 0$
Cross-covariances are to be supplied.
8:     NC – INTEGERInput
On entry: the number of cross-covariances to be calculated in the routine or supplied in the call to the routine.
Constraint: ${\mathbf{MW}}+\left|{\mathbf{ISH}}\right|\le {\mathbf{NC}}\le {\mathbf{NXY}}$.
9:     CXY(NC) – REAL (KIND=nag_wp) arrayInput/Output
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.
10:   CYX(NC) – REAL (KIND=nag_wp) arrayInput/Output
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.
11:   KC – INTEGERInput
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.
12:   L – INTEGERInput
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.
13:   NXYG – INTEGERInput
On entry: the dimension of the arrays XG and YG as declared in the (sub)program from which G13CCF is called.
Constraints:
• if ${\mathbf{IC}}=0$, ${\mathbf{NXYG}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{KC}},{\mathbf{L}}\right)$;
• if ${\mathbf{IC}}\ne 0$, ${\mathbf{NXYG}}\ge {\mathbf{L}}$.
14:   XG(NXYG) – REAL (KIND=nag_wp) arrayInput/Output
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(1\right)$ to ${\mathbf{XG}}\left({\mathbf{NG}}\right)$, and ${\mathbf{XG}}\left({\mathbf{NG}}+1\right)$ to ${\mathbf{XG}}\left({\mathbf{NXYG}}\right)$ contain $0.0$. The $y$ series leads the $x$ series.
15:   YG(NXYG) – REAL (KIND=nag_wp) arrayInput/Output
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(1\right)$ to ${\mathbf{YG}}\left({\mathbf{NG}}\right)$, and ${\mathbf{YG}}\left({\mathbf{NG}}+1\right)$ to ${\mathbf{YG}}\left({\mathbf{NXYG}}\right)$ contain $0.0$. The $y$ series leads the $x$ series.
16:   NG – INTEGEROutput
On exit: the number, $\left[{\mathbf{L}}/2\right]+1$, of complex spectral estimates, whose separate parts are held in XG and YG.
17:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\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}}$, or ${\mathbf{NXYG}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{KC}},{\mathbf{L}}\right)$ and ${\mathbf{IC}}=0$, or ${\mathbf{NXYG}}<{\mathbf{L}}$ and ${\mathbf{IC}}\ne 0$.
${\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.

## 7  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.

G13CCF 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 G13CCF 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}n$ (but see Section 8 in C06PAF for further details).

## 9  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 G13CCF to calculate the cross spectrum and then prints the cross-covariances and cross spectrum.

### 9.1  Program Text

Program Text (g13ccfe.f90)

### 9.2  Program Data

Program Data (g13ccfe.d)

### 9.3  Program Results

Program Results (g13ccfe.r)