Coding wavelets the easy way

Function solutions



function det = vectorize2D(WT)


function det = vectorize2D(WT)
% reshape the Wavelet transform as a vector
scale = size(WT.Details,1);
det = [];
for i = 1:scale
deti = WT.Details{i};
detail= deti.r.s; sz=numel(detail); detail=reshape(detail,1,sz);
det = [det detail];
detail= deti.c.s; sz=numel(detail); detail=reshape(detail,1,sz);
det = [det detail];
detail= deti.d.s; sz=numel(detail); detail=reshape(detail,1,sz);
det = [det detail];
end

function t = thresholdValue2D(WT,numEl)


function t = thresholdValue2D(WT,numEl)
% we wish to keep only the numEl largest wavelet coefficients.
% to do so, we unfold the wavelet transform WT to concatenate
% the details, sort the result by descending order, and get the value
% of the element at the computed position. This value is the output
% threshold
det = vectorize2D(WT);
det = abs(det);
det = sort(det,'descend');
if size(det,2) < numEl
t = det(end);
else
t = det(numEl);
end

function y = thresholdMatrix(x,t)


function y = thresholdMatrix(x,t);
y = zeros(size(x));
[m,n]=size(x);
for j = 1:n
vect = x(:,j);
absvect = abs(vect);
I = find(absvect >= t);
y(I,j) = vect(I);
end

function WT = threshold2D(WT,t)


function WT = threshold2D(WT,t)
% puts the details of WT to 0 if
% the absolute value is below t
scale = size(WT.Details,1);
for i = 1:scale
% rows
det = WT.Details{i}.r.s;
newdet = thresholdMatrix(det,t);
WT.Details{i}.r.s = newdet;
% columns
det = WT.Details{i}.c.s;
newdet = thresholdMatrix(det,t);
WT.Details{i}.c.s = newdet;
% diagonal
det = WT.Details{i}.d.s;
newdet = thresholdMatrix(det,t);
WT.Details{i}.d.s = newdet;
end