# NAG Library Routine Document

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g13ccf ( nxy, mtxy, pxy, iw, mw, ish, ic, nc, cxy, cyx, kc, l, nxyg, xg, yg, ng,
 Integer, Intent (In) :: nxy, mtxy, iw, mw, ish, ic, nc, kc, l, nxyg Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ng Real (Kind=nag_wp), Intent (In) :: pxy Real (Kind=nag_wp), Intent (Inout) :: cxy(nc), cyx(nc), xg(nxyg), yg(nxyg)
#include <nagmk26.h>
 void g13ccf_ (const Integer *nxy, const Integer *mtxy, const double *pxy, const Integer *iw, const Integer *mw, const Integer *ish, const Integer *ic, const Integer *nc, double cxy[], double cyx[], const Integer *kc, const Integer *l, const Integer *nxyg, double xg[], double yg[], Integer *ng, Integer *ifail)

## 3Description

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

## 5Arguments

1:     $\mathbf{nxy}$ – IntegerInput
On entry: $n$, the length of the time series $x$ and $y$.
Constraint: ${\mathbf{nxy}}\ge 1$.
2:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{cxy}\left({\mathbf{nc}}\right)$ – 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:   $\mathbf{cyx}\left({\mathbf{nc}}\right)$ – 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:   $\mathbf{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.
If ${\mathbf{ic}}\ne 0$, that is if covariances are supplied, kc is not used.
Constraint: ${\mathbf{kc}}\ge {\mathbf{nxy}}+{\mathbf{nc}}$.
12:   $\mathbf{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.
Constraint: ${\mathbf{l}}\ge 2×{\mathbf{mw}}-1$.
13:   $\mathbf{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)$;
• otherwise ${\mathbf{nxyg}}\ge {\mathbf{l}}$.
14:   $\mathbf{xg}\left({\mathbf{nxyg}}\right)$ – 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:   $\mathbf{yg}\left({\mathbf{nxyg}}\right)$ – 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:   $\mathbf{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:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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{ic}}=〈\mathit{\text{value}}〉$, ${\mathbf{nxyg}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}\ne 0$, ${\mathbf{nxyg}}\ge {\mathbf{l}}$.
On entry, ${\mathbf{ish}}=〈\mathit{\text{value}}〉$ and ${\mathbf{mw}}=〈\mathit{\text{value}}〉$.
Constraint: $\left|{\mathbf{ish}}\right|\le {\mathbf{mw}}$.
On entry, ${\mathbf{iw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iw}}=1$, $2$, $3$ or $4$.
On entry, ${\mathbf{mtxy}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}=0$ then ${\mathbf{mtxy}}\ne 0$, $1$ or $2$.
On entry, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$, ${\mathbf{ish}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mw}}+\left|{\mathbf{ish}}\right|\le {\mathbf{nxy}}$.
On entry, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mw}}\ge 1$.
On entry, ${\mathbf{nc}}=〈\mathit{\text{value}}〉$, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ish}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nc}}\ge {\mathbf{mw}}+\left|{\mathbf{ish}}\right|$.
On entry, ${\mathbf{nc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nc}}\le {\mathbf{nxy}}$.
On entry, ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nxy}}\ge 1$.
On entry, ${\mathbf{nxyg}}=〈\mathit{\text{value}}〉$, ${\mathbf{kc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{nxyg}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{kc}},{\mathbf{l}}\right)$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{pxy}}\le 1.0$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{pxy}}\ge 0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{kc}}=〈\mathit{\text{value}}〉$, ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nc}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{kc}}\ge {\mathbf{nxy}}+{\mathbf{nc}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{l}}=〈\mathit{\text{value}}〉$ and ${\mathbf{mw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l}}\ge 2×{\mathbf{mw}}-1$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

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

## 8Parallelism and Performance

g13ccf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13ccf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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}\left(n\right)$ (but see Section 9 in c06paf for further details).

## 10Example

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.

### 10.1Program Text

Program Text (g13ccfe.f90)

### 10.2Program Data

Program Data (g13ccfe.d)

### 10.3Program Results

Program Results (g13ccfe.r)