hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_wav_3d_coeff_ins (c09fz)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_wav_3d_coeff_ins (c09fz) inserts a selected set of three-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 reconstruction functions nag_wav_3d_sngl_inv (c09fb) or nag_wav_3d_mxolap_multi_inv (c09fd).

Syntax

[c, icomm, ifail] = c09fz(ilev, cindex, c, d, icomm, 'lenc', lenc)
[c, icomm, ifail] = nag_wav_3d_coeff_ins(ilev, cindex, c, d, icomm, 'lenc', lenc)

Description

nag_wav_3d_coeff_ins (c09fz) inserts a selected set of three-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_3d_coeff_ins (c09fz) is preceded by a call to the initialization function nag_wav_3d_init (c09ac) and either the forwards transform function nag_wav_3d_sngl_fwd (c09fa) or multi-level forwards transform function nag_wav_3d_multi_fwd (c09fc).
Given an initial three-dimensional data set A, a prior call to nag_wav_3d_sngl_fwd (c09fa) or nag_wav_3d_multi_fwd (c09fc) computes the approximation coefficients (at the highest requested level in the case of nag_wav_3d_multi_fwd (c09fc)) and, seven sets of detail coefficients (at all levels in the case of nag_wav_3d_multi_fwd (c09fc)) and stores these in compact form in a one-dimensional array c. nag_wav_3d_coeff_ext (c09fy) can then extract either the approximation coefficients or one of the sets of detail coefficients (at one of the levels following nag_wav_3d_multi_fwd (c09fc)) into a three-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_3d_coeff_ins (c09fz). Several extractions and insertions may be performed. nag_wav_3d_sngl_inv (c09fb) or nag_wav_3d_mxolap_multi_inv (c09fd) can then be used to reconstruct a manipulated data set A~. The dimensions of d depend on the level extracted and are available from either: the arrays dwtlvm, dwtlvn and dwtlvfr as returned by nag_wav_3d_multi_fwd (c09fc) if this was called first; or, otherwise from nwct, nwcn and nwcfr as returned by nag_wav_3d_init (c09ac). See Multiresolution and higher dimensional DWT in the C09 Chapter Introduction for a discussion of the three-dimensional DWT.

References

None.

Parameters

Note: the following notation is used in this section:

Compulsory Input Parameters

1:     ilev int64int32nag_int scalar
The level at which coefficients are to be inserted.
If ilev=0, it is assumed that the coefficient array c was produced by a preceding call to the single level function nag_wav_3d_sngl_fwd (c09fa).
If ilev>0, it is assumed that the coefficient array c was produced by a preceding call to the multi-level function nag_wav_3d_multi_fwd (c09fc).
Constraints:
2:     cindex int64int32nag_int scalar
Identifies which coefficients to insert. The coefficients are identified as follows:
cindex=0
The approximation coefficients, produced by application of the low pass filter over columns, rows and frames of A (LLL). After a call to the multi-level transform function nag_wav_3d_multi_fwd (c09fc) (which implies that ilev>0) the approximation coefficients are present only for ilev=nwl, where nwl is the value used in a preceding call to nag_wav_3d_multi_fwd (c09fc).
cindex=1
The detail coefficients produced by applying the low pass filter over columns and rows of A and the high pass filter over frames (LLH).
cindex=2
The detail coefficients produced by applying the low pass filter over columns, high pass filter over rows and low pass filter over frames of A (LHL).
cindex=3
The detail coefficients produced by applying the low pass filter over columns of A and high pass filter over rows and frames (LHH).
cindex=4
The detail coefficients produced by applying the high pass filter over columns of A and low pass filter over rows and frames (HLL).
cindex=5
The detail coefficients produced by applying the high pass filter over columns, low pass filter over rows and high pass filter over frames of A (HLH).
cindex=6
The detail coefficients produced by applying the high pass filter over columns and rows of A and the low pass filter over frames (HHL).
cindex=7
The detail coefficients produced by applying the high pass filter over columns, rows and frames of A (HHH).
Constraints:
  • if ilev=0, 0cindex7;
  • if ilev=nwl, following a call to nag_wav_3d_multi_fwd (c09fc) transforming nwl levels, 0cindex7;
  • otherwise 1cindex7.
3:     clenc – double array
Contains the DWT coefficients inserted by previous calls to nag_wav_3d_coeff_ins (c09fz), or computed by a previous call to either nag_wav_3d_sngl_fwd (c09fa) or nag_wav_3d_multi_fwd (c09fc).
4:     dlddsdd: – double array
The last dimension of the array d must be at least ncfr
The coefficients to be inserted.
If the DWT coefficients were computed by nag_wav_3d_sngl_fwd (c09fa) then
  • if cindex=0, the approximation coefficients must be stored in dijk, for i=1,2,,ncm, j=1,2,,ncn and k=1,2,,ncfr;
  • if 1cindex7, the detail coefficients, as indicated by cindex, must be stored in dijk, for i=1,2,,ncm, j=1,2,,ncn and k=1,2,,ncfr.
If the DWT coefficients were computed by nag_wav_3d_multi_fwd (c09fc) then
  • if cindex=0 and ilev=nwl, the approximation coefficients must be stored in dijk, for i=1,2,,ncm, j=1,2,,ncn and k=1,2,,ncfr;
  • if 1cindex7, the detail coefficients, as indicated by cindex, for level ilev must be stored in dijk, for i=1,2,,ncm, j=1,2,,ncn and k=1,2,,ncfr.
5:     icomm260 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:     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 either nag_wav_3d_sngl_fwd (c09fa) or nag_wav_3d_multi_fwd (c09fc)..

Output Parameters

