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$, using a specified number, ${n}_{\mathrm{fwd}}$, of levels. The number of levels specified, ${n}_{\mathrm{fwd}}$, must be no more than the value ${l}_{\mathrm{max}}$ returned in nwlmax 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$ to the input data, $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 ${n}_{\mathrm{fwd}}$, all eight types of coefficients are stored. All coefficients are packed into a single array.

## References

Wang Y, Che X and Ma S (2012) Nonlinear filtering based on 3D wavelet transform for MRI denoising URASIP Journal on Advances in Signal Processing 2012:40

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
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:     $\mathrm{fr}$int64int32nag_int scalar
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:     $\mathrm{a}\left(\mathit{lda},\mathit{sda},{\mathbf{fr}}\right)$ – double array
lda, the first dimension of the array, must satisfy the constraint $\mathit{lda}\ge {\mathbf{m}}$.
The $m$ by $n$ by $\mathit{fr}$ three-dimensional input data $A$, where with ${A}_{ijk}$ stored in ${\mathbf{a}}\left(i,j,k\right)$.
4:     $\mathrm{lenc}$int64int32nag_int scalar
The dimension of the array c.
Constraint: ${\mathbf{lenc}}\ge {n}_{\mathrm{ct}}$, where ${n}_{\mathrm{ct}}$ is the total number of wavelet coefficients that correspond to a transform with nwl levels.
5:     $\mathrm{nwl}$int64int32nag_int scalar
The number of levels, ${n}_{\mathrm{fwd}}$, in the multi-level resolution to be performed.
Constraint: $1\le {\mathbf{nwl}}\le {l}_{\mathrm{max}}$, where ${l}_{\mathrm{max}}$ is the value returned in nwlmax (the maximum number of levels) by the call to the initialization function nag_wav_3d_init (c09ac).
6:     $\mathrm{icomm}\left(260\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_3d_init (c09ac).

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the first dimension of the array a.
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).

### Output Parameters

