Main Content

Generate MATLAB Code for 2-D Wavelet Packet Denoising and Compression

2-D Wavelet Packet Compression

You can generate MATLAB® code to reproduce app–based 2-D wavelet packet compression at the command line. You must perform this operation in the Wavelet 2-D - - Compression tool. You must first compress your image before you can enable the File > Generate MATLAB Code (Compression Process) operation.

  1. Enter waveletAnalyzer at the MATLAB command prompt.

  2. Select Wavelet Packet 2-D.

  3. Select File > Load > Example Analysis > Indexed Images, and load the tire.

  4. Using the default parameter settings, click Best Tree.

  5. Click Compress.

  6. Set Select thresholding method to Bal.sparsity-norm (sqrt).

  7. Click Compress.

  8. File > Generate Code (Compression Process) generates the following code.

    function [XCMP,wptCMP] = func_compress_wp2d(X)
    % FUNC_COMPRESS_WP2D Saved Compression Process.
    %   X: matrix of data
    %   -----------------
    %   XCMP: matrix of compressed data
    %   wptCMP: wavelet packet decomposition (wptree object)
    
    % Analysis parameters.
    %---------------------
    Wav_Nam = 'haar';
    Lev_Anal = 2;
    Ent_Nam = 'shannon';
    Ent_Par = 0;
    
    % Compression parameters.
    %-----------------------
    % meth = 'sqrtbal_sn';
    sorh = 'h';    % Specified soft or hard thresholding
    thrSettings = {sorh,'nobest',16.499999999999886,1};
    roundFLAG = true;
    
    % Decompose using WPDEC2.
    %-----------------------
    wpt = wpdec2(X,Lev_Anal,Wav_Nam,Ent_Nam,Ent_Par);
    
    % Nodes to merge.
    %-----------------
    n2m = [2  3];
    for j = 1:length(n2m)
        wpt = wpjoin(wpt,n2m(j));
    end
    
    % Compression using WPDENCMP.
    %----------------------------
    [XCMP,wptCMP] = wpdencmp(wpt,thrSettings{:});
    if roundFLAG , XCMP = round(XCMP); end
    if isequal(class(X),'uint8') , XCMP = uint8(XCMP); end
  9. Save the generated MATLAB code as func_compress_wp2d.m in a folder on the MATLAB search path, and execute the following code.

    load tire;
    [XCMP,wptCMP] = func_compress_wp2d(X);

  10. Save the compressed image from the Wavelet 2-D -- Compression tool as compressed_tire.mat in a folder on the MATLAB search path. Use File > Save > Compressed Image to save the compressed image.

  11. Execute the following code to compare the command line and Wavelet Analyzer app result.

    load compressed_tire.mat;
    norm(XCMP-X,2)