# NAG FL Interfaceg13cbf (uni_​spectrum_​daniell)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g13cbf ( nx, mtx, px, mw, pw, l, kc, lg, xg, ng,
 Integer, Intent (In) :: nx, mtx, mw, l, kc, lg Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ng Real (Kind=nag_wp), Intent (In) :: px, pw Real (Kind=nag_wp), Intent (Inout) :: xg(kc) Real (Kind=nag_wp), Intent (Out) :: stats(4)
#include <nag.h>
 void g13cbf_ (const Integer *nx, const Integer *mtx, const double *px, const Integer *mw, const double *pw, const Integer *l, const Integer *kc, const Integer *lg, double xg[], Integer *ng, double stats[], Integer *ifail)
The routine may be called by the names g13cbf or nagf_tsa_uni_spectrum_daniell.

## 3Description

The supplied time series may be mean or trend corrected (by least squares), and tapered, the tapering factors being those of the split cosine bell:
 $12 (1-cos(π(t-12)/T)) , 1≤t≤T 12 (1-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 unsmoothed sample spectrum
 $f*(ω)=12π |∑t=1nxtexp(iωt)| 2$
is then calculated for frequency values
 $ωk=2 π kK, k= 0,1,…,[K/2],$
where [ ] denotes the integer part.
The smoothed spectrum is returned as a subset of these frequencies for which $k$ is a multiple of a chosen value $r$, i.e.,
 $ωrl=νl=2πlL, l=0,1,…,[L/2],$
where $K=r×L$. You will normally fix $L$ first, then choose $r$ so that $K$ is sufficiently large to provide an adequate representation for the unsmoothed spectrum, i.e., $K\ge 2×n$. It is possible to take $L=K$, i.e., $r=1$.
The smoothing is defined by a trapezium window whose shape is supplied by the function
 $W(α)=1, |α|≤p W(α)=1-|α| 1-p , p<|α|≤1$
the proportion $p$ being supplied by you.
The width of the window is fixed as $2\pi /M$ by you supplying $M$. A set of averaging weights are constructed:
 $Wk=g×W (ωkM π ) , 0≤ωk≤πM,$
where $g$ is a normalizing constant, and the smoothed spectrum obtained is
 $f^(νl)=∑|ωk|< πMWkf*(νl+ωk).$
If no smoothing is required $M$ should be set to $n$, in which case the values returned are $\stackrel{^}{f}\left({\nu }_{l}\right)={f}^{*}\left({\nu }_{l}\right)$. Otherwise, in order that the smoothing approximates well to an integration, it is essential that $K\gg M$, and preferable, but not essential, that $K$ be a multiple of $M$. A choice of $L>M$ would normally be required to supply an adequate description of the smoothed spectrum. Typical choices of $L\simeq n$ and $K\simeq 4n$ should be adequate for usual smoothing situations when $M.
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 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×\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.
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{nx}$Integer Input
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nx}}\ge 1$.
2: $\mathbf{mtx}$Integer Input
On entry: 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: $0\le {\mathbf{mtx}}\le 2$.
3: $\mathbf{px}$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{px}}\le 1.0$.
4: $\mathbf{mw}$Integer Input
On entry: the value of $M$ which determines the frequency width of the smoothing window as $2\pi /M$. A value of $n$ implies no smoothing is to be carried out.
Constraint: $1\le {\mathbf{mw}}\le {\mathbf{nx}}$.
5: $\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{nx}}$ (i.e., no smoothing is carried out), pw is not used.
Constraint: $0.0\le {\mathbf{pw}}\le 1.0$.
6: $\mathbf{l}$Integer Input
On entry: $L$, the frequency division of smoothed spectral estimates as $2\pi /L$.
Constraints:
• ${\mathbf{l}}\ge 1$;
• l must be a factor of kc.
7: $\mathbf{kc}$Integer Input
On entry: $K$, the order of the fast Fourier transform (FFT) used to calculate the spectral estimates.
Constraints:
• ${\mathbf{kc}}\ge 2×{\mathbf{nx}}$;
• kc must be a multiple of l.
8: $\mathbf{lg}$Integer Input
On entry: indicates whether unlogged or logged spectral estimates and confidence limits are required.
${\mathbf{lg}}=0$
For unlogged.
${\mathbf{lg}}\ne 0$
For logged.
9: $\mathbf{xg}\left({\mathbf{kc}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: the $n$ data points.
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(1\right)$ to ${\mathbf{xg}}\left({\mathbf{ng}}\right)$ (logged if ${\mathbf{lg}}\ne 0$). The elements ${\mathbf{xg}}\left(\mathit{i}\right)$, for $\mathit{i}={\mathbf{ng}}+1,\dots ,{\mathbf{kc}}$, contain $0.0$.
10: $\mathbf{ng}$Integer Output
On exit: the number of spectral estimates, $\left[L/2\right]+1$, in xg.
11: $\mathbf{stats}\left(4\right)$Real (Kind=nag_wp) array Output
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}}\ne 0$), and the bandwidth in ${\mathbf{stats}}\left(4\right)$.
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ 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:
Note: in some cases g13cbf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 1$.
On entry, ${\mathbf{mtx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mtx}}\le 2$.
On entry, ${\mathbf{mtx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mtx}}\ge 0$.
On entry, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mw}}\ge 1$.
On entry, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mw}}\le {\mathbf{nx}}$.
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nx}}\ge 1$.
On entry, ${\mathbf{px}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{pw}}<0.0$, ${\mathbf{mw}}={\mathbf{nx}}$.
On entry, ${\mathbf{px}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{mw}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{pw}}>1.0$, ${\mathbf{mw}}={\mathbf{nx}}$.
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$.
${\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{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{kc}}\ge 2×{\mathbf{nx}}$.
${\mathbf{ifail}}=4$
One or more spectral estimates are negative.
Unlogged spectral estimates are returned in xg, and the degrees of freedom, unloged confidence limit factors and bandwidth in stats.
${\mathbf{ifail}}=5$
The calculation of confidence limit factors has failed.
Spectral estimates (logged if requested) are returned in xg, and degrees of freedom and bandwidth in stats.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface 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

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

g13cbf carries out a FFT of length kc to calculate the sample spectrum. The time taken by the routine for this is approximately proportional to ${\mathbf{kc}}×\mathrm{log}\left({\mathbf{kc}}\right)$ (but see Section 9 in c06paf for further details).

## 10Example

This example reads a time series of length $131$. It then calls g13cbf to calculate the univariate spectrum and prints the logged spectrum together with $95%$ confidence limits.

### 10.1Program Text

Program Text (g13cbfe.f90)

### 10.2Program Data

Program Data (g13cbfe.d)

### 10.3Program Results

Program Results (g13cbfe.r)