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_2d_multi_fwd (c09ec)

## Purpose

nag_wav_2d_multi_fwd (c09ec) computes the two-dimensional multi-level discrete wavelet transform (DWT). The initialization function nag_wav_2d_init (c09ab) must be called first to set up the DWT options.

## Syntax

[c, dwtlvm, dwtlvn, icomm, ifail] = c09ec(a, lenc, nwl, icomm, 'm', m, 'n', n)
[c, dwtlvm, dwtlvn, icomm, ifail] = nag_wav_2d_multi_fwd(a, lenc, nwl, icomm, 'm', m, 'n', n)

## Description

nag_wav_2d_multi_fwd (c09ec) computes the multi-level DWT of two-dimensional data. For a given wavelet and end extension method, nag_wav_2d_multi_fwd (c09ec) will compute a multi-level transform of a matrix A$A$, 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_2d_init (c09ab) 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 matrix, A$A$. Level 1 consists of the first set of coefficients computed: the vertical (v1${v}_{1}$), horizontal (h1${h}_{1}$) and diagonal (d1${d}_{1}$) coefficients are stored at this level while the approximation (a1${a}_{1}$) coefficients are used as the input to a repeat of the wavelet transform at the next level. This process is continued until, at level nl${n}_{l}$, all four types of coefficients are stored. The output array, C$C$, stores these sets of coefficients in reverse order, starting with anl${a}_{{n}_{l}}$ followed by vnl , hnl , dnl , vnl1 , hnl1 , dnl1 , , v1 , h1 , d1 ${v}_{{n}_{l}},{h}_{{n}_{l}},{d}_{{n}_{l}},{v}_{{n}_{l}-1},{h}_{{n}_{l}-1},{d}_{{n}_{l}-1},\dots ,{v}_{1},{h}_{1},{d}_{1}$.

None.

## Parameters

### Compulsory Input Parameters

1:     a(lda,n) – double array
lda, the first dimension of the array, must satisfy the constraint ldam$\mathit{lda}\ge {\mathbf{m}}$.
The m$m$ by n$n$ data matrix A$A$.
2:     lenc – int64int32nag_int scalar
The dimension of the array c as declared in the (sub)program from which nag_wav_2d_multi_fwd (c09ec) is called. c must be large enough to contain, nct${n}_{\mathrm{ct}}$, wavelet coefficients. The maximum value of nct${n}_{\mathrm{ct}}$ is returned in nwct by the call to the initialization function nag_wav_2d_init (c09ab) 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, lmax${l}_{\mathrm{max}}$, then nct${n}_{\mathrm{ct}}$ is correspondingly smaller and lenc can be reduced by noting that the vertical, horizontal and diagonal coefficients are stored at every level and that in addition the approximation coefficients are stored for the final level only. The number of coefficients stored at each level is given by 3 × m / 2 × n / 2 $3×⌈\stackrel{-}{m}/2⌉×⌈\stackrel{-}{n}/2⌉$ for mode = 'P'${\mathbf{mode}}=\text{'P'}$ in nag_wav_2d_init (c09ab) and 3 × (m + nf1) / 2 × (n + nf1) / 2 $3×⌊\left(\stackrel{-}{m}+{n}_{f}-1\right)/2⌋×⌊\left(\stackrel{-}{n}+{n}_{f}-1\right)/2⌋$ for mode = 'H','W','Z'${\mathbf{mode}}=\text{'H'},\text{'W'},\text{'Z'}$, where the input data is of dimension m × n$\stackrel{-}{m}×\stackrel{-}{n}$ at that level and nf${n}_{f}$ is the filter length nf provided by the call to nag_wav_2d_init (c09ab). At the final level the storage is 4 / 3$4/3$ times this value to contain the set of approximation coefficients.
Constraint: lencnct${\mathbf{lenc}}\ge {n}_{\mathrm{ct}}$, where nct${n}_{\mathrm{ct}}$ is the total number of 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_2d_init (c09ab).
4:     icomm(180$180$) – 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_2d_init (c09ab).

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The first dimension of the array a.
Number of rows, m$m$, of data matrix A$A$.
Constraint: this must be the same as the value m passed to the initialization function nag_wav_2d_init (c09ab).
2:     n – int64int32nag_int scalar
Default: The second dimension of the array a.
Number of columns, n$n$, of data matrix A$A$.
Constraint: this must be the same as the value n passed to the initialization function nag_wav_2d_init (c09ab).

lda

### Output Parameters

