% This program takes an image of a clown and shows what it
% looks like after compressing it with a 2 dimensional FFT
%
disp('Display images of clowns compressed using a 2D FFT')
% get clown data in matrix X
load clown.mat;
% compute 2-D FFT
fX = fft2(X);
fm = max(max(abs(fX)));
% choose set of thresholds to try compressing with
% (will zero out entries of fX less than 
%    threshold * largest entry of fX = threshold * fm)
th = [0 1e-4 1e-3:1e-3:1e-2];
% plot original clown
figure(1)
image(X), 
colormap('gray');
title('Original 200 by 320 gray scale image')
% make postscript file of figure
% print -deps clown0.eps
% plot compressed clowns, for all thresholds t in th
i = 1;
for t = th;
   i = i+1;
   disp(' ')
   disp(['threshold = ',num2str(t)])
   figure(i), hold off, clf
%  zero out entries of fx less than t*fm
   ffX = fX.*(abs(fX) >= t*fm);
%  produce compressed clown
   cX = real(ifft2(ffX));
%  plot compressed clown
   image(cX);
   colormap('gray');
   title(['Image Compressed by FFT,  keep components > ',num2str(t),' times largest'])
%  compute compression = fraction of nonzero components in ffX
   c = nnz(ffX)/nnz(fX);
   xlabel(['Compression (fraction nonzero components) = ',num2str(c)])
   disp(['Compression (fraction nonzero components) = ',num2str(c)])
%    make postscript file of figure
%    eval(['print -deps clown',int2str(i-10),'.eps'])
   disp('pause (hit return to continue)'), pause
end

