Skip to content
Rafat Hussain edited this page Apr 8, 2019 · 3 revisions

2D Wavelet Transform Class ( wt2 ) and Functions

A couple of things to know before you start using this class.

  1. All the 2D coefficients are stored in 1D row-major vectors. The coefficients access is a bit more involved as a result but memory management is simpler. I have added helper functions to make the access simpler. Also make sure to check the examples.

  2. Unlike the wt object, wt2 object does not store all the wavelet coefficients in the struct. The coefficients are allocated and are returned to the user whenever 2D forward transform is called. Freeing the wt2 object will not free the wavelet coefficients. The user must free the vector before exiting the code or once the vector is no longer needed so as to stop any memory leaks. This is more straght-forward than it seems at first. Please check the example codes in the test folder.

wt2 Initialization

wt2_object wt2_init(wave,method,rows,cols,J);


// wave - Wavelet object created using wave_object
// method - Takes char values - "dwt", "swt" and "modwt"
// rows - Input Dimension (Rows)
// cols - Input Dimension(Cols)
// J - Decomposition Levels

Wavelet Transform Execution


double *wavecoeffs; // Initialize wavelet coefficients. The memory will be allocated when you call the forward //transform

wavecoeffs=dwt2(wt, inp);// Discrete Wavelet Transform (Decimated)

wavecoeffs=swt2(wt, inp);// Stationary Wavelet Transform (Undecimated)

wavecoeffs=modwt2(wt, inp); // Maximal Overlap Discrete Wavelet Transform (Undecimated)

// Don't call the forward transform without the output wavecoeffs as each call allocates a memory block for //coefficients

free(wavecoeffs); // Free the wavecoeffs. You have to free the wavecoeffs each time FWT2 is called.

// obj - wt object
// inp - Input of dimensions rows*cols
// wavecoeffs - Array that contains all the decomposition components. The array is allocated in the wavelet
// transform functions and needs to be freed. See below for more and also check out the examples.

Inverse Wavelet Transform Execution

idwt2(wt, wavecoeffs, oup);// Inverse Discrete Wavelet Transform (Decimated)

iswt2(wt, wavecoeffs, oup);// Inverse Stationary Wavelet Transform (Undecimated)

imodwt2(wt, wavecoeffs, oup)); // Inverse Maximal Overlap Discrete Wavelet Transform (Undecimated)


// obj - wt object
// wavecoeffs - Array obtained from the forward transfrom functions
// oup - Output of dimensions rows*cols

wt2 Object Parameters

        wave_object wave; // wavelet object
        char method[10]; // "dwt", "swt" or "modwt"
        int rows;// Matrix Number of rows
        int cols; // Matrix Number of columns
        int outlength;// Length of the output DWT vector
        int J; // Number of decomposition Levels
        int MaxIter;// Maximum Iterations J <= MaxIter
        char ext[10];// Type of Extension used - "per" or "sym". Only available for method "dwt". Undecimated transforms use periodic extension only.
        int coeffaccesslength; // Length( = 3*J+1 ) of the coeffaccess vector

        int *dimensions;// Length 2*J vector that stores the number rows and columns of coefficents at each level. See below for more details  
        int *coeffaccess;// Vector that contains the values

Accessing WT output 1D vector wavecoeffs stores Output of Discrete Wavelet Transforms. It stores coefficients in the following format:

[cLL(J),cLH(J),cHL(J),cHH(J),cLH(J-1),cHL(J-1),cHH(J-1),....,cHH(1)]

where cLL(J) is the approximation coefficient vector at the Jth level while cLH(n),cHL(n),cHH(n) are the detail coefficient vectors (Horizontal, Vertical and Diagonal) at the nth level. The dimensions at each level are stored in the wt->dimensions level. At level J , number of rows is stored in wt->dimensions[0] , number of columns is stored in wt->dimensions[1] At level J-1, number of rows is stored in wt->dimensions[2] , number of columns is stored in wt->dimensions[3] At level 1, number of rows is stored in wt->dimensions[2J-2] , number of columns is stored in wt->dimensions[2J-1]

You can access wavelet coefficients once you have the rows and column sizes as each coefficient block is of size rows*cols. To make this process easier you may want to use wt->coeffaccess vector which is of size wt->coeffaccesslength.

Method 2:

cLL coefficients can be accessed at wavecoeffs[wt->coeffaccess[0]] cLH coefficients can be accessed at wavecoeffs[wt->coeffaccess[3*(J-level)+1]], where level = J,...,1 cHL coefficients can be accessed at wavecoeffs[wt->coeffaccess[3*(J-level)+2]], where level = J,...,1 cHH coefficients can be accessed at wavecoeffs[wt->coeffaccess[3*(J-level)+3]], where level = J,...,1

Method 3:

Individual coefficents can be obtained using getWT2Coeffs() function.

pointer = getWT2Coeffs(wt2_object wt,double* wcoeffs, int level,char *type, int *rows, int *cols)

where : wcoeffs is the full wavelet coefficients vector level : Coefficients at Level level. type : One of "A", "H", "V", "D" - where "A" is the Approximation vector only available at level J. "H"- Horizontal components, "V" -Vertical components and "D" - Diagonal components

rows - Number of Rows of the coefficients
cols - Number of Columns of the coefficients

The size of coefficients vector is rows*cols. The function returns a pointer that shouldn't be freed. Just free the wavecoeffs vector as has been explained above.

wt2 Functions

    double* getWT2Coeffs(wt2_object wt,double* wcoeffs, int level,char *type, int *rows, int *cols); // See above 

    void dispWT2Coeffs(double *coeff, int row, int col); // Display coefficients vector as a rows * cols matrix

    wt2_summary(wt2_object wt);// Print summary

    wt2_free(wt2_object object);// Frees wt2 object