# NAG Library Routine Document

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g13cdf ( nxy, mtxy, pxy, mw, ish, pw, l, kc, xg, yg, ng,
 Integer, Intent (In) :: nxy, mtxy, mw, ish, l, kc Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ng Real (Kind=nag_wp), Intent (In) :: pxy, pw Real (Kind=nag_wp), Intent (Inout) :: xg(kc), yg(kc)
#include nagmk26.h
 void g13cdf_ (const Integer *nxy, const Integer *mtxy, const double *pxy, const Integer *mw, const Integer *ish, const double *pw, const Integer *l, const Integer *kc, double xg[], double yg[], Integer *ng, Integer *ifail)

## 3Description

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}$.
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: 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:     $\mathbf{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:     $\mathbf{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:     $\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{l}}<{\mathbf{ish}}<{\mathbf{l}}$.
6:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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.
Constraints:
• ${\mathbf{kc}}\ge 2×{\mathbf{nxy}}$;
• kc must be a multiple of l.
9:     $\mathbf{xg}\left({\mathbf{kc}}\right)$ – 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:   $\mathbf{yg}\left({\mathbf{kc}}\right)$ – 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:   $\mathbf{ng}$ – IntegerOutput
On exit: the number of spectral estimates, $\left[L/2\right]+1$, whose separate parts are held in xg and yg.
12:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. 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 $-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 argument, 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).

## 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{ish}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: $\left|{\mathbf{ish}}\right|<{\mathbf{l}}$.
On entry, ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l}}\ge 1$.
On entry, ${\mathbf{mtxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mtxy}}\le 2$.
On entry, ${\mathbf{mtxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mtxy}}\ge 0$.
On entry, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mw}}\ge 1$.
On entry, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mw}}\le {\mathbf{nxy}}$.
On entry, ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nxy}}\ge 1$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{pw}}<0.0$, ${\mathbf{mw}}={\mathbf{nxy}}$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$, ${\mathbf{mw}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{pw}}>1.0$, ${\mathbf{mw}}={\mathbf{nxy}}$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pxy}}\ge 0.0$.
On entry, ${\mathbf{pxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pxy}}\le 1.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{kc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: kc must be a multiple of l.
On entry, ${\mathbf{kc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{kc}}\ge 2×{\mathbf{nxy}}$.
${\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

g13cdf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13cdf 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.

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

## 10Example

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.

### 10.1Program Text

Program Text (g13cdfe.f90)

### 10.2Program Data

Program Data (g13cdfe.d)

### 10.3Program Results

Program Results (g13cdfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017