Coding wavelets the easy way

function t = thresholdValue(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
scale = size(WT.Details,1);
det = [];
for i = 1:scale
det = [det WT.Details{i}.s];
end
det = abs(det);
det = sort(det,'descend');
if size(det,2) < numEl
t = det(end);
else
t = det(numEl)
end

---------------------------------------

function WT = threshold(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
det = WT.Details{i}.s;
absdet = abs(det);
I = find(absdet >= t);
newdet = zeros(size(det));
newdet(I)=det(I);
WT.Details{i}.s = newdet;
end