G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13CDF

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

G13CDF calculates the smoothed sample cross spectrum of a bivariate time series using spectral smoothing by the trapezium frequency (Daniell) window.

## 2  Specification

 SUBROUTINE G13CDF ( NXY, MTXY, PXY, MW, ISH, PW, L, KC, XG, YG, NG, IFAIL)
 INTEGER NXY, MTXY, MW, ISH, L, KC, NG, IFAIL REAL (KIND=nag_wp) PXY, PW, XG(KC), YG(KC)

## 3  Description

The supplied time series may be mean and trend corrected and tapered as in the description of G13CBF before calculation of the unsmoothed sample cross-spectrum
 $fxy* ω = 12πn ∑ t=1 n yt expiωt × ∑ t=1 n xt exp-iωt$
for frequency values ${\omega }_{j}=\frac{2\pi j}{K}$, $0\le {\omega }_{j}\le \pi$.
A correction is made for bias due to any tapering.
As in the description of G13CBF for univariate frequency window smoothing, the smoothed spectrum is returned as a subset of these frequencies,
 $νl=2π lL, l=0,1,…,L/2$
where [ ] denotes the integer part.
Its real part or co-spectrum $cf\left({\nu }_{l}\right)$, and imaginary part or quadrature spectrum $qf\left({\nu }_{l}\right)$ are defined by
 $fxy νl = cf νl + iqf νl = ∑ ωk < πM w~k fxy* νl+ωk$
where the weights ${\stackrel{~}{w}}_{k}$ are similar to the weights ${w}_{k}$ defined for G13CBF, but allow for an implicit alignment shift $S$ between the series:
 $w~k=wkexp-2π iSk/L.$
It is recommended that $S$ is chosen as the lag $k$ at which the cross-covariances ${c}_{xy}\left(k\right)$ peak, so as to minimize bias.
If no smoothing is required, the integer $M$, which determines the frequency window width $\frac{2\pi }{M}$, should be set to $n$.
The bandwidth of the estimates will normally have been calculated in a previous call of G13CBF for estimating the univariate spectra of ${y}_{t}$ and ${x}_{t}$.

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

## 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: 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.
Constraint: $0\le {\mathbf{MTXY}}\le 2$.
3:     PXY – REAL (KIND=nag_wp)Input
On entry: 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.
Constraint: $0.0\le {\mathbf{PXY}}\le 1.0$.
4:     MW – INTEGERInput
On entry: $M$, the frequency width of the smoothing window as $\frac{2\pi }{M}$.
A value of $n$ implies that no smoothing is to be carried out.
Constraint: $1\le {\mathbf{MW}}\le {\mathbf{NXY}}$.
5:     ISH – INTEGERInput
On entry: $S$, the alignment shift between the $x$ and $y$ series. If $x$ leads $y$, the shift is positive.
Constraint: $-{\mathbf{L}}<{\mathbf{ISH}}<{\mathbf{L}}$.
6:     PW – REAL (KIND=nag_wp)Input
On entry: $p$, the shape parameter of the trapezium frequency window.
A value of $0.0$ gives a triangular window, and a value of $1.0$ a rectangular window.
If ${\mathbf{MW}}={\mathbf{NXY}}$ (i.e., no smoothing is carried out) then PW is not used.
Constraint: if ${\mathbf{MW}}\ne {\mathbf{NXY}}$, $0.0\le {\mathbf{PW}}\le 1.0$.
7:     L – INTEGERInput
On entry: $L$, the frequency division of smoothed cross spectral estimates as $\frac{2\pi }{L}$.
Constraints:
• ${\mathbf{L}}\ge 1$;
• L must be a factor of KC.
8:     KC – INTEGERInput
On entry: the dimension of the arrays XG and YG as declared in the (sub)program from which G13CDF is called. The order of the fast Fourier transform ( FFT) used to calculate the spectral estimates. KC should be a product of small primes such as ${2}^{m}$ where $m$ is the smallest integer such that ${2}^{m}\ge 2n$, provided $m\le 20$.
Constraints:
• ${\mathbf{KC}}\ge 2×{\mathbf{NXY}}$;
• KC must be a multiple of L. 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.
9:     XG(KC) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the NXY data points of the $x$ series.
On exit: the real parts of the NG cross 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{KC}}\right)$ contain $0.0$. The $y$ series leads the $x$ series.
10:   YG(KC) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the NXY data points of the $y$ series.
On exit: the imaginary parts of the NG cross 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{KC}}\right)$ contain $0.0$. The $y$ series leads the $x$ series.
11:   NG – INTEGEROutput
On exit: the number of spectral estimates, $\left[L/2\right]+1$, whose separate parts are held in XG and YG.
12:   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$, or ${\mathbf{MTXY}}>2$, or ${\mathbf{PXY}}<0.0$, or ${\mathbf{PXY}}>1.0$, or ${\mathbf{MW}}<1$, or ${\mathbf{MW}}>{\mathbf{NXY}}$, or ${\mathbf{PW}}<0.0$ and ${\mathbf{MW}}\ne {\mathbf{NXY}}$, or ${\mathbf{PW}}>1.0$ and ${\mathbf{MW}}\ne {\mathbf{NXY}}$, or ${\mathbf{L}}<1$, or $\left|{\mathbf{ISH}}\right|\ge {\mathbf{L}}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{KC}}<2×{\mathbf{NXY}}$, or KC is not a multiple of L, or KC has a prime factor exceeding $19$, or KC has more than $20$ prime factors, counting repetitions.
${\mathbf{IFAIL}}=3$
This indicates that a serious error has occurred. Check all array subscripts in calls to G13CDF. Seek expert help.

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

G13CDF carries out an FFT of length KC to calculate the sample cross spectrum. The time taken by the routine for this is approximately proportional to ${\mathbf{KC}}×\mathrm{log}\left({\mathbf{KC}}\right)$ (but see routine document C06PAF for further details).

## 9  Example

This example reads two time series of length $296$. It selects mean correction and a 10% tapering proportion. It selects a $2\pi /16$ frequency width of smoothing window, a window shape parameter of $0.5$ and an alignment shift of $3$. It then calls G13CDF to calculate the smoothed sample cross spectrum and prints the results.

### 9.1  Program Text

Program Text (g13cdfe.f90)

### 9.2  Program Data

Program Data (g13cdfe.d)

### 9.3  Program Results

Program Results (g13cdfe.r)