NAG Library Routine Document
G13CAF
1 Purpose
G13CAF calculates the smoothed sample spectrum of a univariate time series using one of four lag windows – rectangular, Bartlett, Tukey or Parzen window.
2 Specification
SUBROUTINE G13CAF ( 
NX, MTX, PX, IW, MW, IC, NC, C, KC, L, LG, NXG, XG, NG, STATS, IFAIL) 
INTEGER 
NX, MTX, IW, MW, IC, NC, KC, L, LG, NXG, NG, IFAIL 
REAL (KIND=nag_wp) 
PX, C(NC), XG(NXG), STATS(4) 

3 Description
The smoothed sample spectrum is defined as
where
$M$ is the window width, and is calculated for frequency values
where
$\left[\right]$ denotes the integer part.
The autocovariances
${C}_{k}$ may be supplied by you, or constructed from a time series
${x}_{1},{x}_{2},\dots ,{x}_{n}$, as
the fast Fourier transform (FFT) being used to carry out the convolution in this formula.
The time series may be mean or trend corrected (by classical least squares), and tapered before calculation of the covariances, the tapering factors being those of the split cosine bell:
where
$T=\left[\frac{np}{2}\right]$ and
$p$ is the tapering proportion.
The smoothing window is defined by
which for the various windows is defined over
$0\le \alpha <1$ by
rectangular:
Bartlett:
Tukey:
Parzen:
The sampling distribution of
$\hat{f}\left(\omega \right)$ is approximately that of a scaled
${\chi}_{d}^{2}$ variate, whose degrees of freedom
$d$ is provided by the routine, together with multiplying limits
$mu$,
$ml$ from which approximate
$95\%$ confidence intervals for the true spectrum
$f\left(\omega \right)$ may be constructed as
$\left[ml\times \hat{f}\left(\omega \right),mu\times \hat{f}\left(\omega \right)\right]$. Alternatively, log
$\hat{f}\left(\omega \right)$ may be returned, with additive limits.
The bandwidth $b$ of the corresponding smoothing window in the frequency domain is also provided. Spectrum estimates separated by (angular) frequencies much greater than $b$ may be assumed to be independent.
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: NX – INTEGERInput
On entry: $n$, the length of the time series.
Constraint:
${\mathbf{NX}}\ge 1$.
 2: MTX – INTEGERInput
On entry: if covariances are to be calculated by the routine (
${\mathbf{IC}}=0$),
MTX must specify whether the data are to be initially mean or trend corrected.
 ${\mathbf{MTX}}=0$
 For no correction.
 ${\mathbf{MTX}}=1$
 For mean correction.
 ${\mathbf{MTX}}=2$
 For trend correction.
Constraint:
if
${\mathbf{IC}}=0$,
$0\le {\mathbf{MTX}}\le 2$If covariances are supplied (
${\mathbf{IC}}\ne 0$),
MTX is not used.
 3: PX – REAL (KIND=nag_wp)Input
On entry: if covariances are to be calculated by the routine (
${\mathbf{IC}}=0$),
PX must specify the proportion of the data (totalled over both ends) to be initially tapered by the split cosine bell taper.
If covariances are supplied
$\left({\mathbf{IC}}\ne 0\right)$,
PX must specify the proportion of data tapered before the supplied covariances were calculated and after any mean or trend correction.
PX is required for the calculation of output statistics. A value of
$0.0$ implies no tapering.
Constraint:
$0.0\le {\mathbf{PX}}\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 ‘cutoff’ point of the lag window. Windowed covariances at lag $M$ or greater are zero.
Constraint:
$1\le {\mathbf{MW}}\le {\mathbf{NX}}$.
 6: IC – INTEGERInput
On entry: indicates whether covariances are to be calculated in the routine or supplied in the call to the routine.
 ${\mathbf{IC}}=0$
 Covariances are to be calculated.
 ${\mathbf{IC}}\ne 0$
 Covariances are to be supplied.
 7: NC – INTEGERInput
On entry: the number of covariances to be calculated in the routine or supplied in the call to the routine.
Constraint:
${\mathbf{MW}}\le {\mathbf{NC}}\le {\mathbf{NX}}$.
 8: C(NC) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if
${\mathbf{IC}}\ne 0$,
C must contain the
NC covariances for lags from
$0$ to
$\left({\mathbf{NC}}1\right)$, otherwise
C need not be set.
On exit: if
${\mathbf{IC}}=0$,
C will contain the
NC calculated covariances.
If
${\mathbf{IC}}\ne 0$, the contents of
C will be unchanged.
 9: KC – INTEGERInput
