Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_wav_1d_mxolap_multi_fwd (c09dc)

## Purpose

nag_wav_1d_mxolap_multi_fwd (c09dc) computes the one-dimensional multi-level maximal overlap discrete wavelet transform (MODWT). The initialization function nag_wav_1d_init (c09aa) must be called first to set up the MODWT options.

## Syntax

[c, na, icomm, ifail] = c09dc(x, keepa, lenc, nwl, icomm, 'n', n)
[c, na, icomm, ifail] = nag_wav_1d_mxolap_multi_fwd(x, keepa, lenc, nwl, icomm, 'n', n)

## Description

nag_wav_1d_mxolap_multi_fwd (c09dc) 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_wav_1d_init (c09aa) 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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
x contains the input dataset ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:     $\mathrm{keepa}$ – string (length ≥ 1)
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}}=\text{'A'}$
Retain approximation coefficients for all levels computed.
${\mathbf{keepa}}=\text{'F'}$
Retain approximation coefficients for only the final level computed.
Constraint: ${\mathbf{keepa}}=\text{'A'}$ or $\text{'F'}$.
3:     $\mathrm{lenc}$int64int32nag_int scalar
The dimension of the array c. c must be large enough to contain the number of wavelet coefficients.
If ${\mathbf{keepa}}=\text{'F'}$, the total number of coefficients, ${n}_{c}$, is returned in nwc by the call to the initialization function nag_wav_1d_init (c09aa) 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}}=\text{'A'}$, ${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}}=\text{'F'}$, ${\mathbf{lenc}}\ge \left({n}_{l}+1\right)×{n}_{a}$;
• if ${\mathbf{keepa}}=\text{'A'}$, ${\mathbf{lenc}}\ge 2×{n}_{l}×{n}_{a}$.
4:     $\mathrm{nwl}$int64int32nag_int scalar
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_wav_1d_init (c09aa).
5:     $\mathrm{icomm}\left(100\right)$int64int32nag_int array
Contains details of the discrete wavelet transform and the problem dimension as setup in the call to the initialization function nag_wav_1d_init (c09aa).

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
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_wav_1d_init (c09aa).

### Output Parameters

1:     $\mathrm{c}\left({\mathbf{lenc}}\right)$ – double array
The coefficients of a multi-level wavelet transform of the dataset.
The coefficients are stored in c as follows:
If ${\mathbf{keepa}}=\text{'F'}$,
${\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}}=\text{'A'}$,
${\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.
2:     $\mathrm{na}$int64int32nag_int scalar
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}}=\text{'P'}$ in nag_wav_1d_init (c09aa)) this is the same as the length, n, of the data array, x.
3:     $\mathrm{icomm}\left(100\right)$int64int32nag_int array
Contains additional information on the computed transform.
4:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
On entry, n is inconsistent with the value passed to the initialization function.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{keepa}}=_$ was an illegal value.
${\mathbf{ifail}}=4$
lenc is too small.
${\mathbf{ifail}}=6$
Constraint: ${\mathbf{nwl}}\ge 1$.
On entry, nwl is larger than the maximum number of levels returned by the initialization function.
${\mathbf{ifail}}=8$
On entry, the initialization function nag_wav_1d_init (c09aa) has not been called first or it has not been called with ${\mathbf{wtrans}}=\text{'U'}$, or the communication array icomm has become corrupted.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

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

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

## Example

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

fprintf('c09dc example results\n\n');

% Decompose x using maximal overlap discrete wavelet over 2 levels

n      = int64(64);
x      = [6.5271 6.5120 6.5016 6.5237 6.4625 6.3496 6.4025 6.4035 ...
6.4407 6.4746 6.5095 6.6551 6.6100 6.5969 6.6083 6.6520 ...
6.7113 6.7227 6.7196 6.7649 6.7794 6.8037 6.8308 6.7712 ...
6.7067 6.7690 6.7068 6.7024 6.6463 6.6098 6.5900 6.5960 ...
6.5457 6.5470 6.5797 6.5895 6.6275 6.6795 6.6598 6.6925 ...
6.6873 6.7223 6.7205 6.6843 6.7030 6.6470 6.6008 6.6061 ...
6.6097 6.6485 6.6394 6.6571 6.6357 6.6224 6.6073 6.6075 ...
6.6379 6.6294 6.5906 6.6258 6.6369 6.6515 6.6826 6.7042];

wavnam = 'DB4';
mode   = 'Periodic';
wtrans = 'U';
keepa = 'All';
fprintf(' MLMODWT :: Wavelet : %10s, End mode : %10s, n = %10d\n',...
wavnam, mode, n);
fprintf('         :: Keepa   : %10s\n\n',keepa);

% Setup for wavelet
[nwlmax, nf, nwc, icomm, ifail] = c09aa(wavnam, wtrans, mode, n);

% Compute decomposition over two levels
nwl = int64(2);
lenc = 2*n*nwl;
[c, na,  icomm, ifail] = c09dc(x, keepa, lenc, nwl, icomm);

