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_multi_fwd (c09cc)

## Purpose

nag_wav_1d_multi_fwd (c09cc) computes the one-dimensional multi-level discrete wavelet transform (DWT). The initialization function nag_wav_1d_init (c09aa) must be called first to set up the DWT options.

## Syntax

[c, dwtlev, icomm, ifail] = c09cc(x, lenc, nwl, icomm, 'n', n)
[c, dwtlev, icomm, ifail] = nag_wav_1d_multi_fwd(x, lenc, nwl, icomm, 'n', n)

## Description

nag_wav_1d_multi_fwd (c09cc) computes the multi-level DWT of one-dimensional data. For a given wavelet and end extension method, nag_wav_1d_multi_fwd (c09cc) will compute a multi-level transform of a data array, xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, using a specified number, nl${n}_{l}$, of levels. The number of levels specified, nl${n}_{l}$, must be no more than the value lmax${l}_{\mathrm{max}}$ returned in nwl by the initialization function nag_wav_1d_init (c09aa) for the given problem. The transform is returned as a set of coefficients for the different levels (packed into a single array) and a representation of the multi-level structure.
The notation used here assigns level 0$0$ to the input dataset, x$x$, with level 1$1$ being the first set of coefficients computed, with the detail coefficients, d1${d}_{1}$, being stored while the approximation coefficients, a1${a}_{1}$, are used as the input to a repeat of the wavelet transform. This process is continued until, at level nl${n}_{l}$, both the detail coefficients, dnl${d}_{{n}_{l}}$, and the approximation coefficients, anl${a}_{{n}_{l}}$ are retained. The output array, C$C$, stores these sets of coefficients in reverse order, starting with anl${a}_{{n}_{l}}$ followed by dnl,dnl1,,d1${d}_{{n}_{l}},{d}_{{n}_{l}-1},\dots ,{d}_{1}$.

None.

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint .
x contains the one-dimensional input dataset xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
2:     lenc – int64int32nag_int scalar
The dimension of the array c as declared in the (sub)program from which nag_wav_1d_multi_fwd (c09cc) is called. c must be large enough to contain the number, nc${n}_{c}$, of wavelet coefficients. The maximum value of nc${n}_{c}$ is returned in nwc by the call to the initialization function nag_wav_1d_init (c09aa) and corresponds to the DWT being continued for the maximum number of levels possible for the given data set. When the number of levels, nl${n}_{l}$, is chosen to be less than the maximum, then nc${n}_{c}$ is correspondingly smaller and lenc can be reduced by noting that the number of coefficients at each level is given by n / 2$⌈\stackrel{-}{n}/2⌉$ for mode = 'P'${\mathbf{mode}}=\text{'P'}$ in nag_wav_1d_init (c09aa) and (n + nf1) / 2$⌊\left(\stackrel{-}{n}+{n}_{f}-1\right)/2⌋$ for mode = 'H','W','Z'${\mathbf{mode}}=\text{'H'},\text{'W'},\text{'Z'}$, where n$\stackrel{-}{n}$ is the number of input data at that level and nf${n}_{f}$ is the filter length provided by the call to nag_wav_1d_init (c09aa). At the final level the storage is doubled to contain the set of approximation coefficients.
Constraint: lencnc${\mathbf{lenc}}\ge {n}_{c}$, where nc${n}_{c}$ is the number of approximation and detail coefficients that correspond to a transform with nwl levels.
3:     nwl – int64int32nag_int scalar
The number of levels, nl${n}_{l}$, in the multi-level resolution to be performed.
Constraint: 1nwllmax$1\le {\mathbf{nwl}}\le {l}_{\mathrm{max}}$, where lmax${l}_{\mathrm{max}}$ is the value returned in nwl (the maximum number of levels) by the call to the initialization function nag_wav_1d_init (c09aa).
4:     icomm(100$100$) – 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:     n – int64int32nag_int scalar
Default: The dimension of the array x.
The number of elements, n$n$, in the data array x$x$.
Constraint: this must be the same as the value n passed to the initialization function nag_wav_1d_init (c09aa).

None.

### Output Parameters

