% dwt (Discrete wavelet transform) by Dan Piehl (2006) % % This is a discrete wavelet tranform based on the Haar wavelet. % It can do one or two dimensional transforms, and requires the % specification of a level parameter. If a negative level % is supplied, the wavelet inverse is performed. % (For instance, use +8 and -8 for a 256 point signal) % % Note: To filter color images (here x is a 3-plane double array input) % % First convert to wavelets wx=dwt(x,8); % Then apply soft threshold wz=thresh(wx,0.4,1); % Then convert back z=dwt(wz,-8); % Optional low-pass step z=lowpass(z); % Clean up invalid pixels z=imclip(z); function b = dwt(a,level) % Examine parameters and return error messages, if needed numDimsA = ndims(a); % Get the number of input dimensions S1=size(a,1); % Get the number of rows S2=size(a,2); % Get the number of columns S3=size(a,3); % Get the number of color planes if (nargin < 1 || level==0) error('MATLAB:wavelet:NoInputs',['No input arguments specified.']) end if (numDimsA<1 || numDimsA>3) % Only 1-3 dimensional arrays are allowed error('MATLAB:wavelet:NoInputs',['Invalid array dimensions.']) end if (level==0 || level>32 || level-floor(level)>0) error('MATLAB:wavelet:NoInputs',['Invalid level number.']) end if not ( (S1==1 && S2>1) || (S1>1 && S2==1) || (S1>1 && S2>1) ) error('MATLAB:wavelet:NoInputs',['Invalid array dimensions.']) end if not (S1==1 || (S1>1 && S1>=2^abs(level))) error('MATLAB:wavelet:NoInputs',['Invalid number of rows.']) end if not (S2==1 || (S2>1 && S2>=2^abs(level))) error('MATLAB:wavelet:NoInputs',['Invalid number of columns.']) end if (log(S1)/log(2)-floor(log(S1)/log(2))>0) error('MATLAB:wavelet:NoInputs',['Array dimensions must be powers of 2.']) end if (log(S2)/log(2)-floor(log(S2)/log(2))>0) error('MATLAB:wavelet:NoInputs',['Array dimensions must be powers of 2.']) end % Perform the forward or reverse wavelet transform b=a; % Reset return value iteration=0; % Reset level counter if (level>0) % For "forward" wavelet transform... while ((S1>1 || S2>1) && iteration