On entry: if
${\mathbf{IC}}=0$,
KC must specify the order of the fast Fourier transform (FFT) used to calculate the covariances.
KC should be a product of small primes such as
${2}^{m}$ where
$m$ is the smallest integer such that
${2}^{m}\ge {\mathbf{NX}}+{\mathbf{NC}}$, provided
$m\le 20$.
If
${\mathbf{IC}}\ne 0$, that is covariances are supplied,
KC is not used.
Constraint:
${\mathbf{KC}}\ge {\mathbf{NX}}+{\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.
 10: 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 covariances.
L should be a product of small primes such as
${2}^{m}$ where
$m$ is the smallest integer such that
${2}^{m}\ge 2M1$, provided
$m\le 20$.
Constraint:
${\mathbf{L}}\ge 2\times {\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.
 11: LG – INTEGERInput
On entry: indicates whether unlogged or logged spectral estimates and confidence limits are required.
 ${\mathbf{LG}}=0$
 Unlogged.
 ${\mathbf{LG}}\ne 0$
 Logged.
 12: NXG – INTEGERInput
On entry: the dimension of the array
XG as declared in the (sub)program from which G13CAF is called.
Constraints:
 if ${\mathbf{IC}}=0$, ${\mathbf{NXG}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{KC}},{\mathbf{L}}\right)$;
 if ${\mathbf{IC}}\ne 0$, ${\mathbf{NXG}}\ge {\mathbf{L}}$.
 13: XG(NXG) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if the covariances are to be calculated, then
XG must contain the
NX data points. If covariances are supplied,
XG may contain any values.
On exit: contains the
NG spectral estimates,
$\hat{f}\left({\omega}_{\mathit{i}}\right)$, for
$\mathit{i}=0,1,\dots ,\left[L/2\right]$ in
${\mathbf{XG}}\left(1\right)$ to
${\mathbf{XG}}\left({\mathbf{NG}}\right)$ respectively (logged if
${\mathbf{LG}}=1$). The elements
${\mathbf{XG}}\left(\mathit{i}\right)$, for
$\mathit{i}={\mathbf{NG}}+1,\dots ,{\mathbf{NXG}}$ contain
$0.0$.
 14: NG – INTEGEROutput
On exit: the number of spectral estimates,
$\left[L/2\right]+1$, in
XG.
 15: STATS($4$) – REAL (KIND=nag_wp) arrayOutput
On exit: four associated statistics. These are the degrees of freedom in ${\mathbf{STATS}}\left(1\right)$, the lower and upper $95\%$ confidence limit factors in ${\mathbf{STATS}}\left(2\right)$ and ${\mathbf{STATS}}\left(3\right)$ respectively (logged if ${\mathbf{LG}}=1$), and the bandwidth in ${\mathbf{STATS}}\left(4\right)$.
 16: 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{NX}}<1$, 
or  ${\mathbf{MTX}}<0$ and ${\mathbf{IC}}=0$, 
or  ${\mathbf{MTX}}>2$ and ${\mathbf{IC}}=0$, 
or  ${\mathbf{PX}}<0.0$, 
or  ${\mathbf{PX}}>1.0$, 
or  ${\mathbf{IW}}<1$, 
or  ${\mathbf{IW}}>4$, 
or  ${\mathbf{MW}}<1$, 
or  ${\mathbf{MW}}>{\mathbf{NX}}$, 
or  ${\mathbf{NC}}<{\mathbf{MW}}$, 
or  ${\mathbf{NC}}>{\mathbf{NX}}$, 
or  ${\mathbf{NXG}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{KC}},{\mathbf{L}}\right)$ and ${\mathbf{IC}}=0$, 
or  ${\mathbf{NXG}}<{\mathbf{L}}$ and ${\mathbf{IC}}\ne 0$. 
 ${\mathbf{IFAIL}}=2$
On entry,  ${\mathbf{KC}}<{\mathbf{NX}}+{\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\times {\mathbf{MW}}1$, 
or  L has a prime factor exceeding $19$, 
or  L has more than $20$ prime factors, counting repetitions. 
 ${\mathbf{IFAIL}}=4$

One or more spectral estimates are negative. Unlogged spectral estimates are returned in
XG, and the degrees of freedom, unlogged confidence limit factors and bandwidth in
STATS.
 ${\mathbf{IFAIL}}=5$

The calculation of confidence limit factors has failed. This error will not normally occur. Spectral estimates (logged if requested) are returned in
XG, and degrees of freedom and bandwidth in
STATS.
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.
G13CAF carries out two FFTs of length
KC to calculate the covariances and one FFT of length
L to calculate the sample spectrum. The time taken by the routine for an FFT of length
$n$ is approximately proportional to
$n\mathrm{log}\left(n\right)$ (but see
Section 8 in C06PAF for further details).
9 Example
This example reads a time series of length $256$. It selects the mean correction option, a tapering proportion of $0.1$, the Parzen smoothing window and a cutoff point for the window at lag $100$. It chooses to have $100$ autocovariances calculated and unlogged spectral estimates at a frequency division of $2\pi /200$. It then calls G13CAF to calculate the univariate spectrum and statistics and prints the autocovariances and the spectrum together with its $95\%$ confidence multiplying limits.
9.1 Program Text
Program Text (g13cafe.f90)
9.2 Program Data
Program Data (g13cafe.d)
9.3 Program Results
Program Results (g13cafe.r)