1:     c(lenc) – double array
Let q(i)$q\left(\mathit{i}\right)$ denote the number of coefficients (of each type) produced by the wavelet transform at level i$\mathit{i}$, for i = nl,nl1,,1$\mathit{i}={n}_{l},{n}_{l}-1,\dots ,1$. These values are returned in dwtlev. Setting k1 = q(nl)${k}_{1}=q\left({n}_{l}\right)$ and kj + 1 = kj + q(nlj + 1)${k}_{\mathit{j}+1}={k}_{\mathit{j}}+q\left({n}_{l}-\mathit{j}+1\right)$, for j = 1,2,,nl$\mathit{j}=1,2,\dots ,{n}_{l}$, the coefficients are stored as follows:
c(i)${\mathbf{c}}\left(\mathit{i}\right)$, for i = 1,2,,k1$\mathit{i}=1,2,\dots ,{k}_{1}$
Contains the level nl${n}_{l}$ approximation coefficients, anl${a}_{{n}_{l}}$.
c(i)${\mathbf{c}}\left(\mathit{i}\right)$, for i = k1 + 1,,k2$\mathit{i}={k}_{1}+1,\dots ,{k}_{2}$
Contains the level nl${n}_{l}$ detail coefficients dnl${d}_{{n}_{l}}$.
c(i)${\mathbf{c}}\left(\mathit{i}\right)$, for i = kj + 1,,kj + 1$\mathit{i}={k}_{j}+1,\dots ,{k}_{j+1}$
Contains the level nlj + 1${n}_{l}-\mathit{j}+1$ detail coefficients, for j = 2,3,,nl$\mathit{j}=2,3,\dots ,{n}_{l}$.
2:     dwtlev(nwl + 1${\mathbf{nwl}}+1$) – int64int32nag_int array
The number of transform coefficients at each level. dwtlev(1)${\mathbf{dwtlev}}\left(1\right)$ and dwtlev(2)${\mathbf{dwtlev}}\left(2\right)$ contain the number, q(nl)$q\left({n}_{l}\right)$, of approximation and detail coefficients respectively, for the final level of resolution (these are equal); dwtlev(i)${\mathbf{dwtlev}}\left(\mathit{i}\right)$ contains the number of detail coefficients, q(nli + 2)$q\left({n}_{l}-\mathit{i}+2\right)$, for the (nli + 2${n}_{l}-\mathit{i}+2$)th level, for i = 3,4,,nl + 1$\mathit{i}=3,4,\dots ,{n}_{l}+1$.
3:     icomm(100$100$) – int64int32nag_int array
Contains additional information on the computed transform.
4:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
On entry, n is inconsistent with the value passed to the initialization function nag_wav_1d_init (c09aa).
ifail = 3${\mathbf{ifail}}=3$
On entry, lenc < nc*${\mathbf{lenc}}<{n}_{c}^{*}$, where nc*${n}_{c}^{*}$ is the number of coefficients that will be generated given the chosen value of nwl.
ifail = 5${\mathbf{ifail}}=5$
 On entry, nwl < 1${\mathbf{nwl}}<1$, or nwl > lmax${\mathbf{nwl}}>{l}_{\mathrm{max}}$, where lmax${l}_{\mathrm{max}}$ is the value returned in nwl by the call to the initialization function nag_wav_1d_init (c09aa).
ifail = 7${\mathbf{ifail}}=7$
On entry, the initialization function nag_wav_1d_init (c09aa) has not been called first or it has been called with wtrans = 'S'${\mathbf{wtrans}}=\text{'S'}$, or the communication array icomm has become corrupted.

