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_coeff_ext (c09ey)

## Purpose

nag_wav_2d_coeff_ext (c09ey) extracts a selected set of discrete wavelet transform (DWT) coefficients from the full set of coefficients stored in compact form, as computed by nag_wav_2d_multi_fwd (c09ec) (two-dimensional DWT).

## Syntax

[d, icomm, ifail] = c09ey(ilev, cindex, c, icomm, 'lenc', lenc)
[d, icomm, ifail] = nag_wav_2d_coeff_ext(ilev, cindex, c, icomm, 'lenc', lenc)

## Description

nag_wav_2d_coeff_ext (c09ey) is intended to be used after a call to nag_wav_2d_multi_fwd (c09ec) (two-dimensional DWT), which in turn should be preceded by a call to nag_wav_2d_init (c09ab) (two-dimensional wavelet filter initialization). Given an initial two-dimensional data set $A$, a prior call to nag_wav_2d_multi_fwd (c09ec) computes the approximation coefficients (at the highest requested level) and three sets of detail coeficients at all levels and stores these in compact form in a one-dimensional array c. nag_wav_2d_coeff_ext (c09ey) can then extract either the approximation coefficients or one of the sets of detail coefficients at one of the levels into a matrix $D$. The dimensions of $D$ depend on the level extracted and are available from the arrays dwtlvm and dwtlvn as returned by nag_wav_2d_multi_fwd (c09ec) which contain the first and second dimensions respectively. See Multiresolution and higher dimensional DWT in the C09 Chapter Introduction for a discussion of the two-dimensional DWT.

None.

## Parameters

Note: the following notation is used in this section:
• ${n}_{\mathrm{cm}}$ is the number of wavelet coefficients in the first dimension, which, at level ilev, is equal to ${\mathbf{dwtlvm}}\left({\mathbf{nwl}}-{\mathbf{ilev}}+1\right)$ as returned by a call to nag_wav_2d_multi_fwd (c09ec) transforming nwl levels.
• ${n}_{\mathrm{cn}}$ is the number of wavelet coefficients in the second dimension, which, at level ilev, is equal to ${\mathbf{dwtlvn}}\left({\mathbf{nwl}}-{\mathbf{ilev}}+1\right)$ as returned by a call to nag_wav_2d_multi_fwd (c09ec) transforming nwl levels.

### Compulsory Input Parameters

1:     $\mathrm{ilev}$int64int32nag_int scalar
The level at which coefficients are to be extracted.
Constraints:
• $1\le {\mathbf{ilev}}\le {\mathbf{nwl}}$, where nwl is as used in a preceding call to nag_wav_2d_multi_fwd (c09ec);
• if ${\mathbf{cindex}}=0$, ${\mathbf{ilev}}={\mathbf{nwl}}$.
2:     $\mathrm{cindex}$int64int32nag_int scalar
Identifies which coefficients to extract. The coefficients are identified as follows:
${\mathbf{cindex}}=0$
The approximation coefficients, produced by application of the low pass filter over columns and rows of the original matrix ($\mathrm{LL}$). The approximation coefficients are available only for ${\mathbf{ilev}}={\mathbf{nwl}}$, where nwl is the value used in a preceding call to nag_wav_2d_multi_fwd (c09ec).
${\mathbf{cindex}}=1$
The vertical detail coefficients produced by applying the low pass filter over columns of the original matrix and the high pass filter over rows ($\mathrm{LH}$).
${\mathbf{cindex}}=2$
The horizontal detail coefficients produced by applying the high pass filter over columns of the original matrix and the low pass filter over rows ($\mathrm{HL}$).
${\mathbf{cindex}}=3$
The diagonal detail coefficients produced by applying the high pass filter over columns and rows of the original matrix ($\mathrm{HH}$).
Constraint: $0\le {\mathbf{cindex}}\le 3$ when ${\mathbf{ilev}}={\mathbf{nwl}}$ as used in nag_wav_2d_multi_fwd (c09ec), otherwise $1\le {\mathbf{cindex}}\le 3$.
3:     $\mathrm{c}\left({\mathbf{lenc}}\right)$ – double array
DWT coefficients, as computed by a preceding call to nag_wav_2d_multi_fwd (c09ec).
4:     $\mathrm{icomm}\left(180\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_2d_init (c09ab).

### Optional Input Parameters

1:     $\mathrm{lenc}$int64int32nag_int scalar
Default: the dimension of the array c.
The dimension of the array c.
Constraint: lenc must be unchanged from the value used in the preceding call to nag_wav_2d_multi_fwd (c09ec)..

### Output Parameters

1:     $\mathrm{d}\left(\mathit{ldd},:\right)$ – double array
The first dimension of the array d will be ${n}_{\mathrm{cm}}$.
The second dimension of the array d will be $\mathit{ncn}$.
The requested coefficients.
If ${\mathbf{ilev}}={\mathbf{nwl}}$ (as used in nag_wav_2d_multi_fwd (c09ec)) and ${\mathbf{cindex}}=0$, the ${n}_{\mathit{cm}}$ by ${n}_{\mathit{cn}}$ approximation coefficients ${a}_{ij}$ are stored in ${\mathbf{d}}\left(i,\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{\mathit{cm}}$ and $\mathit{j}=1,2,\dots ,{n}_{\mathit{cn}}$.
Otherwise the ${n}_{\mathit{cm}}$ by ${n}_{\mathit{cn}}$ level ilev detail coefficients (of type specified by cindex) ${d}_{ij}$ are stored in ${\mathbf{d}}\left(i,\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{\mathit{cm}}$ and $\mathit{j}=1,2,\dots ,{n}_{\mathit{cn}}$.
2:     $\mathrm{icomm}\left(180\right)$int64int32nag_int array
Communication array, used to store information between calls to nag_wav_2d_coeff_ext (c09ey).
3:     $\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$
Constraint: ${\mathbf{ilev}}\le {\mathbf{nwl}}$, where nwl is the number of levels used in the call to nag_wav_2d_multi_fwd (c09ec).
Constraint: ${\mathbf{ilev}}\ge 1$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{cindex}}\le 3$.
Constraint: ${\mathbf{cindex}}\ge 0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{lenc}}\ge {n}_{\mathrm{ct}}$, where ${n}_{\mathrm{ct}}$ is the number of DWT coefficients computed in a previous call to nag_wav_2d_multi_fwd (c09ec).
${\mathbf{ifail}}=4$
Constraint: $\mathit{ldd}\ge {n}_{\mathrm{cm}}$, where ${n}_{\mathrm{cm}}$ is the number of DWT coefficients in the first dimension at the selected level ilev.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{cindex}}>0$ when ${\mathbf{ilev}}<{\mathbf{nwl}}$ in the preceding call to nag_wav_2d_multi_fwd (c09ec).
${\mathbf{ifail}}=6$
Either the initialization function has not been called first or icomm has been corrupted.
Either the initialization function was called with ${\mathbf{wtrans}}=\text{'S'}$ or icomm has been 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.

