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_3d_multi_fwd (c09fc)

## Purpose

nag_wav_3d_multi_fwd (c09fc) computes the three-dimensional multi-level discrete wavelet transform (DWT). The initialization function nag_wav_3d_init (c09ac) must be called first to set up the DWT options.

## Syntax

[c, dwtlvm, dwtlvn, dwtlvfr, icomm, ifail] = c09fc(n, fr, a, lenc, nwl, icomm, 'm', m)
[c, dwtlvm, dwtlvn, dwtlvfr, icomm, ifail] = nag_wav_3d_multi_fwd(n, fr, a, lenc, nwl, icomm, 'm', m)

## Description

nag_wav_3d_multi_fwd (c09fc) computes the multi-level DWT of three-dimensional data. For a given wavelet and end extension method, nag_wav_3d_multi_fwd (c09fc) will compute a multi-level transform of a three-dimensional array 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_3d_init (c09ac) 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 seven sets of detail coefficients are stored at this level while the approximation 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 eight types of coefficients are stored. All coefficients are packed into a single array.

None.

## Parameters

### Compulsory Input Parameters

1:     n – int64int32nag_int scalar
The second dimension of the input data: the number of columns of each two-dimensional frame.
Constraint: this must be the same as the value n passed to the initialization function nag_wav_3d_init (c09ac).
2:     fr – int64int32nag_int scalar
The third dimension of the input data: the number of two-dimensional frames.
Constraint: this must be the same as the value fr passed to the initialization function nag_wav_3d_init (c09ac).
3:     a(lda,sda,fr) – 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$ by fr$\mathit{fr}$ input three-dimensional array A$A$.
4:     lenc – int64int32nag_int scalar
The dimension of the array c as declared in the (sub)program from which nag_wav_3d_multi_fwd (c09fc) is called.
Constraint: lencnct${\mathbf{lenc}}\ge {n}_{\mathrm{ct}}$, where nct${n}_{\mathrm{ct}}$ is the total number of wavelet coefficients that correspond to a transform with nwl levels.
5:     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_3d_init (c09ac).
6:     icomm(260$260$) – 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_3d_init (c09ac).

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The first dimension of the array a.
The first dimension of the input data: the number of rows of each two-dimensional frame.
Constraint: this must be the same as the value m passed to the initialization function nag_wav_3d_init (c09ac).

lda sda

### Output Parameters

