c09 Chapter Contents
c09 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_mlmodwt (c09dcc)

## 1  Purpose

nag_mlmodwt (c09dcc) computes the one-dimensional multi-level maximal overlap discrete wavelet transform (MODWT). The initialization function nag_wfilt (c09aac) must be called first to set up the MODWT options.

## 2  Specification

 #include #include
 void nag_mlmodwt (Integer n, const double x[], Nag_WaveletCoefficients keepa, Integer lenc, double c[], Integer nwl, Integer *na, Integer icomm[], NagError *fail)

## 3  Description

nag_mlmodwt (c09dcc) computes the multi-level MODWT for a data set, ${\mathit{x}}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$, in one dimension. For a chosen number of levels, ${n}_{l}$, with ${n}_{l}\le {l}_{\mathrm{max}}$, where ${l}_{\mathrm{max}}$ is returned by the initialization function nag_wfilt (c09aac) in nwlmax, the transform is returned as a set of coefficients for the different levels stored in a single array. Periodic reflection is currently the only available end extension method to reduce the edge effects caused by finite data sets.
The argument keepa can be set to retain both approximation and detail coefficients at each level resulting in ${n}_{l}×\left({n}_{a}+{n}_{d}\right)$ coefficients being returned in the output array, c, where ${n}_{a}$ is the number of approximation coefficients and ${n}_{d}$ is the number of detail coefficients. Otherwise, only the detail coefficients are stored for each level along with the approximation coefficients for the final level, in which case the length of the output array, c, is ${n}_{a}+{n}_{l}×{n}_{d}$. In the present implementation, for simplicity, ${n}_{a}$ and ${n}_{d}$ are chosen to be equal by adding zero padding to the wavelet filters where necessary.

## 4  References

Percival D B and Walden A T (2000) Wavelet Methods for Time Series Analysis Cambridge University Press

## 5  Arguments

