%% dft2demo3
%% "brick wall" filtering of an image
%% 

% get image
 load clown
%load earth
%load mandrill

% get 2D FFT of image
FX = fft2(X); 

% create low pass "brick wall" mask
[m,n] = size(X);
hm = round(m/2);
hn =  round(n/2);
M = ones(size(FX));
% stop = 50; M((hm-stop):(hm+stop),(hn-stop):(hn+stop)) = 0;  % stop high frequencies
stop = 50; M(1:stop,1:stop) = 0;  M((m-stop):m,1:stop) = 0; M(1:stop,(n-stop):n) = 0; M((m-stop):m,(n-stop):n) = 0; % stop high frequencies


% create filtered image  
mFX = M.*FX;
mX = real(ifft2(mFX));

%shifted versions
sFX = fftshift(FX);
sM = fftshift(M);
smFX = fftshift(mFX);


% display results 
figure(1)
subplot(2,1,1)
imagesc(X), colormap(gray), axis equal, axis tight 
title('original')
subplot(2,1,2)
imagesc(log(1+abs(sFX))), colormap(gray), axis equal, axis tight 
title('original: shifted log(1+|dft|)')
disp('Press a key'); pause;

figure(2)
subplot(2,1,1)
imagesc(M), colormap(gray), axis equal, axis tight 
title('mask')
subplot(2,1,2)
imagesc(sM), colormap(gray), axis equal, axis tight 
title('shifted mask')
disp('Press a key'); pause;

figure(3)
subplot(2,1,1)
imagesc(mX), colormap(gray), axis equal, axis tight 
title('reconstructed image')
subplot(2,1,2)
imagesc(log(1+abs(smFX))), colormap(gray), axis equal, axis tight 
title('shifted masked dft: log(1+|mdft|)')
disp('Press a key'); pause;

figure(4)
subplot(2,1,1)
imagesc(X), colormap(gray), axis equal, axis tight 
title('original')
subplot(2,1,2)
imagesc(mX), colormap(gray), axis equal, axis tight 
title('reconstructed image')
disp('Press a key'); pause;