1:     c(lenc) – double array
The coefficients of the discrete wavelet transform.
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(nli + 1) × dwtlvn(nli + 1) × dwtlvfr(nli + 1) $q\left(i\right)={\mathbf{dwtlvm}}\left({n}_{l}-i+1\right)×{\mathbf{dwtlvn}}\left({n}_{l}-i+1\right)×{\mathbf{dwtlvfr}}\left({n}_{l}-i+1\right)$. Then, letting k1 = q(nl)${k}_{1}=q\left({n}_{l}\right)$ and kj + 1 = kj + q(nlj / 7 + 1)${k}_{\mathit{j}+1}={k}_{\mathit{j}}+q\left({n}_{l}-⌈\mathit{j}/7⌉+1\right)$, for j = 1,2,,7nl$\mathit{j}=1,2,\dots ,7{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}}$. Note that for computational efficiency reasons these coefficients are stored as dwtlvm(1) × dwtlvn(1) × dwtlvfr(1) ${\mathbf{dwtlvm}}\left(1\right)×{\mathbf{dwtlvn}}\left(1\right)×{\mathbf{dwtlvfr}}\left(1\right)$ in c.
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 / 7 + 1${n}_{l}-⌈j/7⌉+1$ detail coefficients. These are:
• LLH coefficients if j  mod  7 = 1;
• LHL coefficients if j  mod  7 = 2;
• LHH coefficients if j  mod  7 = 3;
• HLL coefficients if j  mod  7 = 4;
• HLH coefficients if j  mod  7 = 5;
• HHL coefficients if j  mod  7 = 6;
• HHH coefficients if j  mod  7 = 0,
for j = 1,,7nl$j=1,\dots ,7{n}_{l}$.
Note that for computational efficiency reasons these coefficients are stored as dwtlvfr (j / 7) × dwtlvm (j / 7) × dwtlvn (j / 7) ${\mathbf{dwtlvfr}}\left(⌈j/7⌉\right)×{\mathbf{dwtlvm}}\left(⌈j/7⌉\right)×{\mathbf{dwtlvn}}\left(⌈j/7⌉\right)$ in c.
See Section [Example] for details of how to access each set of coefficients in order to perform extraction from C following a call to this function, or insertion into c before a call to the three-dimensional multi-level inverse function nag_wav_3d_mxolap_multi_inv (c09fd).
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}$.
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}$.
4:     dwtlvfr(nwl) – int64int32nag_int array
The number of coefficients in the third dimension for each coefficient type at each level. dwtlvfr(i)${\mathbf{dwtlvfr}}\left(\mathit{i}\right)$ contains the number of coefficients in the third 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}$.
5:     icomm(260$260$) – int64int32nag_int array
Contains additional information on the computed transform.
6:     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: fr = fr${\mathbf{fr}}=\mathrm{fr}$, the value of fr on initialization (see nag_wav_3d_init (c09ac)).
Constraint: m = m${\mathbf{m}}=m$, the value of m on initialization (see nag_wav_3d_init (c09ac)).
Constraint: n = n${\mathbf{n}}=n$, the value of n on initialization (see nag_wav_3d_init (c09ac)).
ifail = 2${\mathbf{ifail}}=2$
Constraint: ldam$\mathit{lda}\ge {\mathbf{m}}$.
Constraint: sdan$\mathit{sda}\ge {\mathbf{n}}$.
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_3d_init (c09ac).
Constraint: nwl1${\mathbf{nwl}}\ge 1$.
ifail = 6${\mathbf{ifail}}=6$
Either the initialization function has not been called first or the communication array icomm has been corrupted.
The initialization function was called with wtrans = 'S'${\mathbf{wtrans}}=\text{'S'}$.
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 example program shows how the wavelet coefficients at each level can be extracted from the output array c. Denoising can be carried out by applying a thresholding operation to the detail coefficients at every level. If cij${c}_{ij}$ is a detail coefficient then 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_3d_mxolap_multi_inv (c09fd) 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_3d_multi_fwd_example
m  = int64(7);
n  = int64(6);
fr = int64(5);
wavnam = 'Bior1.1';
mode = 'period';
wtrans = 'Multilevel';
a = zeros(m, n, fr);
a(:, :, 1) = [3, 2, 2, 2, 1, 1;
2, 9, 1, 2, 1, 3;
2, 5, 1, 2, 1, 1;
1, 6, 2, 2, 7, 2;
5, 3, 2, 2, 4, 7;
2, 2, 1, 1, 2, 1;
6, 2, 1, 3, 6, 9];
a(:, :, 2) = [2, 1, 5, 1, 2, 3;
2, 9, 5, 2, 1, 2;
2, 3, 2, 7, 1, 1;
2, 1, 1, 2, 3, 1;
2, 1, 2, 8, 3, 3;
1, 4, 5, 1, 2, 7;
8, 1, 3, 9, 1, 2];
a(:, :, 3) = [3, 1, 4, 1, 1, 1;
1, 1, 2, 1, 2, 6;
4, 1, 7, 2, 5, 6;
3, 2, 1, 5, 9, 5;
1, 1, 2, 2, 2, 1;
2, 6, 3, 9, 5, 1;
1, 1, 8, 2, 1, 3];
a(:, :, 4) = [5, 8, 1, 2, 2, 1;
1, 2, 2, 9, 2, 9;
2, 2, 2, 1, 1, 3;
1, 1, 1, 5, 1, 2;
3, 2, 8, 1, 9, 2;
2, 1, 9, 1, 2, 2;
3, 6, 5, 3, 2, 2];
a(:, :, 5) = [5, 2, 1, 2, 1, 1;
3, 1, 9, 1, 2, 1;
2, 3, 1, 1, 7, 2;
7, 2, 2, 6, 1, 1;
5, 1, 7, 2, 1, 1;
2, 1, 3, 2, 2, 1;
5, 3, 9, 1, 4, 1];

% Query wavelet filter dimensions
[nwl, nf, nwct, nwcn, nwcfr, icomm, ifail] = ...
nag_wav_3d_init(wavnam, wtrans, mode, m, n, fr);

% Perform Discrete Wavelet transform
[c, dwtlvm, dwtlvn, dwtlvfr, icomm, ifail] = ...
nag_wav_3d_multi_fwd(n, fr, a, nwct, nwl, icomm);

fprintf(' Number of Levels : %d\n\n', nwl);
fprintf(' Number of coefficients in 1st dimension for each level:\n');
fprintf(' %8d', dwtlvm(1:nwl));
fprintf('\n');
fprintf(' Number of coefficients in 2nd dimension for each level:\n');
fprintf(' %8d', dwtlvn(1:nwl));
fprintf('\n');
fprintf(' Number of coefficients in 3rd dimension for each level:\n');
fprintf(' %8d', dwtlvfr(1:nwl));
fprintf('\n');