fprintf(' Number of Levels                     : %10d\n',nwl);
fprintf(' Number of coefficients in each level : %10d\n\n',na);
fprintf(' Wavelet coefficients C : \n');
fprintf('%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n',c)

% Reconstruct
[y, ifail] = c09dd(nwl, keepa, c, n, icomm);

fprintf('\n Reconstruction       Y : \n')
fprintf('%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n',y)

```
```c09dc example results

MLMODWT :: Wavelet :        DB4, End mode :   Periodic, n =         64
:: Keepa   :        All

Number of Levels                     :          2
Number of coefficients in each level :         64

Wavelet coefficients C :
6.6448   6.6505   6.6415   6.6090   6.5631   6.5119   6.4657   6.4371
6.4162   6.4041   6.4062   6.4235   6.4652   6.5191   6.5744   6.6170
6.6375   6.6496   6.6575   6.6741   6.7038   6.7335   6.7633   6.7849
6.7939   6.7970   6.7868   6.7649   6.7407   6.7102   6.6814   6.6571
6.6269   6.5993   6.5773   6.5598   6.5574   6.5688   6.5881   6.6173
6.6492   6.6741   6.6941   6.7052   6.7078   6.7083   6.7001   6.6842
6.6616   6.6338   6.6146   6.6072   6.6139   6.6306   6.6428   6.6459
6.6384   6.6252   6.6147   6.6113   6.6143   6.6189   6.6264   6.6361
6.6719   6.5883   6.4958   6.4890   6.5103   6.4695   6.3900   6.3656
6.4065   6.4444   6.4727   6.5273   6.6057   6.6409   6.6102   6.6001
6.6469   6.7019   6.7288   6.7330   6.7501   6.7824   6.8064   6.8147
6.7846   6.7332   6.7239   6.7297   6.6971   6.6508   6.6127   6.5897
6.5818   6.5636   6.5476   6.5657   6.5980   6.6284   6.6627   6.6803
6.6821   6.6941   6.7131   6.7182   6.7020   6.6824   6.6562   6.6140
6.5942   6.6126   6.6378   6.6502   6.6498   6.6403   6.6233   6.6086
6.6099   6.6260   6.6300   6.6112   6.6094   6.6358   6.6581   6.6778
0.0107   0.0084   0.0003  -0.0065  -0.0000   0.0196   0.0191  -0.0152
-0.0369  -0.0291  -0.0131   0.0227   0.0461   0.0005  -0.0488  -0.0145
0.0518   0.0503  -0.0038  -0.0243  -0.0087  -0.0111  -0.0316  -0.0191
0.0323   0.0461  -0.0001  -0.0300  -0.0107   0.0164   0.0112  -0.0156
-0.0225  -0.0091   0.0090   0.0244   0.0050  -0.0281  -0.0150   0.0146
0.0145   0.0034  -0.0019   0.0058   0.0188   0.0074  -0.0133  -0.0127
-0.0062  -0.0008   0.0077   0.0022  -0.0151  -0.0192  -0.0041   0.0091
0.0136   0.0230   0.0203  -0.0081  -0.0274  -0.0179  -0.0013   0.0074
-0.0150   0.0126   0.0048  -0.0276  -0.0227   0.0639  -0.0184  -0.0048
-0.0303   0.0180   0.0327  -0.0343   0.0119  -0.0046   0.0167   0.0025
-0.0524   0.0369   0.0029   0.0055  -0.0070  -0.0134   0.0099   0.0088
-0.0095   0.0103  -0.0114  -0.0181   0.0269   0.0132  -0.0371   0.0250
-0.0186   0.0138   0.0022  -0.0058  -0.0112   0.0207  -0.0058  -0.0054
0.0115  -0.0089  -0.0106   0.0180  -0.0096   0.0107  -0.0156   0.0068
0.0074  -0.0242   0.0169   0.0075  -0.0045   0.0031  -0.0108   0.0092
-0.0115   0.0061  -0.0002   0.0078  -0.0012  -0.0168   0.0074   0.0157

Reconstruction       Y :
6.5271   6.5120   6.5016   6.5237   6.4625   6.3496   6.4025   6.4035
6.4407   6.4746   6.5095   6.6551   6.6100   6.5969   6.6083   6.6520
6.7113   6.7227   6.7196   6.7649   6.7794   6.8037   6.8308   6.7712
6.7067   6.7690   6.7068   6.7024   6.6463   6.6098   6.5900   6.5960
6.5457   6.5470   6.5797   6.5895   6.6275   6.6795   6.6598   6.6925
6.6873   6.7223   6.7205   6.6843   6.7030   6.6470   6.6008   6.6061
6.6097   6.6485   6.6394   6.6571   6.6357   6.6224   6.6073   6.6075
6.6379   6.6294   6.5906   6.6258   6.6369   6.6515   6.6826   6.7042
```