Open in the MATLAB editor: c09ez_example
function c09ez_example fprintf('c09ez example results\n\n'); % 2D Data m = int64(7); n = int64(6); a = zeros(m,n); a(1:2:m,:) = 0.01; a(2:2:m,:) = 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, 0, 1.0e-4, state); an = a + reshape(x,[m,n]); fprintf('\nInput data a:\n'); disp(a); fprintf('\nNoisy data an:\n'); disp(an); % Wavelet setup wavnam = 'DB6'; mode = 'Period'; wtrans = 'Multilevel'; [nwl, nf, nwct, nwcn, icomm, ifail] = ... c09ab(... wavnam, wtrans, mode, m, n); % Multi-level wavelet transform on noisy data lenc = nwct; % Perform Discrete Wavelet transform [c, dwtlvm, dwtlvn, icomm, ifail] = ... c09ec(... an, lenc, nwl, icomm); % Reconstruct without thresholding of detail coefficients [b, ifail] = c09ed(nwl, c, m, n, icomm); % Mean square error of noisy reconstruction mse = (norm(reshape(a-b,[m*n,1]))^2)/(double(m*n)); 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))); nt = 0; nnt = 0; for ilev = 1:nwl level = int64(nwl - ilev + 1); for detail = int64(1:3) % Extract the selected set of coefficients. [d, icomm, ifail] = c09ey(... level, detail, c, icomm); % Threshold d1 = dwtlvm(ilev); d2 = dwtlvn(ilev); for i = 1:d1 for j = 1:d2 if abs(d(i,j))<thresh d(i,j) = 0; nt = nt + 1; end nnt = nnt + 1; end end % Insert de-noised coefficients back into c [c, icomm, ifail] = c09ez(... 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] = c09ed(nwl, c, m, n, icomm); % Mean square error of de-noised reconstruction mse = (norm(reshape(a-b,[m*n,1]))^2)/(double(m*n)); fprintf('With denoising Mean Square Error is %9.6f\n\n',mse); disp('Reconstruction of denoised input: '); disp(b);
c09ez example results Input data a: 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100 Noisy data an: 0.0135 0.0170 -0.0049 -0.0009 0.0002 0.0123 1.0015 0.9896 0.9983 1.0044 1.0097 0.9847 -0.0017 0.0107 0.0194 -0.0084 0.0114 -0.0006 0.9899 1.0038 1.0005 0.9921 0.9923 0.9982 -0.0093 0.0149 0.0094 0.0160 0.0058 0.0257 0.9842 1.0278 0.9991 0.9956 1.0113 0.9911 0.0139 -0.0011 0.0180 0.0187 0.0106 0.0118 Without denoising Mean Square Error is 0.000098 Number of coefficients denoised is 32 out of 48 With denoising Mean Square Error is 0.000018 Reconstruction of denoised input: 0.0127 0.0094 0.0030 0.0007 0.0009 0.0065 0.9913 0.9940 1.0000 1.0027 1.0032 0.9976 0.0084 0.0086 0.0072 0.0048 0.0028 0.0050 1.0009 0.9998 0.9966 0.9942 0.9930 0.9965 0.0061 0.0070 0.0103 0.0134 0.0154 0.0114 1.0034 1.0036 1.0028 1.0011 0.9996 1.0011 0.0135 0.0113 0.0093 0.0114 0.0147 0.0148