g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_tsa_spectrum_univar_cov (g13cac)

## 1  Purpose

nag_tsa_spectrum_univar_cov (g13cac) calculates the smoothed sample spectrum of a univariate time series using one of four lag windows – rectangular, Bartlett, Tukey or Parzen window.

## 2  Specification

 #include #include
 void nag_tsa_spectrum_univar_cov (Integer nx, Integer mtx, double px, Integer iw, Integer mw, Integer ic, Integer nc, double c[], Integer kc, Integer l, Nag_LoggedSpectra lg_spect, Integer nxg, double xg[], Integer *ng, double stats[], NagError *fail)

## 3  Description

The smoothed sample spectrum is defined as
 $f^ω=12π C0+2∑k=1 M-1wkCkcosωk ,$
where $M$ is the window width, and is calculated for frequency values
 $ωi=2πiL, i=0,1,…,L/2,$
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
 $Ck=1n∑t=1 n-kxtxt+k,$
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:
 $121-cosπ t-12/T, 1≤t≤T 121-cosπ n-t+12/T, n+ 1-T≤t≤n 1, otherwise,$
where $T=\left[\frac{np}{2}\right]$ and $p$ is the tapering proportion.
The smoothing window is defined by
 $wk=W kM , k≤M-1,$
which for the various windows is defined over $0\le \alpha <1$ by
rectangular:
 $Wα=1$
Bartlett:
 $Wα = 1-α$
Tukey:
 $Wα=121+cosπα$
Parzen:
 $Wα= 1- 6α2+ 6α3, 0≤α≤12 Wα= 2 1-α 3, 12<α< 1.$
The sampling distribution of $\stackrel{^}{f}\left(\omega \right)$ is approximately that of a scaled ${\chi }_{d}^{2}$ variate, whose degrees of freedom $d$ is provided by the function, 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×\stackrel{^}{f}\left(\omega \right),mu×\stackrel{^}{f}\left(\omega \right)\right]$. Alternatively, log $\stackrel{^}{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  Arguments

1:     nxIntegerInput
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nx}}\ge 1$.
2:     mtxIntegerInput
On entry: if covariances are to be calculated by the function (${\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:     pxdoubleInput
On entry: if covariances are to be calculated by the function (${\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:     iwIntegerInput
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:     mwIntegerInput
On entry: $M$, the ‘cut-off’ point of the lag window. Windowed covariances at lag $M$ or greater are zero.
Constraint: $1\le {\mathbf{mw}}\le {\mathbf{nx}}$.
6:     icIntegerInput
On entry: indicates whether covariances are to be calculated in the function or supplied in the call to the function.
${\mathbf{ic}}=0$
Covariances are to be calculated.
${\mathbf{ic}}\ne 0$
Covariances are to be supplied.
7:     ncIntegerInput
On entry: the number of covariances to be calculated in the function or supplied in the call to the function.
Constraint: ${\mathbf{mw}}\le {\mathbf{nc}}\le {\mathbf{nx}}$.
8:     c[nc]doubleInput/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:     kcIntegerInput
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:   lIntegerInput
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 2M-1$, provided $m\le 20$.
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.
11:   lg_spectNag_LoggedSpectraInput
On entry: indicates whether unlogged or logged spectral estimates and confidence limits are required.
${\mathbf{lg_spect}}=\mathrm{Nag_Unlogged}$
Unlogged.
${\mathbf{lg_spect}}=\mathrm{Nag_Logged}$
Logged.
Constraint: ${\mathbf{lg_spect}}=\mathrm{Nag_Unlogged}$ or $\mathrm{Nag_Logged}$.
12:   nxgIntegerInput
On entry: the dimension of the array xg.
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]doubleInput/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, $\stackrel{^}{f}\left({\omega }_{\mathit{i}}\right)$, for $\mathit{i}=0,1,\dots ,\left[L/2\right]$ in ${\mathbf{xg}}\left[0\right]$ to ${\mathbf{xg}}\left[{\mathbf{ng}}-1\right]$ respectively (logged if ${\mathbf{lg_spect}}=\mathrm{Nag_Logged}$). The elements ${\mathbf{xg}}\left[\mathit{i}-1\right]$, for $\mathit{i}={\mathbf{ng}}+1,\dots ,{\mathbf{nxg}}$ contain $0.0$.
14:   ngInteger *Output
On exit: the number of spectral estimates, $\left[L/2\right]+1$, in xg.
15:   stats[$4$]doubleOutput
On exit: four associated statistics. These are the degrees of freedom in ${\mathbf{stats}}\left[0\right]$, the lower and upper $95%$ confidence limit factors in ${\mathbf{stats}}\left[1\right]$ and ${\mathbf{stats}}\left[2\right]$ respectively (logged if ${\mathbf{lg_spect}}=\mathrm{Nag_Logged}$), and the bandwidth in ${\mathbf{stats}}\left[3\right]$.
16:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_CONFID_LIMITS
The calculation of confidence limit factors has failed.
NE_INT
On entry, ${\mathbf{ic}}=0$ and ${\mathbf{mtx}}<0$: ${\mathbf{mtx}}=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{ic}}=0$ and ${\mathbf{mtx}}>2$: ${\mathbf{mtx}}=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{iw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{iw}}=1$, $2$, $3$ or $4$.
On entry, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mw}}\ge 1$.
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nx}}\ge 1$.
NE_INT_2
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 2×{\mathbf{mw}}-1$.
On entry, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mw}}\le {\mathbf{nx}}$.
On entry, ${\mathbf{nc}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nc}}\ge {\mathbf{mw}}$.
On entry, ${\mathbf{nc}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nc}}\le {\mathbf{nx}}$.
On entry, ${\mathbf{nxg}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ic}}\ne 0$, ${\mathbf{nxg}}\ge {\mathbf{l}}$.
NE_INT_3
On entry, ${\mathbf{kc}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nc}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{kc}}\ge \left({\mathbf{nx}}+{\mathbf{nc}}\right)$.
On entry, ${\mathbf{nxg}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{kc}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ic}}=0$, ${\mathbf{nxg}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{kc}},{\mathbf{l}}\right)$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_PRIME_FACTOR
kc has a prime factor exceeding $19$, or more than 20 prime factors (counting repetitions): ${\mathbf{kc}}=⟨\mathit{\text{value}}⟩$.
l has a prime factor exceeding $19$, or more than 20 prime factors (counting repetitions): ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
NE_REAL
On entry, ${\mathbf{px}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{px}}\le 1.0$.
On entry, ${\mathbf{px}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{px}}\ge 0.0$.
NE_SPECTRAL_ESTIMATES
One or more spectral estimates are zero. Consult the values in xg and 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.

## 8  Parallelism and Performance

Not applicable.

nag_tsa_spectrum_univar_cov (g13cac) 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 function for an FFT of length $n$ is approximately proportional to $n\mathrm{log}\left(n\right)$ (but see Section 9 in nag_sum_fft_realherm_1d (c06pac) for further details).

## 10  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 cut-off point for the window at lag $100$. It chooses to have $100$ auto-covariances calculated and unlogged spectral estimates at a frequency division of $2\pi /200$. It then calls nag_tsa_spectrum_univar_cov (g13cac) to calculate the univariate spectrum and statistics and prints the autocovariances and the spectrum together with its $95%$ confidence multiplying limits.

### 10.1  Program Text

Program Text (g13cace.c)

### 10.2  Program Data

Program Data (g13cace.d)

### 10.3  Program Results

Program Results (g13cace.r)