1:     c(lenc) – double array
The coefficients of a multi-level wavelet transform of the dataset.
Let q(i)$q\left(\mathit{i}\right)$ denote the number of coefficients (of each type) at level i$\mathit{i}$, for i = 1,2,,nl$\mathit{i}=1,2,\dots ,{n}_{l}$, such that q(i) = dwtlvm( nl i + 1 ) × dwtlvn( nl i + 1 ) $q\left(i\right)={\mathbf{dwtlvm}}\left({n}_{l}-i+1\right)×{\mathbf{dwtlvn}}\left({n}_{l}-i+1\right)$. Then, letting k1 = q(nl)${k}_{1}=q\left({n}_{l}\right)$ and kj + 1 = kj + q(nlj / 3 + 1)${k}_{\mathit{j}+1}={k}_{\mathit{j}}+q\left({n}_{l}-⌈\mathit{j}/3⌉+1\right)$, for j = 1,2,,3nl$\mathit{j}=1,2,\dots ,3{n}_{l}$, the coefficients are stored in c 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 = kj + 1,,kj + 1$\mathit{i}={k}_{j}+1,\dots ,{k}_{j+1}$
Contains the level nlj / 3 + 1${n}_{l}-⌈j/3⌉+1$ vertical, horizontal and diagonal coefficients. These are:
• vertical coefficients if j  mod  3 = 1;
• horizontal coefficients if j  mod  3 = 2;
• diagonal coefficients if j  mod  3 = 0,
for j = 1,,3nl$j=1,\dots ,3{n}_{l}$
2:     dwtlvm(nwl) – int64int32nag_int array
The number of coefficients in the first dimension for each coefficient type at each level. dwtlvm(i)${\mathbf{dwtlvm}}\left(\mathit{i}\right)$ contains the number of coefficients in the first dimension (for each coefficient type computed) at the (nli + 1${n}_{l}-\mathit{i}+1$)th level of resolution, for i = 1,2,,nl$\mathit{i}=1,2,\dots ,{n}_{l}$. Thus for the first nl1${n}_{l}-1$ levels of resolution, dwtlvm(nli + 1)${\mathbf{dwtlvm}}\left({n}_{l}-\mathit{i}+1\right)$ is the size of the first dimension of the matrices of vertical, horizontal and diagonal coefficients computed at this level; for the final level of resolution, dwtlvm(1)${\mathbf{dwtlvm}}\left(1\right)$ is the size of the first dimension of the matrices of approximation, vertical, horizontal and diagonal coefficients computed.
3:     dwtlvn(nwl) – int64int32nag_int array
The number of coefficients in the second dimension for each coefficient type at each level. dwtlvn(i)${\mathbf{dwtlvn}}\left(\mathit{i}\right)$ contains the number of coefficients in the second dimension (for each coefficient type computed) at the (nli + 1${n}_{l}-\mathit{i}+1$)th level of resolution, for i = 1,2,,nl$\mathit{i}=1,2,\dots ,{n}_{l}$. Thus for the first nl1${n}_{l}-1$ levels of resolution, dwtlvn(nli + 1)${\mathbf{dwtlvn}}\left({n}_{l}-\mathit{i}+1\right)$ is the size of the second dimension of the matrices of vertical, horizontal and diagonal coefficients computed at this level; for the final level of resolution, dwtlvn(1)${\mathbf{dwtlvn}}\left(1\right)$ is the size of the second dimension of the matrices of approximation, vertical, horizontal and diagonal coefficients computed.
4:     icomm(180$180$) – int64int32nag_int array
Contains additional information on the computed transform.
5:     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$
Constraint: m = m${\mathbf{m}}=m$, the value of m on initialization (see nag_wav_2d_init (c09ab)).
Constraint: n = n${\mathbf{n}}=n$, the value of n on initialization (see nag_wav_2d_init (c09ab)).
ifail = 2${\mathbf{ifail}}=2$
Constraint: ldam$\mathit{lda}\ge {\mathbf{m}}$.
ifail = 3${\mathbf{ifail}}=3$
lenc is too small, the total number of coefficents to be generated.
ifail = 5${\mathbf{ifail}}=5$
Constraint: ${\mathbf{nwl}}\le {\mathbf{nwl}}$ in nag_wav_2d_init (c09ab).
Constraint: nwl1${\mathbf{nwl}}\ge 1$.
ifail = 7${\mathbf{ifail}}=7$
Either the initialization function has not been called first or icomm has been corrupted.
Either the initialization function was called with wtrans = 'S'${\mathbf{wtrans}}=\text{'S'}$ or icomm has been corrupted.
ifail = 999${\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 dwtlvm and dwtlvn on exit (see the descriptions of c, dwtlvm and dwtlvn in Section [Parameters]). For example, given an input data set, A$A$, denoising can be carried out by applying a thresholding operation to the detail (vertical, horizontal and diagonal) coefficients at every level. The elements c(k1 + 1) ${\mathbf{c}}\left({k}_{1}+1\right)$ to c(knl + 1)${\mathbf{c}}\left({k}_{{n}_{l}+1}\right)$, as described in Section [Parameters], contain the detail coefficients, ij${\stackrel{^}{c}}_{ij}$, for i = nl,nl1,,1$\mathit{i}={n}_{l},{n}_{l}-1,\dots ,1$ and j = 1,2,,3q(i)$\mathit{j}=1,2,\dots ,3q\left(i\right)$, where q(i)$q\left(i\right)$ is the number of each type of coefficient at level i$i$ and ij = cij + σεij${\stackrel{^}{c}}_{ij}={c}_{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
cij =
 { 0, if ​ |ĉij| ≤ α ĉij , if ​ |ĉij| > α,
$c- ij = { 0, if ​ |c^ij| ≤ α c^ij , if ​ |c^ij| > α,$
taking cij${\stackrel{-}{c}}_{ij}$ to be an approximation to the required detail coefficient without noise, cij${c}_{ij}$. The resulting coefficients can then be used as input to nag_wav_2d_multi_inv (c09ed) 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_2d_multi_fwd_example
m = int64(7);
n = int64(8);
wavnam = 'DB2';
mode = 'Half';
wtrans = 'Multilevel';
a = [3, 7, 9, 1, 9, 9, 1, 0;
9, 9, 3, 3, 4, 1, 2, 4;
7, 8, 1, 3, 8, 9, 3, 3;
1, 1, 1, 1, 2, 8, 4, 0;
1, 2, 4, 6, 5, 6, 5, 4;
2, 2, 5, 7, 3, 6, 6, 8;
7, 9, 3, 1, 3, 4, 7, 2];

fprintf('\nInput data a:\n');
disp(a);
[nwl, nf, nwct, nwcn, icomm, ifail] = nag_wav_2d_init(wavnam, wtrans, mode, m, n);

lenc = nwct;
% Perform Discrete Wavelet transform
[c, dwtlvm, dwtlvn, icomm, ifail] = nag_wav_2d_multi_fwd(a, lenc, nwl, icomm);

fprintf('\nLength of wavelet filter : %d\n', nf);
fprintf('Number of Levels :         %d\n', nwl);
fprintf('Number of coefficients in first dimension for each level :\n');
disp(transpose(dwtlvm(1:double(nwl))));
fprintf('Number of coefficients in second dimension for each level :\n');
disp(transpose(dwtlvn(1:double(nwl))));

fprintf('\nTotal number of wavelet coefficients : %d\n', nwct);
fprintf('\nWavelet coefficients c :\n');
jstart = 1;
for ilevel = 1:double(nwl)
fprintf('-------------------------------------------------------\n');
fprintf('Level %d output is %d by %d\n', ...
nwl-ilevel+1, dwtlvm(ilevel), dwtlvn(ilevel));
fprintf('-------------------------------------------------------\n');

iskip = double(dwtlvm(ilevel));
i2 = iskip*double(dwtlvn(ilevel)) - 1;

for itype_coeffs = 1:4
switch itype_coeffs
case {1}
if (ilevel == nwl)
fprintf('Approximation coefficients:\n');
end
case {2}
fprintf('Vertical coefficients:\n');
case {3}
fprintf('Horizontal coefficients:\n');
case {4}
fprintf('Diagonal coefficients:\n');
end
if (itype_coeffs>1 || ilevel==1)
for i1 = jstart:jstart+iskip-1
fprintf('%8.4f',c(i1:iskip:i1+i2));
fprintf('\n');
end
jstart = jstart + i2 + 1;
end
end
fprintf('\n');
end

% Reconstruct original data
[b, ifail] = nag_wav_2d_multi_inv(nwl, c, m, n, icomm);
fprintf('Reconstruction       b:\n');
disp(b);
```
```

Input data a:
3     7     9     1     9     9     1     0
9     9     3     3     4     1     2     4
7     8     1     3     8     9     3     3
1     1     1     1     2     8     4     0
1     2     4     6     5     6     5     4
2     2     5     7     3     6     6     8
7     9     3     1     3     4     7     2

Length of wavelet filter : 4
Number of Levels :         2
Number of coefficients in first dimension for each level :
4                    5

Number of coefficients in second dimension for each level :
4                    5

Total number of wavelet coefficients : 139

Wavelet coefficients c :
-------------------------------------------------------
Level 2 output is 4 by 4
-------------------------------------------------------
24.9724 25.6017 20.8900  7.9280
27.6100 27.0955 18.7941  8.2804
11.2663 11.0273 19.6410 18.6651
27.6050 26.6443 14.5913 18.0835
Vertical coefficients:
-2.5552 -6.1078 -4.0629  8.2136
-1.6061 -7.2355 -3.3633  7.6075
-0.2225 -1.6283 -0.5301  3.7415
-0.9052 -6.5810  0.8023  1.8591
Horizontal coefficients:
-3.8069 -3.0730  2.1121 -1.8525
-2.7548 -4.5949 -0.8321 -4.8155
4.8398  4.5104 -1.5308 -0.6456
-6.4332 -4.5381  2.4753  6.8224
Diagonal coefficients:
-0.8978 -0.2326 -1.2515  2.6346
0.5708 -4.9783 -1.5309  6.4569
-0.1854 -1.8430  0.2426 -0.0754
0.0345  7.1864  1.5938 -5.9745

-------------------------------------------------------
Level 1 output is 5 by 5
-------------------------------------------------------
Approximation coefficients:
Vertical coefficients:
-2.5981  4.6471  2.5392 -2.8415 -0.2165
-1.3203 -0.0592  3.0490 -2.5837  1.0458
-0.4330 -1.6405 -1.1752  0.2533 -2.3448
-0.4118 -0.0682 -2.4608 -0.0167  0.4387
-1.5368 -1.1450 -0.5547  4.5936 -3.6863
Horizontal coefficients:
-4.3301 -1.8170  0.8023  5.7566 -2.8146
4.3089  3.6908  0.8349  3.4653  1.7108
-1.5311 -1.0736  1.5257  0.0212 -0.9608
2.8873  3.1148 -1.9118 -0.4007 -1.5302
-2.2377 -2.7611  2.4453 -0.3705  4.3448
Diagonal coefficients:
-1.5000  4.4151 -0.0057 -0.8236 -1.1250
-0.1953 -2.9530  1.8840 -1.7635  0.9877
-0.4330  0.2745  1.1450  0.4632 -0.5547
-0.3538 -0.3215  0.6462  1.3705 -1.2778
0.7288  0.4587 -1.8873 -1.8828  2.4028

Reconstruction       b:
3.0000    7.0000    9.0000    1.0000    9.0000    9.0000    1.0000    0.0000
9.0000    9.0000    3.0000    3.0000    4.0000    1.0000    2.0000    4.0000
7.0000    8.0000    1.0000    3.0000    8.0000    9.0000    3.0000    3.0000
1.0000    1.0000    1.0000    1.0000    2.0000    8.0000    4.0000    0.0000
1.0000    2.0000    4.0000    6.0000    5.0000    6.0000    5.0000    4.0000
2.0000    2.0000    5.0000    7.0000    3.0000    6.0000    6.0000    8.0000
7.0000    9.0000    3.0000    1.0000    3.0000    4.0000    7.0000    2.0000

```
```function c09ec_example
m = int64(7);
n = int64(8);
wavnam = 'DB2';
mode = 'Half';
wtrans = 'Multilevel';
a = [3, 7, 9, 1, 9, 9, 1, 0;
9, 9, 3, 3, 4, 1, 2, 4;
7, 8, 1, 3, 8, 9, 3, 3;
1, 1, 1, 1, 2, 8, 4, 0;
1, 2, 4, 6, 5, 6, 5, 4;
2, 2, 5, 7, 3, 6, 6, 8;
7, 9, 3, 1, 3, 4, 7, 2];

fprintf('\nInput data a:\n');
disp(a);
[nwl, nf, nwct, nwcn, icomm, ifail] = c09ab(wavnam, wtrans, mode, m, n);

lenc = nwct;
% Perform Discrete Wavelet transform
[c, dwtlvm, dwtlvn, icomm, ifail] = c09ec(a, lenc, nwl, icomm);

fprintf('\nLength of wavelet filter : %d\n', nf);
fprintf('Number of Levels :         %d\n', nwl);
fprintf('Number of coefficients in first dimension for each level :\n');
disp(transpose(dwtlvm(1:double(nwl))));
fprintf('Number of coefficients in second dimension for each level :\n');
disp(transpose(dwtlvn(1:double(nwl))));

fprintf('\nTotal number of wavelet coefficients : %d\n', nwct);
fprintf('\nWavelet coefficients c :\n');
jstart = 1;
for ilevel = 1:double(nwl)
fprintf('-------------------------------------------------------\n');
fprintf('Level %d output is %d by %d\n', ...
nwl-ilevel+1, dwtlvm(ilevel), dwtlvn(ilevel));
fprintf('-------------------------------------------------------\n');

iskip = double(dwtlvm(ilevel));
i2 = iskip*double(dwtlvn(ilevel)) - 1;

for itype_coeffs = 1:4
switch itype_coeffs
case {1}
if (ilevel == nwl)
fprintf('Approximation coefficients:\n');
end
case {2}
fprintf('Vertical coefficients:\n');
case {3}
fprintf('Horizontal coefficients:\n');
case {4}
fprintf('Diagonal coefficients:\n');
end
if (itype_coeffs>1 || ilevel==1)
for i1 = jstart:jstart+iskip-1
fprintf('%8.4f',c(i1:iskip:i1+i2));
fprintf('\n');
end
jstart = jstart + i2 + 1;
end
end
fprintf('\n');
end

% Reconstruct original data
[b, ifail] = c09ed(nwl, c, m, n, icomm);
fprintf('Reconstruction       b:\n');
disp(b);
```
```

Input data a:
3     7     9     1     9     9     1     0
9     9     3     3     4     1     2     4
7     8     1     3     8     9     3     3
1     1     1     1     2     8     4     0
1     2     4     6     5     6     5     4
2     2     5     7     3     6     6     8
7     9     3     1     3     4     7     2

Length of wavelet filter : 4
Number of Levels :         2
Number of coefficients in first dimension for each level :
4                    5

Number of coefficients in second dimension for each level :
4                    5

Total number of wavelet coefficients : 139

Wavelet coefficients c :
-------------------------------------------------------
Level 2 output is 4 by 4
-------------------------------------------------------
24.9724 25.6017 20.8900  7.9280
27.6100 27.0955 18.7941  8.2804
11.2663 11.0273 19.6410 18.6651
27.6050 26.6443 14.5913 18.0835
Vertical coefficients:
-2.5552 -6.1078 -4.0629  8.2136
-1.6061 -7.2355 -3.3633  7.6075
-0.2225 -1.6283 -0.5301  3.7415
-0.9052 -6.5810  0.8023  1.8591
Horizontal coefficients:
-3.8069 -3.0730  2.1121 -1.8525
-2.7548 -4.5949 -0.8321 -4.8155
4.8398  4.5104 -1.5308 -0.6456
-6.4332 -4.5381  2.4753  6.8224
Diagonal coefficients:
-0.8978 -0.2326 -1.2515  2.6346
0.5708 -4.9783 -1.5309  6.4569
-0.1854 -1.8430  0.2426 -0.0754
0.0345  7.1864  1.5938 -5.9745

-------------------------------------------------------
Level 1 output is 5 by 5
-------------------------------------------------------
Approximation coefficients:
Vertical coefficients:
-2.5981  4.6471  2.5392 -2.8415 -0.2165
-1.3203 -0.0592  3.0490 -2.5837  1.0458
-0.4330 -1.6405 -1.1752  0.2533 -2.3448
-0.4118 -0.0682 -2.4608 -0.0167  0.4387
-1.5368 -1.1450 -0.5547  4.5936 -3.6863
Horizontal coefficients:
-4.3301 -1.8170  0.8023  5.7566 -2.8146
4.3089  3.6908  0.8349  3.4653  1.7108
-1.5311 -1.0736  1.5257  0.0212 -0.9608
2.8873  3.1148 -1.9118 -0.4007 -1.5302
-2.2377 -2.7611  2.4453 -0.3705  4.3448
Diagonal coefficients:
-1.5000  4.4151 -0.0057 -0.8236 -1.1250
-0.1953 -2.9530  1.8840 -1.7635  0.9877
-0.4330  0.2745  1.1450  0.4632 -0.5547
-0.3538 -0.3215  0.6462  1.3705 -1.2778
0.7288  0.4587 -1.8873 -1.8828  2.4028

Reconstruction       b:
3.0000    7.0000    9.0000    1.0000    9.0000    9.0000    1.0000    0.0000
9.0000    9.0000    3.0000    3.0000    4.0000    1.0000    2.0000    4.0000
7.0000    8.0000    1.0000    3.0000    8.0000    9.0000    3.0000    3.0000
1.0000    1.0000    1.0000    1.0000    2.0000    8.0000    4.0000    0.0000
1.0000    2.0000    4.0000    6.0000    5.0000    6.0000    5.0000    4.0000
2.0000    2.0000    5.0000    7.0000    3.0000    6.0000    6.0000    8.0000
7.0000    9.0000    3.0000    1.0000    3.0000    4.0000    7.0000    2.0000

```