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

# Syntax

C#
```public static void g13cb(
int nx,
int mtx,
double px,
int mw,
double pw,
int l,
int kc,
int lg,
double[] xg,
out int ng,
double[] stats,
out int ifail
)```
Visual Basic
```Public Shared Sub g13cb ( _
nx As Integer, _
mtx As Integer, _
px As Double, _
mw As Integer, _
pw As Double, _
l As Integer, _
kc As Integer, _
lg As Integer, _
xg As Double(), _
<OutAttribute> ByRef ng As Integer, _
stats As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g13cb(
int nx,
int mtx,
double px,
int mw,
double pw,
int l,
int kc,
int lg,
array<double>^ xg,
[OutAttribute] int% ng,
array<double>^ stats,
[OutAttribute] int% ifail
)```
F#
```static member g13cb :
nx : int *
mtx : int *
px : float *
mw : int *
pw : float *
l : int *
kc : int *
lg : int *
xg : float[] *
ng : int byref *
stats : float[] *
ifail : int byref -> unit
```

#### Parameters

nx
Type: System..::..Int32
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nx}}\ge 1$.
mtx
Type: System..::..Int32
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$.
px
Type: System..::..Double
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$.
mw
Type: System..::..Int32
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}}$.
pw
Type: System..::..Double
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$.
l
Type: System..::..Int32
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.
kc
Type: System..::..Int32
On entry: $K$, the order of the fast Fourier transform (FFT) used to calculate the spectral estimates. kc should be a multiple 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{nx}}$;
• 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.
lg
Type: System..::..Int32
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.
xg
Type: array<System..::..Double>[]()[][]
An array of size [kc]
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[0\right]$ to ${\mathbf{xg}}\left[{\mathbf{ng}}-1\right]$ (logged if ${\mathbf{lg}}\ne 0$). The elements ${\mathbf{xg}}\left[\mathit{i}-1\right]$, for $\mathit{i}={\mathbf{ng}}+1,\dots ,{\mathbf{kc}}$, contain $0.0$.
ng
Type: System..::..Int32%
On exit: the number of spectral estimates, $\left[L/2\right]+1$, in xg.
stats
Type: array<System..::..Double>[]()[][]
An array of size [$4$]
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}}\ne 0$), and the bandwidth in ${\mathbf{stats}}\left[3\right]$.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

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:
 $121-cosπt-12/T,1≤t≤T121-cosπn-t+12/T,n+1-T≤t≤n1,otherwise,$
where $T=\left[\frac{np}{2}\right]$ and $p$ is the tapering proportion.
The unsmoothed sample spectrum
 $f*ω=12π∑t=1nxtexpiωt2$
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,α≤pWα=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 method, 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.

# 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

# Error Indicators and Warnings

Note: g13cb may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nx}}<1$, or ${\mathbf{mtx}}<0$, or ${\mathbf{mtx}}>2$, or ${\mathbf{px}}<0.0$, or ${\mathbf{px}}>1.0$, or ${\mathbf{mw}}<1$, or ${\mathbf{mw}}>{\mathbf{nx}}$, or ${\mathbf{pw}}<0.0$ and ${\mathbf{mw}}\ne {\mathbf{nx}}$, or ${\mathbf{pw}}>1.0$ and ${\mathbf{mw}}\ne {\mathbf{nx}}$, or ${\mathbf{l}}<1$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{kc}}<2×{\mathbf{nx}}$, 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 and method parameter lists in calls to g13cb. Seek expert help.
${\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.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

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

# Parallelism and Performance

None.

g13cb carries out a FFT of length kc to calculate the sample spectrum. The time taken by the method for this is approximately proportional to ${\mathbf{kc}}×\mathrm{log}\left({\mathbf{kc}}\right)$ (but see [Further Comments] in c06pa for further details).

# Example

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

Example program (C#): g13cbe.cs

Example program data: g13cbe.d

Example program results: g13cbe.r