1:     nIntegerInput
On entry: the number of elements, $n$, in the data array $x$.
Constraint: this must be the same as the value n passed to the initialization function nag_wfilt (c09aac).
2:     x[n]const doubleInput
On entry: x contains the input dataset ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     keepaNag_WaveletCoefficientsInput
On entry: determines whether the approximation coefficients are stored in array c for every level of the computed transform or else only for the final level. In both cases, the detail coefficients are stored in c for every level computed.
${\mathbf{keepa}}=\mathrm{Nag_StoreAll}$
Retain approximation coefficients for all levels computed.
${\mathbf{keepa}}=\mathrm{Nag_StoreFinal}$
Retain approximation coefficients for only the final level computed.
Constraint: ${\mathbf{keepa}}=\mathrm{Nag_StoreAll}$ or $\mathrm{Nag_StoreFinal}$.
4:     lencIntegerInput
On entry: the dimension of the array c. c must be large enough to contain the number of wavelet coefficients.
If ${\mathbf{keepa}}=\mathrm{Nag_StoreFinal}$, the total number of coefficients, ${n}_{c}$, is returned in nwc by the call to the initialization function nag_wfilt (c09aac) and corresponds to the MODWT being continued for the maximum number of levels possible for the given data set. When the number of levels, ${n}_{l}$, is chosen to be less than the maximum, then the number of stored coefficients is correspondingly smaller and lenc can be reduced by noting that ${n}_{d}$ detail coefficients are stored at each level, with the storage increased at the final level to contain the ${n}_{a}$ approximation coefficients.
If ${\mathbf{keepa}}=\mathrm{Nag_StoreAll}$, ${n}_{d}$ detail coefficients and ${n}_{a}$ approximation coefficients are stored for each level computed, requiring ${\mathbf{lenc}}\ge {n}_{l}×\left({n}_{a}+{n}_{d}\right)=2×{n}_{l}×{n}_{a}$, since the numbers of stored approximation and detail coefficients are equal. The number of approximation (or detail) coefficients at each level, ${n}_{a}$, is returned in na.
Constraints:
• if ${\mathbf{keepa}}=\mathrm{Nag_StoreFinal}$, ${\mathbf{lenc}}\ge \left({n}_{l}+1\right)×{n}_{a}$;
• if ${\mathbf{keepa}}=\mathrm{Nag_StoreAll}$, ${\mathbf{lenc}}\ge 2×{n}_{l}×{n}_{a}$.
5:     c[lenc]doubleOutput
On exit: the coefficients of a multi-level wavelet transform of the dataset.
The coefficients are stored in c as follows:
If ${\mathbf{keepa}}=\mathrm{Nag_StoreFinal}$,
${\mathbf{C}}\left(1:{n}_{a}\right)$
Contains the level ${n}_{l}$ approximation coefficients;
${\mathbf{C}}\left({n}_{a}+\left(i-1\right)×{n}_{d}+1:{n}_{a}+i×{n}_{d}\right)$
Contains the level $\left({n}_{l}-\mathit{i}+1\right)$ detail coefficients, for $\mathit{i}=1,2,\dots ,{n}_{l}$;
If ${\mathbf{keepa}}=\mathrm{Nag_StoreAll}$,
${\mathbf{C}}\left(\left(i-1\right)×{n}_{a}+1:i×{n}_{a}\right)$
Contains the level $\left({n}_{l}-\mathit{i}+1\right)$ approximation coefficients, for $\mathit{i}=1,2,\dots ,{n}_{l}$;
${\mathbf{C}}\left({n}_{l}×{n}_{a}+\left(i-1\right)×{n}_{d}+1:{n}_{l}×{n}_{a}+i×{n}_{d}\right)$
Contains the level i detail coefficients, for $\mathit{i}=1,2,\dots ,{n}_{l}$;
The values ${n}_{a}$ and ${n}_{d}$ denote the numbers of approximation and detail coefficients respectively, which are equal and returned in na.
6:     nwlIntegerInput
On entry: the number of levels, ${n}_{l}$, in the multi-level resolution to be performed.
Constraint: $1\le {\mathbf{nwl}}\le {l}_{\mathrm{max}}$, where ${l}_{\mathrm{max}}$ is the value returned in nwlmax (the maximum number of levels) by the call to the initialization function nag_wfilt (c09aac).
7:     naInteger *Output
On exit: na contains the number of approximation coefficients, ${n}_{a}$, at each level which is equal to the number of detail coefficients, ${n}_{d}$. With periodic end extension (${\mathbf{mode}}=\mathrm{Nag_Periodic}$ in nag_wfilt (c09aac)) this is the same as the length, n, of the data array, x.
8:     icomm[$100$]IntegerCommunication Array
On entry: contains details of the discrete wavelet transform and the problem dimension as setup in the call to the initialization function nag_wfilt (c09aac).
On exit: contains additional information on the computed transform.
9:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ARRAY_DIM_LEN
On entry, lenc is set too small: ${\mathbf{lenc}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lenc}}\ge ⟨\mathit{\text{value}}⟩$.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INITIALIZATION
On entry, n is inconsistent with the value passed to the initialization function: ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$, n should be $⟨\mathit{\text{value}}⟩$.
On entry, nwl is larger than the maximum number of levels returned by the initialization function: ${\mathbf{nwl}}=⟨\mathit{\text{value}}⟩$, maximum = $⟨\mathit{\text{value}}⟩$.
On entry, the initialization function nag_wfilt (c09aac) has not been called first or it has not been called with ${\mathbf{wtrans}}=\mathrm{Nag_MODWTMulti}$, or the communication array icomm has become corrupted.
NE_INT
On entry, ${\mathbf{nwl}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nwl}}\ge 1$.
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.

## 7  Accuracy

The accuracy of the wavelet transform depends only on the floating-point operations used in the convolution and downsampling and should thus be close to machine precision.

## 8  Parallelism and Performance

Not applicable.

The wavelet coefficients at each level can be extracted from the output array c using the information contained in na on exit.

## 10  Example

A set of data values (${\mathbf{n}}=64$) is decomposed using the MODWT over two levels and then the inverse (nag_imlmodwt (c09ddc)) is applied to restore the original data set.

### 10.1  Program Text

Program Text (c09dcce.c)

### 10.2  Program Data

Program Data (c09dcce.d)

### 10.3  Program Results

Program Results (c09dcce.r)