1:     clenc – 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 one of the reconstruction functions nag_wav_3d_sngl_inv (c09fb) (if nag_wav_3d_sngl_fwd (c09fa) was called previously) or nag_wav_3d_mxolap_multi_inv (c09fd) (if nag_wav_3d_multi_fwd (c09fc) was called previously).
2:     icomm260 int64int32nag_int array
Communication array, used to store information between calls to nag_wav_3d_coeff_ins (c09fz).
3:     ifail int64int32nag_int scalar
ifail=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
Constraint: ilev=0 following a call to the single level function nag_wav_3d_sngl_fwd (c09fa).
Constraint: ilev>0 following a call to the multi-level function nag_wav_3d_multi_fwd (c09fc).
Constraint: ilevnwl, where nwl is the number of levels used in the call to nag_wav_3d_multi_fwd (c09fc).
   ifail=2
Constraint: cindex7.
Constraint: cindex0.
   ifail=3
Constraint: lencnct, where nct is the number of DWT coefficients computed in a previous call to nag_wav_3d_sngl_fwd (c09fa).
Constraint: lencnct, where nct is the number of DWT coefficients computed in a previous call to nag_wav_3d_multi_fwd (c09fc).
   ifail=4
Constraint: lddncm, where ncm is the number of DWT coefficients in the first dimension at the selected level ilev.
Constraint: lddncm, where ncm is the number of DWT coefficients in the first dimension following the single level transform.
Constraint: sddncn, where ncn is the number of DWT coefficients in the second dimension at the selected level ilev.
Constraint: sddncn, where ncn is the number of DWT coefficients in the second dimension following the single level transform.
   ifail=5
Constraint: cindex>0 when ilev<nwl in the preceding call to nag_wav_3d_multi_fwd (c09fc).
   ifail=6
Either the initialization function has not been called first or icomm has been corrupted.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

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 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 c09fz_example


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

% 3D Data
m = int64(4);
n = int64(4);
f = int64(4);
a = zeros(m,n,f);
a(1:2:m,:,1:2:f) = 0.01;
a(2:2:m,:,2:2:f) = 0.01;
a(2:2:m,:,1:2:f) = 1;
a(1:2:m,:,2:2:f) = 1;

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

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

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

% Multi-level wavelet transform on noisy data
[c, dwtlvm, dwtlvn, dwtlvfr, icomm, ifail] = ...
c09fc(...
       n, f, an, nwct, nwl, icomm);

% Reconstruct without thresholding of detail coefficients
[b, ifail] = c09fd(nwl, c, m, n, f, icomm);
 
% Mean square error of noisy reconstruction
mse = (norm(reshape(a-b,[m*n*f,1]))^2)/(double(m*n*f));
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*f)));
nt = 0;
nnt = 0;
for ilev = 1:nwl
  level = int64(nwl - ilev + 1);

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

    % Threshold
    d1 = dwtlvm(ilev);
    d2 = dwtlvn(ilev);
    d3 = dwtlvfr(ilev);
    for i = 1:d1
      for j = 1:d2
        for k = 1:d3
          if abs(d(i,j,k))<thresh
            d(i,j,k) = 0;
            nt = nt + 1;
          end
          nnt = nnt + 1;
        end
      end
    end
    % Insert de-noised coefficients back into c
    [c, icomm, ifail] = c09fz(...
                               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] = c09fd(nwl, c, m, n, f, icomm);

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

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


c09fz example results


Input data a:

(:,:,1) =

    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000


(:,:,2) =

    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100


(:,:,3) =

    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000


(:,:,4) =

    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100
    1.0000    1.0000    1.0000    1.0000
    0.0100    0.0100    0.0100    0.0100


Noisy data an:

(:,:,1) =

    0.0135   -0.0093   -0.0004    0.0378
    1.0015    0.9842    1.0007    0.9889
   -0.0017    0.0139    0.0138   -0.0049
    0.9899    1.0070    1.0049    0.9983


(:,:,2) =

    1.0094    1.0080    0.9921    0.9902
    0.0105   -0.0009    0.0160    0.0197
    0.9994    1.0044    0.9956    1.0014
    0.0091   -0.0084    0.0187    0.0023


(:,:,3) =

    0.0058   -0.0053    0.0011    0.0159
    1.0113    0.9894    1.0018    0.9992
    0.0106    0.0082    0.0093    0.0153
    1.0023    1.0157    1.0084    0.9834


(:,:,4) =

    0.9969    1.0010    0.9904    0.9968
    0.0227    0.0022    0.0062    0.0214
    0.9948    0.9981    0.9951    0.9968
    0.0121    0.0103    0.0114    0.0206

Without denoising Mean Square Error is  0.000081

Number of coefficients denoised is  55 out of  63
With denoising Mean Square Error is  0.000015

Reconstruction of denoised input: 

(:,:,1) =

    0.0053    0.0053    0.0166    0.0166
    1.0026    1.0026    0.9913    0.9913
    0.0055    0.0055    0.0077    0.0077
    1.0025    1.0025    1.0003    1.0003


(:,:,2) =

    1.0026    1.0026    0.9913    0.9913
    0.0053    0.0053    0.0166    0.0166
    1.0025    1.0025    1.0003    1.0003
    0.0055    0.0055    0.0077    0.0077


(:,:,3) =

    0.0073    0.0073    0.0110    0.0110
    1.0006    1.0006    0.9969    0.9969
    0.0078    0.0078    0.0131    0.0131
    1.0002    1.0002    0.9949    0.9949


(:,:,4) =

    1.0006    1.0006    0.9969    0.9969
    0.0073    0.0073    0.0110    0.0110
    1.0002    1.0002    0.9949    0.9949
    0.0078    0.0078    0.0131    0.0131


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015