disp('This program uses a threshold (on absolute values)')
disp('to discard Fourier coefficients.')
disp('For nine different thresholds the program')
disp('shows the image obtained after Fourier coefficients')
disp('are discarded, and also displays the threshold,')
disp('the percentage of discarded (i.e. set to 0)')
disp('coefficients, and the MSE.')
disp(['Keep in mind that 1/256 = ',num2str(1/256)])
disp('This MSE value is reached with approximately')
disp('50% of discarded coefficients,')
disp('but fairly good results are obtained even with')
disp('appr. 70% of discarded coefficients.')


pause

zeroims = cell(1,9);

im = imread('100_2007.jpg');
im = im2double(im);
im = 0.6*im(:,:,1)+0.3*im(:,:,2)+0.1*im(:,:,3);
im = im(51:250,101:300);

f = fft2(im);
fs = fftshift(f);

for i=1:9

fs2 = fs;

t = 2^(-7-i);
zeroims{i} = (abs(fs2/(200*200))>=t);

fs2 = fs2.*zeroims{i};

f2 = ifftshift(fs2);
recim =ifft2(f2); 

recerror = mse(recim(:)-im(:));

subplot(3,3,i);

imshow(recim);
title(['t = ',num2str(t),' zeros = ',num2str(100*sum(fs2(:)==0)/(200*200),5),'%   sqrt(mse)=',num2str(sqrt(recerror))]);
end

pause

for i=1:9
  subplot(3,3,i);
  imshow(zeroims{i});
end


