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_ins (c09ez)

## Purpose

nag_wav_2d_coeff_ins (c09ez) inserts a selected set of two-dimensional discrete wavelet transform (DWT) coefficients into the full set of coefficients stored in compact form, which may be later used as input to the multi-level reconstruction function nag_wav_2d_multi_inv (c09ed).

## Syntax

[c, icomm, ifail] = c09ez(ilev, cindex, c, d, icomm, 'lenc', lenc)
[c, icomm, ifail] = nag_wav_2d_coeff_ins(ilev, cindex, c, d, icomm, 'lenc', lenc)

## Description

nag_wav_2d_coeff_ins (c09ez) inserts a selected set of two-dimensional DWT coefficients into the full set of coefficients stored in compact form in a one-dimensional array c. It is required that nag_wav_2d_coeff_ins (c09ez) is preceded by a call to the initialization function nag_wav_2d_init (c09ab) and the forward multi-level transform function nag_wav_2d_multi_fwd (c09ec).
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 two-dimensional array, d. Following some calculation on this set of coefficients (for example, denoising), the updated coefficients in d are inserted back into the full set c using nag_wav_2d_coeff_ins (c09ez). Several extractions and insertions may be performed at different levels. nag_wav_2d_multi_inv (c09ed) can then be used to reconstruct a manipulated data set $\stackrel{~}{A}$. 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 multi-level 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 inserted.
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 insert. 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 present 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
Contains the DWT coefficients inserted by previous calls to nag_wav_2d_coeff_ins (c09ez), or computed by a previous call to nag_wav_2d_multi_fwd (c09ec).
4:     $\mathrm{d}\left(\mathit{ldd},:\right)$ – double array
The first dimension of the array d must be at least ${n}_{\mathrm{cm}}$.
The second dimension of the array d must be at least $\mathit{ncn}$.
The coefficients to be inserted.
If ${\mathbf{ilev}}={\mathbf{nwl}}$ (as used in nag_wav_2d_multi_fwd (c09ec)) and ${\mathbf{cindex}}=0$, the ${n}_{\mathrm{cm}}$ by ${n}_{\mathrm{cn}}$ manipulated approximation coefficients ${a}_{\mathit{i}\mathit{j}}$ must be stored in ${\mathbf{d}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{cm}}$ and $\mathit{i}=1,2,\dots ,{n}_{\mathrm{cn}}$.
Otherwise the ${n}_{\mathrm{cm}}$ by ${n}_{\mathrm{cn}}$ manipulated level ilev detail coefficients (of type specified by cindex) ${d}_{\mathit{i}\mathit{j}}$ must be stored in ${\mathbf{d}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{cm}}$ and $\mathit{j}=1,2,\dots ,{n}_{\mathrm{cn}}$.
5:     $\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{c}\left({\mathbf{lenc}}\right)$ – double array
Contains the same DWT coefficients provided on entry except for those identified by ilev and cindex, which are updated with the values supplied in d, inserted into the correct locations as expected by the reconstruction function nag_wav_2d_multi_inv (c09ed).
2:     $\mathrm{icomm}\left(180\right)$int64int32nag_int array
Communication array, used to store information between calls to nag_wav_2d_coeff_ins (c09ez).
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 ${\mathbf{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

The following example demonstrates using the coefficient extraction and insertion functions in order to apply denoising using a thresholding operation. The original input data, which is horizontally striped, has artificial noise introduced to it, taken from a normal random number distribution. Reconstruction then takes place on both the noisy data and denoised data. The Mean Square Errors (MSE) of the two reconstructions are printed along with the reconstruction of the denoised data. The MSE of the denoised reconstruction is less than that of the noisy reconstruction.
```function c09ez_example

fprintf('c09ez 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);

```
```c09ez 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

```