1:     $\mathrm{c}\left({\mathbf{lenc}}\right)$ – double array
The coefficients of the discrete wavelet transform. If you need to access or modify the approximation coefficients or any specific set of detail coefficients then the use of nag_wav_3d_coeff_ext (c09fy) or nag_wav_3d_coeff_ins (c09fz) is recommended. For completeness the following description provides details of precisely how the coefficients are stored in c but this information should only be required in rare cases.
Let $q\left(\mathit{i}\right)$ denote the number of coefficients of each type at level $\mathit{i}$, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{fwd}}$, such that $q\left(i\right)={\mathbf{dwtlvm}}\left({n}_{\mathrm{fwd}}-i+1\right)×{\mathbf{dwtlvn}}\left({n}_{\mathrm{fwd}}-i+1\right)×{\mathbf{dwtlvfr}}\left({n}_{\mathrm{fwd}}-i+1\right)$. Then, letting ${k}_{1}=q\left({n}_{\mathrm{fwd}}\right)$ and ${k}_{\mathit{j}+1}={k}_{\mathit{j}}+q\left({n}_{\mathrm{fwd}}-⌈\mathit{j}/7⌉+1\right)$, for $\mathit{j}=1,2,\dots ,7{n}_{\mathrm{fwd}}$, the coefficients are stored in c as follows:
${\mathbf{c}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{k}_{1}$
Contains the level ${n}_{\mathrm{fwd}}$ approximation coefficients, ${a}_{{n}_{\mathrm{fwd}}}$. Note that for computational efficiency reasons these coefficients are stored as ${\mathbf{dwtlvm}}\left(1\right)×{\mathbf{dwtlvn}}\left(1\right)×{\mathbf{dwtlvfr}}\left(1\right)$ in c.
${\mathbf{c}}\left(\mathit{i}\right)$, for $\mathit{i}={k}_{j}+1,\dots ,{k}_{j+1}$
Contains the level ${n}_{\mathrm{fwd}}-⌈j/7⌉+1$ detail coefficients. These are:
• LLH coefficients if ;
• LHL coefficients if ;
• LHH coefficients if ;
• HLL coefficients if ;
• HLH coefficients if ;
• HHL coefficients if ;
• HHH coefficients if ,
for $j=1,\dots ,7{n}_{\mathrm{fwd}}$. See Multiresolution and higher dimensional DWT in the C09 Chapter Introduction for a description of how these coefficients are produced.
Note that for computational efficiency reasons these coefficients are stored as ${\mathbf{dwtlvfr}}\left(⌈j/7⌉\right)×{\mathbf{dwtlvm}}\left(⌈j/7⌉\right)×{\mathbf{dwtlvn}}\left(⌈j/7⌉\right)$ in c.
2:     $\mathrm{dwtlvm}\left({\mathbf{nwl}}\right)$int64int32nag_int array
The number of coefficients in the first dimension for each coefficient type at each level. ${\mathbf{dwtlvm}}\left(\mathit{i}\right)$ contains the number of coefficients in the first dimension (for each coefficient type computed) at the (${n}_{\mathrm{fwd}}-\mathit{i}+1$)th level of resolution, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{fwd}}$.
3:     $\mathrm{dwtlvn}\left({\mathbf{nwl}}\right)$int64int32nag_int array
The number of coefficients in the second dimension for each coefficient type at each level. ${\mathbf{dwtlvn}}\left(\mathit{i}\right)$ contains the number of coefficients in the second dimension (for each coefficient type computed) at the (${n}_{\mathrm{fwd}}-\mathit{i}+1$)th level of resolution, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{fwd}}$.
4:     $\mathrm{dwtlvfr}\left({\mathbf{nwl}}\right)$int64int32nag_int array
The number of coefficients in the third dimension for each coefficient type at each level. ${\mathbf{dwtlvfr}}\left(\mathit{i}\right)$ contains the number of coefficients in the third dimension (for each coefficient type computed) at the (${n}_{\mathrm{fwd}}-\mathit{i}+1$)th level of resolution, for $\mathit{i}=1,2,\dots ,{n}_{\mathrm{fwd}}$.
5:     $\mathrm{icomm}\left(260\right)$int64int32nag_int array
Contains additional information on the computed transform.
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$
Constraint: ${\mathbf{fr}}=_$, the value of fr on initialization (see nag_wav_3d_init (c09ac)).
Constraint: ${\mathbf{m}}=_$, the value of m on initialization (see nag_wav_3d_init (c09ac)).
Constraint: ${\mathbf{n}}=_$, the value of n on initialization (see nag_wav_3d_init (c09ac)).
${\mathbf{ifail}}=2$
Constraint: $\mathit{lda}\ge {\mathbf{m}}$.
Constraint: $\mathit{sda}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=3$
lenc is too small, the total number of coefficents to be generated.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{nwl}}\le {\mathbf{nwlmax}}$ in nag_wav_3d_init (c09ac).
Constraint: ${\mathbf{nwl}}\ge 1$.
${\mathbf{ifail}}=6$
Either the communication array icomm has been corrupted or there has not been a prior call to the initialization function nag_wav_3d_init (c09ac).
The initialization function was called with ${\mathbf{wtrans}}=\text{'S'}$.
${\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.

## 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 ${c}_{ij}$ is a detail coefficient then ${\stackrel{^}{c}}_{ij}={c}_{ij}+\sigma {\epsilon }_{ij}$ and $\sigma {\epsilon }_{ij}$ is the transformed noise term. If some threshold parameter $\alpha$ is chosen, a simple hard thresholding rule can be applied as
 $c- ij = 0, if ​ c^ij ≤ α c^ij , if ​ c^ij > α,$
taking ${\stackrel{-}{c}}_{ij}$ to be an approximation to the required detail coefficient without noise, ${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 Example in nag_wav_3d_coeff_ins (c09fz) for a simple example of denoising.
See the references given in the introduction to this chapter for a more complete account of wavelet denoising and other applications.

## Example

This example computes the three-dimensional multi-level discrete wavelet decomposition for $7×6×5$ input data using the biorthogonal wavelet of order $1.1$ (set ${\mathbf{wavnam}}=\text{'BIOR1.1'}$ in nag_wav_3d_init (c09ac)) with periodic end extension, prints a selected set of wavelet coefficients and then reconstructs and verifies that the reconstruction matches the original data.
```function c09fc_example

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

% Data
m  = int64(7);
n  = int64(6);
fr = int64(5);
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
wavnam = 'Bior1.1';
mode   = 'period';
wtrans = 'Multilevel';
[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 detail coefficients
want_level = int64(1);
want_coeffs = int64(4);

cpass = char('LLH','LHL','LHH','HLL','HLH','HHL','HHH');

if (want_coeffs==0)
title = 'Approximation coefficients (LLL)'
else
title = sprintf('Detail coefficients (%s)',cpass(want_coeffs,:));
end

% Extract coefficients
[d, icomm, ifail] = c09fy(...
want_level, want_coeffs, c, icomm);
fprintf('\n%s\n Level %2d, Coefficients %2d :\n',title, want_level, ...
want_coeffs );

% Matrix printing arguments
matrix = 'General'; diag   = 'Non-unit'; fmt = 'F9.4';
labrow = 'Integer'; labcol = labrow;
rlabs  = {' '};     clabs  = rlabs;
ncols  = int64(80); indent = int64(0);

nk = dwtlvfr(nwl-want_level+1);
for k = 1:nk
title = sprintf('Frame: %3d',k);
[ifail] =  x04cb(...
matrix, diag, d(:,:,k), fmt, title, labrow, ...
rlabs, labcol, clabs, ncols, indent);
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

```
```c09fc example results

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

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

Success: the reconstruction matches the original.
```