% Print the first level HLL coefficients
want_level = 1;

% Select the approximation coefficients.
want_coeffs = 4;

% Identify each set of coefficients in c
for ilevel = nwl:-1:1

if ilevel ~= want_level
continue
end

nwcm = dwtlvm(nwl-ilevel+1);
nwcn = dwtlvn(nwl-ilevel+1);
nwcfr = dwtlvfr(nwl-ilevel+1);

fprintf('\n--------------------------------\n');
fprintf(' Level %d output is %d by %d by %d.\n', ilevel, nwcm, nwcn, nwcfr);
fprintf('--------------------------------\n\n');

for itype_coeffs = 0:7

if itype_coeffs ~= want_coeffs
continue
end

% Unless we're looking at the deepest level of nesting, which contains
% approximation coefficients, advance the pointer on past the preceding
% levels
if ilevel == nwl
locc = 0;
else
locc = 8*dwtlvm(1)*dwtlvn(1)*dwtlvfr(1);
for i = ilevel + 1 : nwl - 1
locc = locc + 7*dwtlvm(nwl-i+1)*dwtlvn(nwl-i+1)*dwtlvfr(nwl-i+1);
end
end

% Now decide which coefficient type we are considering
switch (itype_coeffs)
case {0}
if (ilevel==nwl)
fprintf('Approximation coefficients (LLL)\n');
locc = locc + 1;
end
case {1}
fprintf('Detail coefficients (LLH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients
locc = locc + nwcm*nwcn*nwcfr + 1;
else
locc = locc + 1;
end
case {2}
fprintf('Detail coefficients (LHL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 1 set of
% detail coefficients
locc = locc + 2*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 1 set of detail coefficients
locc = locc + nwcm*nwcn*nwcfr + 1;
end
case {3}
fprintf('Detail coefficients (LHH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 2 sets of
% detail coefficients
locc = locc + 3*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 2 sets of detail coefficients
locc = locc + 2*nwcm*nwcn*nwcfr + 1;
end
case {4}
fprintf('Detail coefficients (HLL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 3 sets of
% detail coefficients
locc = locc + 4*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 3 sets of detail coefficients
locc = locc + 3*nwcm*nwcn*nwcfr + 1;
end
case {5}
fprintf('Detail coefficients (HLH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 4 sets of
% detail coefficients
locc = locc + 5*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 4 sets of detail coefficients
locc = locc + 4*nwcm*nwcn*nwcfr + 1;
end
case {6}
fprintf('Detail coefficients (HHL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 5 sets of
% detail coefficients
locc = locc + 6*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 4 sets of detail coefficients
locc = locc + 5*nwcm*nwcn*nwcfr + 1;
end
case {7}
fprintf('Detail coefficients (HHH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 6 sets of
% detail coefficients
locc = locc + 7*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 5 sets of detail coefficients
locc = locc + 6*nwcm*nwcn*nwcfr + 1;
end
end

if itype_coeffs > 0 || ilevel == nwl

if (itype_coeffs==0)
% For a multi level transform approx coeffs stored as
% nwcm x nwcn x nwcfr
i1 = locc;
for k = 1:nwcfr
for j = 1:nwcn
for i = 1:nwcm
d(i,j,k) = c(i1);
i1 = i1 + 1;
end
end
end
else
% ... but detail coefficients are stored as ncwfr x nwcm x nwcn
for k = 1:nwcfr
for j = 1:nwcn
for i = 1:nwcm
i1 = locc - 1 + (j-1)*nwcfr*nwcm + (i-1)*nwcfr + k;
d(i,j,k) = c(i1);
end
end
end
end

% Print out the selected set of coefficients
fprintf('Level %d, Coefficients %d:\n', ilevel, itype_coeffs);
for k = 1:nwcfr
fprintf('Frame %d:\n', k);
for i = 1:nwcm
for j=1:nwcn
fprintf('%8.4f ', d(i, j, k));
end
fprintf('\n');
end
end

end

end
end

% Reconstruct original data
[b, ifail] = nag_wav_3d_mxolap_multi_inv(nwl, c, m, n, fr, icomm);

% Check reconstruction matches original
eps = 10*double(m*n*fr)*nag_machine_precision;
err = a-b;
frob = 0;
for i=1:fr
fnew = sqrt(sum(sum(err(:,:,i).^2)));
frob = max(frob,fnew);
end

if frob < eps
fprintf('\nSuccess: the reconstruction matches the original.\n');
else
fprintf('\nFail: Frobenius norm of b-a is too large.\n');
end
```
```
Number of Levels : 2

Number of coefficients in 1st dimension for each level:
2        4
Number of coefficients in 2nd dimension for each level:
2        3
Number of coefficients in 3rd dimension for each level:
2        3

--------------------------------
Level 1 output is 4 by 3 by 3.
--------------------------------

Detail coefficients (HLL)
Level 1, Coefficients 4:
Frame 1:
-4.9497   0.0000   0.0000
0.7071   1.7678  -3.1820
0.7071   2.1213   1.7678
0.0000   0.0000   0.0000
Frame 2:
4.2426  -2.1213  -4.9497
0.7071  -0.0000  -0.7071
-1.4142  -3.1820   1.4142
0.0000   0.0000   0.0000
Frame 3:
2.1213  -4.9497  -0.7071
-2.8284  -4.2426   4.9497
2.1213   2.8284  -0.7071
0.0000   0.0000   0.0000

Success: the reconstruction matches the original.

```
```function c09fc_example
m  = int64(7);
n  = int64(6);
fr = int64(5);
wavnam = 'Bior1.1';
mode = 'period';
wtrans = 'Multilevel';
a = zeros(m, n, fr);
a(:, :, 1) = [3, 2, 2, 2, 1, 1;
2, 9, 1, 2, 1, 3;
2, 5, 1, 2, 1, 1;
1, 6, 2, 2, 7, 2;
5, 3, 2, 2, 4, 7;
2, 2, 1, 1, 2, 1;
6, 2, 1, 3, 6, 9];
a(:, :, 2) = [2, 1, 5, 1, 2, 3;
2, 9, 5, 2, 1, 2;
2, 3, 2, 7, 1, 1;
2, 1, 1, 2, 3, 1;
2, 1, 2, 8, 3, 3;
1, 4, 5, 1, 2, 7;
8, 1, 3, 9, 1, 2];
a(:, :, 3) = [3, 1, 4, 1, 1, 1;
1, 1, 2, 1, 2, 6;
4, 1, 7, 2, 5, 6;
3, 2, 1, 5, 9, 5;
1, 1, 2, 2, 2, 1;
2, 6, 3, 9, 5, 1;
1, 1, 8, 2, 1, 3];
a(:, :, 4) = [5, 8, 1, 2, 2, 1;
1, 2, 2, 9, 2, 9;
2, 2, 2, 1, 1, 3;
1, 1, 1, 5, 1, 2;
3, 2, 8, 1, 9, 2;
2, 1, 9, 1, 2, 2;
3, 6, 5, 3, 2, 2];
a(:, :, 5) = [5, 2, 1, 2, 1, 1;
3, 1, 9, 1, 2, 1;
2, 3, 1, 1, 7, 2;
7, 2, 2, 6, 1, 1;
5, 1, 7, 2, 1, 1;
2, 1, 3, 2, 2, 1;
5, 3, 9, 1, 4, 1];

% Query wavelet filter dimensions
[nwl, nf, nwct, nwcn, nwcfr, icomm, ifail] = ...
c09ac(wavnam, wtrans, mode, m, n, fr);

% Perform Discrete Wavelet transform
[c, dwtlvm, dwtlvn, dwtlvfr, icomm, ifail] = c09fc(n, fr, a, nwct, nwl, icomm);

fprintf(' Number of Levels : %d\n\n', nwl);
fprintf(' Number of coefficients in 1st dimension for each level:\n');
fprintf(' %8d', dwtlvm(1:nwl));
fprintf('\n');
fprintf(' Number of coefficients in 2nd dimension for each level:\n');
fprintf(' %8d', dwtlvn(1:nwl));
fprintf('\n');
fprintf(' Number of coefficients in 3rd dimension for each level:\n');
fprintf(' %8d', dwtlvfr(1:nwl));
fprintf('\n');

% Print the first level HLL coefficients
want_level = 1;

% Select the approximation coefficients.
want_coeffs = 4;

% Identify each set of coefficients in c
for ilevel = nwl:-1:1

if ilevel ~= want_level
continue
end

nwcm = dwtlvm(nwl-ilevel+1);
nwcn = dwtlvn(nwl-ilevel+1);
nwcfr = dwtlvfr(nwl-ilevel+1);

fprintf('\n--------------------------------\n');
fprintf(' Level %d output is %d by %d by %d.\n', ilevel, nwcm, nwcn, nwcfr);
fprintf('--------------------------------\n\n');

for itype_coeffs = 0:7

if itype_coeffs ~= want_coeffs
continue
end

% Unless we're looking at the deepest level of nesting, which contains
% approximation coefficients, advance the pointer on past the preceding
% levels
if ilevel == nwl
locc = 0;
else
locc = 8*dwtlvm(1)*dwtlvn(1)*dwtlvfr(1);
for i = ilevel + 1 : nwl - 1
locc = locc + 7*dwtlvm(nwl-i+1)*dwtlvn(nwl-i+1)*dwtlvfr(nwl-i+1);
end
end

% Now decide which coefficient type we are considering
switch (itype_coeffs)
case {0}
if (ilevel==nwl)
fprintf('Approximation coefficients (LLL)\n');
locc = locc + 1;
end
case {1}
fprintf('Detail coefficients (LLH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients
locc = locc + nwcm*nwcn*nwcfr + 1;
else
locc = locc + 1;
end
case {2}
fprintf('Detail coefficients (LHL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 1 set of
% detail coefficients
locc = locc + 2*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 1 set of detail coefficients
locc = locc + nwcm*nwcn*nwcfr + 1;
end
case {3}
fprintf('Detail coefficients (LHH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 2 sets of
% detail coefficients
locc = locc + 3*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 2 sets of detail coefficients
locc = locc + 2*nwcm*nwcn*nwcfr + 1;
end
case {4}
fprintf('Detail coefficients (HLL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 3 sets of
% detail coefficients
locc = locc + 4*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 3 sets of detail coefficients
locc = locc + 3*nwcm*nwcn*nwcfr + 1;
end
case {5}
fprintf('Detail coefficients (HLH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 4 sets of
% detail coefficients
locc = locc + 5*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 4 sets of detail coefficients
locc = locc + 4*nwcm*nwcn*nwcfr + 1;
end
case {6}
fprintf('Detail coefficients (HHL)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 5 sets of
% detail coefficients
locc = locc + 6*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 4 sets of detail coefficients
locc = locc + 5*nwcm*nwcn*nwcfr + 1;
end
case {7}
fprintf('Detail coefficients (HHH)\n');
if (ilevel==nwl)
% Advance pointer past approximation coefficients and 6 sets of
% detail coefficients
locc = locc + 7*nwcm*nwcn*nwcfr + 1;
else
% Advance pointer past 5 sets of detail coefficients
locc = locc + 6*nwcm*nwcn*nwcfr + 1;
end
end

if itype_coeffs > 0 || ilevel == nwl

if (itype_coeffs==0)
% For a multi level transform approx coeffs stored as
% nwcm x nwcn x nwcfr
i1 = locc;
for k = 1:nwcfr
for j = 1:nwcn
for i = 1:nwcm
d(i,j,k) = c(i1);
i1 = i1 + 1;
end
end
end
else
% ... but detail coefficients are stored as ncwfr x nwcm x nwcn
for k = 1:nwcfr
for j = 1:nwcn
for i = 1:nwcm
i1 = locc - 1 + (j-1)*nwcfr*nwcm + (i-1)*nwcfr + k;
d(i,j,k) = c(i1);
end
end
end
end

% Print out the selected set of coefficients
fprintf('Level %d, Coefficients %d:\n', ilevel, itype_coeffs);
for k = 1:nwcfr
fprintf('Frame %d:\n', k);
for i = 1:nwcm
for j=1:nwcn
fprintf('%8.4f ', d(i, j, k));
end
fprintf('\n');
end
end

end

end
end

% Reconstruct original data
[b, ifail] = c09fd(nwl, c, m, n, fr, icomm);

% Check reconstruction matches original
eps = 10*double(m*n*fr)*x02aj;
err = a-b;
frob = 0;
for i=1:fr
fnew = sqrt(sum(sum(err(:,:,i).^2)));
frob = max(frob,fnew);
end

if frob < eps
fprintf('\nSuccess: the reconstruction matches the original.\n');
else
fprintf('\nFail: Frobenius norm of b-a is too large.\n');
end
```
```
Number of Levels : 2

Number of coefficients in 1st dimension for each level:
2        4
Number of coefficients in 2nd dimension for each level:
2        3
Number of coefficients in 3rd dimension for each level:
2        3

--------------------------------
Level 1 output is 4 by 3 by 3.
--------------------------------

Detail coefficients (HLL)
Level 1, Coefficients 4:
Frame 1:
-4.9497   0.0000   0.0000
0.7071   1.7678  -3.1820
0.7071   2.1213   1.7678
0.0000   0.0000   0.0000
Frame 2:
4.2426  -2.1213  -4.9497
0.7071  -0.0000  -0.7071
-1.4142  -3.1820   1.4142
0.0000   0.0000   0.0000
Frame 3:
2.1213  -4.9497  -0.7071
-2.8284  -4.2426   4.9497
2.1213   2.8284  -0.7071
0.0000   0.0000   0.0000

Success: the reconstruction matches the original.

```