Not applicable.

None.

## Example

```function c09ez_example

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

% 2D Data
m = int64(7);
n = int64(6);
a = zeros(m,n);
a(1:2:m,:) = 0.01;
a(2:2:m,:) = 1;

genid = int64(3);
subid = int64(0);
seed(1) = int64(642521);
[state, ifail] = g05kf(genid, subid, seed);
[state, x, ifail] = g05sk(m*n, 0, 1.0e-4, state);
an = a + reshape(x,[m,n]);

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

% Wavelet setup
wavnam = 'DB6';
mode = 'Period';
wtrans = 'Multilevel';
[nwl, nf, nwct, nwcn, icomm, ifail] = ...
c09ab(...
wavnam, wtrans, mode, m, n);

% Multi-level wavelet transform on noisy data
lenc = nwct;
% Perform Discrete Wavelet transform
[c, dwtlvm, dwtlvn, icomm, ifail] = ...
c09ec(...
an, lenc, nwl, icomm);

% Reconstruct without thresholding of detail coefficients
[b, ifail] = c09ed(nwl, c, m, n, icomm);

% Mean square error of noisy reconstruction
mse = (norm(reshape(a-b,[m*n,1]))^2)/(double(m*n));
fprintf('Without denoising Mean Square Error is %9.6f\n',mse);

% De-noise by applying hard threshold to detail coefficients
thresh = 0.01*sqrt(2*log(double(m*n)));
nt = 0;
nnt = 0;
for ilev = 1:nwl
level = int64(nwl - ilev + 1);

for detail = int64(1:3)
% Extract the selected set of coefficients.
[d, icomm, ifail] = c09ey(...
level, detail, c, icomm);

% Threshold
d1 = dwtlvm(ilev);
d2 = dwtlvn(ilev);
for i = 1:d1
for j = 1:d2
if abs(d(i,j))<thresh
d(i,j) = 0;
nt = nt + 1;
end
nnt = nnt + 1;
end
end
% Insert de-noised coefficients back into c
[c, icomm, ifail] = c09ez(...
level, detail, c, d, icomm);
end
end

fprintf('\nNumber of coefficients denoised is %3d out of %3d\n',nt,nnt);

% Reconstruct data after threholding
[b, ifail] = c09ed(nwl, c, m, n, icomm);

% Mean square error of de-noised reconstruction
mse = (norm(reshape(a-b,[m*n,1]))^2)/(double(m*n));
fprintf('With denoising Mean Square Error is %9.6f\n\n',mse);

disp('Reconstruction of denoised input: ');
disp(b);

```
```c09ey example results

Input data a:
0.0100    0.0100    0.0100    0.0100    0.0100    0.0100
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
0.0100    0.0100    0.0100    0.0100    0.0100    0.0100
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
0.0100    0.0100    0.0100    0.0100    0.0100    0.0100
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
0.0100    0.0100    0.0100    0.0100    0.0100    0.0100

Noisy data an:
0.0135    0.0170   -0.0049   -0.0009    0.0002    0.0123
1.0015    0.9896    0.9983    1.0044    1.0097    0.9847
-0.0017    0.0107    0.0194   -0.0084    0.0114   -0.0006
0.9899    1.0038    1.0005    0.9921    0.9923    0.9982
-0.0093    0.0149    0.0094    0.0160    0.0058    0.0257
0.9842    1.0278    0.9991    0.9956    1.0113    0.9911
0.0139   -0.0011    0.0180    0.0187    0.0106    0.0118

Without denoising Mean Square Error is  0.000098

Number of coefficients denoised is  32 out of  48
With denoising Mean Square Error is  0.000018

Reconstruction of denoised input:
0.0127    0.0094    0.0030    0.0007    0.0009    0.0065
0.9913    0.9940    1.0000    1.0027    1.0032    0.9976
0.0084    0.0086    0.0072    0.0048    0.0028    0.0050
1.0009    0.9998    0.9966    0.9942    0.9930    0.9965
0.0061    0.0070    0.0103    0.0134    0.0154    0.0114
1.0034    1.0036    1.0028    1.0011    0.9996    1.0011
0.0135    0.0113    0.0093    0.0114    0.0147    0.0148

```