## 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 dwtlev on exit (see the descriptions of c and dwtlev in Section [Parameters]). For example, given an input data set, x$x$, denoising can be carried out by applying a thresholding operation to the detail coefficients at every level. The elements c(k1 + 1 : knl + 1)${\mathbf{c}}\left({k}_{1}+1:{k}_{{n}_{l}+1}\right)$, as described in Section [Parameters], contain the detail coefficients, ij${\stackrel{^}{d}}_{\mathit{i}\mathit{j}}$, for i = nl,nl1,,1$\mathit{i}={n}_{l},{n}_{l}-1,\dots ,1$ and j = 1,2,,q(i)$\mathit{j}=1,2,\dots ,q\left(i\right)$, where ij = dij + σεij${\stackrel{^}{d}}_{ij}={d}_{ij}+\sigma {\epsilon }_{ij}$ and σεij$\sigma {\epsilon }_{ij}$ is the transformed noise term. If some threshold parameter α$\alpha$ is chosen, a simple hard thresholding rule can be applied as
dij =
 { 0, if ​ |d̂ij| ≤ α d̂ij , if ​ |d̂ij| > α,
$d- ij = { 0, if ​ |d^ij| ≤ α d^ij , if ​ |d^ij| > α,$
taking dij${\stackrel{-}{d}}_{ij}$ to be an approximation to the required detail coefficient without noise, dij${d}_{ij}$. The resulting coefficients can then be used as input to nag_wav_1d_multi_inv (c09cd) in order to reconstruct the denoised signal.
See the references given in the introduction to this chapter for a more complete account of wavelet denoising and other applications.

## Example

```function nag_wav_1d_multi_fwd_example
n = int64(64);
wavnam = 'DB4';
mode = 'zero';
wtrans = 'Multilevel';
x = [ 6.5271; 6.512; 6.5016; 6.5237; 6.4625;
6.3496; 6.4025; 6.4035; 6.4407; 6.4746;
6.5095; 6.6551; 6.61; 6.5969; 6.6083;
6.652; 6.7113; 6.7227; 6.7196; 6.7649;
6.7794; 6.8037; 6.8308; 6.7712; 6.7067;
6.769; 6.7068; 6.7024; 6.6463; 6.6098;
6.59; 6.596; 6.5457; 6.547; 6.5797;
6.5895; 6.6275; 6.6795; 6.6598; 6.6925;
6.6873; 6.7223; 6.7205; 6.6843; 6.703;
6.647; 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];
fprintf('\n Input Data:\n');
for i=1:8:double(n)
fprintf('%8.4f ', x(i:i+8-1));
fprintf('\n');
end
fprintf('\n');

% Query wavelet filter dimensions
[nwl, nf, nwc, icomm, ifail] = nag_wav_1d_init(wavnam, wtrans, mode, n);

if ifail == int64(0)
% Perform Discrete Wavelet transform
[c, dwtlev, icomm, ifail] = nag_wav_1d_multi_fwd(x, nwc, nwl, icomm);

if ifail == int64(0)
fprintf(' Length of wavelet filter :             %10d\n', nf);
fprintf(' Number of Levels :                     %10d\n\n', nwl);
fprintf(' Number of coefficients in each level :\n     ');
fprintf(' %8d', dwtlev);
fprintf('\n');
fprintf(' Total number of wavelet coefficients : %10d\n\n', nwc);
fprintf(' Wavelet coefficients C : \n');
for i=1:8:double(nwc)
if i+8-1 <= numel(c)
fprintf('%8.4f ', c(i:i+8-1));
else
fprintf('%8.4f ', c(i:numel(c)));
end
fprintf('\n');
end
fprintf('\n');

% Reconstruct original data
[y, ifail] = nag_wav_1d_multi_inv(nwl, c, n, icomm);

if ifail == int64(0)
fprintf('\n Reconstruction       Y : \n');
for i=1:8:double(n)
fprintf('%8.4f ', y(i:i+8-1));
fprintf('\n');
end
fprintf('\n');
end
end
end
```
```

Input Data:
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

Length of wavelet filter :                      8
Number of Levels :                              6

Number of coefficients in each level :
7        7        8       10       14       21       35
Total number of wavelet coefficients :        102

Wavelet coefficients C :
0.0000  -0.0227  -0.3446   2.7574 -10.1970  44.8800  15.9443   0.0010
-0.4881 -10.2673  11.3258  -1.7469   2.0785  -0.7334  -0.0054  -0.1402
-5.8980  -1.1527   5.5613   2.1352   0.3203  -0.4004   0.0010   0.5229
0.5055  -2.7274  -0.0911  -0.2806  -0.3669   2.9467  -0.3799  -0.1552
0.0218   0.0922   5.4626  -2.1620   0.5196  -0.0287  -0.0199   0.0920
-0.0134  -0.1298  -5.5168   2.3105  -0.5383  -0.0155   0.3057   0.6186
-1.5542   0.2682   0.1566   0.0030  -0.0152  -0.0589   0.0126   0.0063
0.0171  -0.0268   0.0077  -0.0189   0.0207   0.0104  -0.3207  -0.6062
1.6288  -0.2414  -0.0671   3.1657  -1.1462   0.2785   0.0523  -0.0030
-0.0270  -0.0442   0.0090   0.0171  -0.0230  -0.0015   0.0213  -0.0402
-0.0263  -0.0099   0.0021  -0.0250   0.0210  -0.0028  -0.0298  -0.0095
0.0034   0.0281  -0.0188  -0.0002  -0.0173  -0.0076  -0.0014   0.0184
-0.0318   0.0048   0.0047  -3.2555   1.1710  -0.2913

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

```
```function c09cc_example
n = int64(64);
wavnam = 'DB4';
mode = 'zero';
wtrans = 'Multilevel';
x = [ 6.5271; 6.512; 6.5016; 6.5237; 6.4625;
6.3496; 6.4025; 6.4035; 6.4407; 6.4746;
6.5095; 6.6551; 6.61; 6.5969; 6.6083;
6.652; 6.7113; 6.7227; 6.7196; 6.7649;
6.7794; 6.8037; 6.8308; 6.7712; 6.7067;
6.769; 6.7068; 6.7024; 6.6463; 6.6098;
6.59; 6.596; 6.5457; 6.547; 6.5797;
6.5895; 6.6275; 6.6795; 6.6598; 6.6925;
6.6873; 6.7223; 6.7205; 6.6843; 6.703;
6.647; 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];
fprintf('\n Input Data:\n');
for i=1:8:double(n)
fprintf('%8.4f ', x(i:i+8-1));
fprintf('\n');
end
fprintf('\n');

% Query wavelet filter dimensions
[nwl, nf, nwc, icomm, ifail] = c09aa(wavnam, wtrans, mode, n);

if ifail == int64(0)
% Perform Discrete Wavelet transform
[c, dwtlev, icomm, ifail] = c09cc(x, nwc, nwl, icomm);

if ifail == int64(0)
fprintf(' Length of wavelet filter :             %10d\n', nf);
fprintf(' Number of Levels :                     %10d\n\n', nwl);
fprintf(' Number of coefficients in each level :\n     ');
fprintf(' %8d', dwtlev);
fprintf('\n');
fprintf(' Total number of wavelet coefficients : %10d\n\n', nwc);
fprintf(' Wavelet coefficients C : \n');
for i=1:8:double(nwc)
if i+8-1 <= numel(c)
fprintf('%8.4f ', c(i:i+8-1));
else
fprintf('%8.4f ', c(i:numel(c)));
end
fprintf('\n');
end
fprintf('\n');

% Reconstruct original data
[y, ifail] = c09cd(nwl, c, n, icomm);

if ifail == int64(0)
fprintf('\n Reconstruction       Y : \n');
for i=1:8:double(n)
fprintf('%8.4f ', y(i:i+8-1));
fprintf('\n');
end
fprintf('\n');
end
end
end
```
```

Input Data:
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

Length of wavelet filter :                      8
Number of Levels :                              6

Number of coefficients in each level :
7        7        8       10       14       21       35
Total number of wavelet coefficients :        102

Wavelet coefficients C :
0.0000  -0.0227  -0.3446   2.7574 -10.1970  44.8800  15.9443   0.0010
-0.4881 -10.2673  11.3258  -1.7469   2.0785  -0.7334  -0.0054  -0.1402
-5.8980  -1.1527   5.5613   2.1352   0.3203  -0.4004   0.0010   0.5229
0.5055  -2.7274  -0.0911  -0.2806  -0.3669   2.9467  -0.3799  -0.1552
0.0218   0.0922   5.4626  -2.1620   0.5196  -0.0287  -0.0199   0.0920
-0.0134  -0.1298  -5.5168   2.3105  -0.5383  -0.0155   0.3057   0.6186
-1.5542   0.2682   0.1566   0.0030  -0.0152  -0.0589   0.0126   0.0063
0.0171  -0.0268   0.0077  -0.0189   0.0207   0.0104  -0.3207  -0.6062
1.6288  -0.2414  -0.0671   3.1657  -1.1462   0.2785   0.0523  -0.0030
-0.0270  -0.0442   0.0090   0.0171  -0.0230  -0.0015   0.0213  -0.0402
-0.0263  -0.0099   0.0021  -0.0250   0.0210  -0.0028  -0.0298  -0.0095
0.0034   0.0281  -0.0188  -0.0002  -0.0173  -0.0076  -0.0014   0.0184
-0.0318   0.0048   0.0047  -3.2555   1.1710  -0.2913

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

```