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_init (c09ab)

## Purpose

nag_wav_2d_init (c09ab) returns the details of the chosen two-dimensional discrete wavelet filter. For a chosen mother wavelet, discrete wavelet transform type (single-level or multi-level DWT) and end extension method, this function returns the maximum number of levels of resolution (appropriate to a multi-level transform), the filter length, the total number of approximation, horizontal, vertical and diagonal coefficients and the number of coefficients in the second dimension for the single-level case. This function must be called before any of the two-dimensional transform functions in this chapter.

## Syntax

[nwlmax, nf, nwct, nwcn, icomm, ifail] = c09ab(wavnam, wtrans, mode, m, n)
[nwlmax, nf, nwct, nwcn, icomm, ifail] = nag_wav_2d_init(wavnam, wtrans, mode, m, n)

## Description

Two-dimensional discrete wavelet transforms (DWT) are characterised by the mother wavelet, the end extension method and whether multiresolution analysis is to be performed. For the selected combination of choices for these three characteristics, and for given dimensions ($m×n$) of data matrix $A$, nag_wav_2d_init (c09ab) returns the dimension details for the transform determined by this combination. The dimension details are: ${l}_{\mathrm{max}}$, the maximum number of levels of resolution that would be computed were a multi-level DWT applied; ${n}_{f}$, the filter length; ${n}_{\mathrm{ct}}$ the total number of approximation, horizontal, vertical and diagonal coefficients (over all levels in the multi-level DWT case); and ${n}_{\mathrm{cn}}$, the number of coefficients in the second dimension for a single-level DWT. These values are also stored in the communication array icomm, as are the input choices, so that they may be conveniently communicated to the two-dimensional transform functions in this chapter.

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{wavnam}$ – string
The name of the mother wavelet. See the C09 Chapter Introduction for details.
${\mathbf{wavnam}}=\text{'HAAR'}$
Haar wavelet.
${\mathbf{wavnam}}=\text{'DB}\mathbit{n}\text{'}$, where $\mathbit{n}=2,3,\dots ,10$
Daubechies wavelet with $\mathbit{n}$ vanishing moments ($2\mathbit{n}$ coefficients). For example, ${\mathbf{wavnam}}=\text{'DB4'}$ is the name for the Daubechies wavelet with $4$ vanishing moments ($8$ coefficients).
${\mathbf{wavnam}}=\text{'BIOR}\mathbit{x}$.$\mathbit{y}\text{'}$, where $\mathbit{x}$.$\mathbit{y}$ can be one of 1.1, 1.3, 1.5, 2.2, 2.4, 2.6, 2.8, 3.1, 3.3, 3.5 or 3.7
Biorthogonal wavelet of order $\mathbit{x}$.$\mathbit{y}$. For example ${\mathbf{wavnam}}=\text{'BIOR3.1'}$ is the name for the biorthogonal wavelet of order $3.1$.
Constraint: ${\mathbf{wavnam}}=\text{'HAAR'}$, $\text{'DB2'}$, $\text{'DB3'}$, $\text{'DB4'}$, $\text{'DB5'}$, $\text{'DB6'}$, $\text{'DB7'}$, $\text{'DB8'}$, $\text{'DB9'}$, $\text{'DB10'}$, $\text{'BIOR1.1'}$, $\text{'BIOR1.3'}$, $\text{'BIOR1.5'}$, $\text{'BIOR2.2'}$, $\text{'BIOR2.4'}$, $\text{'BIOR2.6'}$, $\text{'BIOR2.8'}$, $\text{'BIOR3.1'}$, $\text{'BIOR3.3'}$, $\text{'BIOR3.5'}$ or $\text{'BIOR3.7'}$.
2:     $\mathrm{wtrans}$ – string (length ≥ 1)
The type of discrete wavelet transform that is to be applied.
${\mathbf{wtrans}}=\text{'S'}$
Single-level decomposition or reconstruction by discrete wavelet transform.
${\mathbf{wtrans}}=\text{'M'}$
Multiresolution, by a multi-level DWT or its inverse.
Constraint: ${\mathbf{wtrans}}=\text{'S'}$ or $\text{'M'}$.
3:     $\mathrm{mode}$ – string (length ≥ 1)
The end extension method.
${\mathbf{mode}}=\text{'P'}$
Periodic end extension.
${\mathbf{mode}}=\text{'H'}$
Half-point symmetric end extension.
${\mathbf{mode}}=\text{'W'}$
Whole-point symmetric end extension.
${\mathbf{mode}}=\text{'Z'}$
Zero end extension.
Constraint: ${\mathbf{mode}}=\text{'P'}$, $\text{'H'}$, $\text{'W'}$ or $\text{'Z'}$.
4:     $\mathrm{m}$int64int32nag_int scalar
The number of elements, $m$, in the first dimension (number of rows of data matrix $A$) of the input data.
Constraint: ${\mathbf{m}}\ge 2$.
5:     $\mathrm{n}$int64int32nag_int scalar
The number of elements, $n$, in the second dimension (number of columns of data matrix $A$) of the input data.
Constraint: ${\mathbf{n}}\ge 2$.

None.

### Output Parameters

