Main Content

Generate MATLAB Code for 1-D Stationary Wavelet Denoising

You can generate MATLAB® code to reproduce app-based 1-D nondecimated (stationary) wavelet denoising at the command line. You must perform this operation in the Stationary Wavelet Transform Denoising 1-D tool. You must first denoise your signal before you can enable the File > Generate MATLAB Code (Denoising Process) operation.

1-D Stationary Wavelet Transform Denoising

  1. Enter waveletAnalyzer at the MATLAB command prompt.

  2. Select SWT Denoising 1-D.

  3. Load the Noisy bumps example. Select File > Example Analysis > Noisy Signals > with sym4 at level 5 - - -> Noisy bumps

  4. Set the thresholds as follows:

    • Level 1 — 3.5

    • Level 2 — 3.4

    • Level 3 — 2.3

    • Level 4 — 5.3

    • Level 5 — 2.2

    Click Denoise.

  5. Generate the MATLAB code with File > Generate MATLAB Code (Denoising Process).

    The operation generates the following MATLAB code.

    function [sigDEN,wDEC] = func_denoise_sw1d(SIG)
    % FUNC_DENOISE_SW1-D Saved Denoising Process.
    %   SIG: vector of data
    %   -------------------
    %   sigDEN: vector of denoised data
    %   wDEC: stationary wavelet decomposition
    
    % Analysis parameters.
    %---------------------
    wname = 'sym4';
    level = 5;
    
    % Denoising parameters.
    %----------------------
    % meth = 'sqtwolog';
    % scal_OR_alfa = one;
    sorh = 's';    % Specified soft or hard thresholding
    thrParams =  {...
        [...
        1.00000000  1024.00000000     3.50000000; ...
        ]; ...
        [...
        1.00000000  1024.00000000     3.40000000; ...
        ]; ...
        [...
        1.00000000  1024.00000000     2.30000000; ...
        ]; ...
        [...
        1.00000000  1024.00000000     5.29965570; ...
        ]; ...
        [...
        1.00000000  1024.00000000     2.20000000; ...
        ]; ...
        };
    
    % Decompose using SWT.
    %---------------------
    wDEC = swt(SIG,level,wname);
    
    % Denoise.
    %---------
    len = length(SIG);
    for k = 1:level
        thr_par = thrParams{k};
        if ~isempty(thr_par)
            NB_int = size(thr_par,1);
            x      = [thr_par(:,1) ; thr_par(NB_int,2)];
            x      = round(x);
            x(x<1) = 1;
            x(x>len) = len;
            thr = thr_par(:,3);
            for j = 1:NB_int
                if j==1 , d_beg = 0; else d_beg = 1; end
                j_beg = x(j)+d_beg;
                j_end = x(j+1);
                j_ind = (j_beg:j_end);
                wDEC(k,j_ind) = wthresh(wDEC(k,j_ind),sorh,thr(j));
            end
        end
    end
    
    % Reconstruct the denoise signal using ISWT.
    %-------------------------------------------
    sigDEN = iswt(wDEC,wname);
  6. Save func_denoise_sw1d.m in a folder on the MATLAB search path. Execute the following code.

    load noisbump.mat;
    [sigDEN,wDEC] = func_denoise_sw1d(noisbump);
  7. Select File > Save Denoised Signal, and save the denoised signal as denoisedbumps.mat in a folder on the MATLAB search path.

    Execute the following code.

    load denoisedbump.mat;
    plot(sigDEN,'k'); axis tight;
    hold on;
    plot(denoisedbump,'r');
    % norm of the difference
    norm(sigDEN-denoisedbump,2)

Note

Thresholds are derived from a subset of the coefficients in the stationary wavelet decomposition. For more information, see Coefficient Selection.