 wname = 'db2'
% load image
load wbarb

% compute DWT
[CA,CH,CV,CD] =dwt2(X,wname,'mode','per');

% compute energies
ae = energy(CA) 
he = energy(CH) 
ve = energy(CV) 
de = energy(CD) 

ma = max(abs(CA(:))) 
mh = max(abs(CH(:)))
mv = max(abs(CV(:)))
md = max(abs(CD(:))) 

figure(1)
imshow(X,[]);
title('original')
figure(2) 
imshow([CA,CV; CH,CD],[]);
title('level 1 transform')
figure(3)
imshow([CA/ma,CV/mv;CH/mh,CD/md],[]);
title('normalized level 1 transform')

Z = zeros(size(CA));
XA = idwt2(CA,Z,Z,Z,wname,'mode','per');
XH = idwt2(Z,CH,Z,Z,wname,'mode','per');
XV = idwt2(Z,Z,CV,Z,wname,'mode','per');
XD = idwt2(Z,Z,Z,CD,wname,'mode','per');


figure(5)
subplot(2,2,1)
imshow(XA,[]);
title('approximation')
subplot(2,2,2);
imshow(XH,[]);
title('horizontal details')
subplot(2,2,3);
imshow(XV,[]);
title('vertical details')
subplot(2,2,4);
imshow(XD,[]);
title('diagonal details')
figure(6)
imshow([XA,XH+XV+XD],[]);
title('approximation and details')

%Energy calculation

wae = energy(CA) 
whe = energy(CH) 
wve = energy(CV) 
wde = energy(CD) 
xae = energy(XA) 
xhe = energy(XH) 
xve = energy(XV) 
xde = energy(XD) 

[wae,whe,wve,wde]
[xae,xhe,xve,xde]