1:     $\mathrm{nwlmax}$int64int32nag_int scalar
The maximum number of levels of resolution, ${l}_{\mathrm{max}}$, that can be computed if a multi-level discrete wavelet transform is applied (${\mathbf{wtrans}}=\text{'M'}$). It is such that ${2}^{{l}_{\mathrm{max}}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)<{2}^{{l}_{\mathrm{max}}+1}$, for ${l}_{\mathrm{max}}$ an integer.
If ${\mathbf{wtrans}}=\text{'S'}$, nwlmax is not set.
2:     $\mathrm{nf}$int64int32nag_int scalar
The filter length, ${n}_{f}$, for the supplied mother wavelet. This is used to determine the number of coefficients to be generated by the chosen transform.
3:     $\mathrm{nwct}$int64int32nag_int scalar
The total number of wavelet coefficients, ${n}_{\mathrm{ct}}$, that will be generated. When ${\mathbf{wtrans}}=\text{'S'}$ the number of rows required in each of the output coefficient matrices can be calculated as ${n}_{\mathrm{cm}}={n}_{\mathrm{ct}}/\left(4{n}_{\mathrm{cn}}\right)$. When ${\mathbf{wtrans}}=\text{'M'}$ the length of the array used to store all of the coefficient matrices must be at least ${n}_{\mathrm{ct}}$.
4:     $\mathrm{nwcn}$int64int32nag_int scalar
For a single-level transform (${\mathbf{wtrans}}=\text{'S'}$), the number of coefficients that would be generated in the second dimension, ${n}_{\mathrm{cn}}$, for each coefficient type. For a multi-level transform (${\mathbf{wtrans}}=\text{'M'}$) this is set to $1$.
5:     $\mathrm{icomm}\left(180\right)$int64int32nag_int array
Contains details of the wavelet transform and the problem dimension which is to be communicated to the two-dimensional discrete transform functions in this chapter.
6:     $\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, ${\mathbf{wavnam}}=_$ was an illegal value.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{wtrans}}=_$ was an illegal value.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{mode}}=_$ was an illegal value.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{m}}\ge 2$.
Constraint: ${\mathbf{n}}\ge 2$.
${\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.

Not applicable.

None.

## Example

This example computes the two-dimensional multi-level resolution for a $6×6$ matrix by a discrete wavelet transform using the Haar wavelet with whole-point symmetric end extensions. The number of levels of transformation actually performed is one less than the maximum possible. This number of levels, the length of the wavelet filter, the total number of coefficients and the number of coefficients in each dimension for each level are printed along with the vertical detail coefficients from the first level, before a reconstruction is performed.
```function c09ab_example

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

m = int64(8);
n = int64(8);
a = [6, 7, 8, 0, 1, 9, 7, 8;
9, 1, 9, 9, 2, 8, 1, 9;
3, 0, 4, 1, 3, 1, 0, 4;
2, 5, 9, 4, 4, 2, 5, 9;
1, 8, 3, 3, 5, 3, 8, 3;
8, 1, 6, 4, 6, 1, 1, 6;
8, 1, 1, 1, 2, 3, 1, 6;
9, 2, 2, 4, 6, 1, 2, 9];

fprintf('\nInput data:\n');
disp(a);

wavnam = 'Haar';
mode = 'Whole';
wtrans = 'Multilevel';
[nwl, nf, nwct, nwcn, icomm, ifail] = ...
c09ab(...
wavnam, wtrans, mode, m, n);

lenc = nwct;
% Calculate one less than the max possible number of levels
nwl = nwl - 1;

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

% c09ab returns nwct based on max levels
% so recalculate based on nwl levels
nwct = 3*sum(dwtlvm(1:nwl).*dwtlvn(1:nwl));
nwct = nwct + dwtlvm(1)*dwtlvn(1);

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:nwl)));
fprintf('Number of coefficients in second dimension for each level :\n');
disp(transpose(dwtlvn(1:nwl)));

fprintf('\nTotal number of wavelet coefficients : %d\n', nwct);
fprintf('\nWavelet coefficients c :\n');
% print c using groups of eight
ngroups = nwct/8;
nleft = mod(nwct,8);
for i=0:ngroups-1
fprintf('%8.4f', c(8*i+1:8*i+8));
fprintf('\n');
end
fprintf('%8.4f', c(nwct-nleft+1:nwct));
fprintf('\n\n');

% Now select a nominated matrix of coefficients at a nominated level.
% level 0 is input data, 1 first coeffs and so on up to nwl:
% the deepest level and containing approximation coefficients.
want_level = nwl - 1;

% Only vertical detail coeffs at selected level.
want_coeffs = int64(1);
nwcm = dwtlvm(nwl-want_level+1);
nwcn = dwtlvn(nwl-want_level+1);

% Extract the selected set of coefficients.
[d, icomm, ifail] = c09ey(want_level, want_coeffs, c, icomm);

% Display the selected coefficients
fprintf('\nType %3d coefficients at selected wavelet level %3d :\n', ...
want_coeffs, want_level);
disp(d);

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

```
```c09ab example results

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

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

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

Total number of wavelet coefficients : 64

Wavelet coefficients c :
19.2500 15.5000 18.2500 15.7500 -2.7500  3.5000 -3.2500 -2.2500
5.2500  1.5000  4.2500  0.7500  1.2500 -2.5000  0.7500  0.7500
3.5000  0.0000  0.0000  7.0000  4.0000  4.0000  1.0000 -1.0000
-7.0000  2.0000  3.5000  2.0000 -4.5000 -4.0000  0.0000 -6.0000
1.5000 -2.0000  0.0000 -1.0000 -5.0000 -4.0000 -2.0000 -2.0000
0.0000 -1.0000  0.5000 -1.0000  2.5000 -5.0000  2.0000 -2.0000
-4.5000  3.0000 -7.0000 -0.0000  4.0000 -1.0000 -1.0000  1.0000
-1.0000  0.0000 -1.5000 -3.0000  3.5000  0.0000  5.0000  1.0000

Type   1 coefficients at selected wavelet level   1 :
3.5000    4.0000   -7.0000   -4.5000
0    4.0000    2.0000   -4.0000
0    1.0000    3.5000         0
7.0000   -1.0000    2.0000   -6.0000

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

```