f=imread('Fig4.03(a).jpg');

figure 
image(f); colormap(gray); axis image; title('input image f'); 

[M N] = size(f); 

f=double(f);

% multiply f by (-1)^(x+y) to shift the center 
for i = 1:M,
   for j = 1:N,
         d=(i-1)+(j-1);
         B(i,j)=f(i,j)*(-1)^d;
   end
end

% compute the DFT of f*(-1)^{x+y} 
F=fft2(B); 

% compute the spectrum 
D=abs(F);

%%%%%%%%%%%%%%
% Plot the centered spectrum, after rescaling using the log transform 
%%%%%%%%%%%%%%

c=5; 
for i = 1:M,
             for j = 1:N,

                 E(i,j)=c*log(1+D(i,j));
             end
         end

% plot the spectrum (transformed by log for visualisation purposes) 
figure 
image(E); colormap(gray); axis image; title('spectrum of FT of f'); 

%%%%%%%%%%%%%
% compute the IFFT2 to go back to f 
%%%%%%%%%%%%%

%% apply IFFT and take the real part 
ff=real(ifft2(F));

% multiply ff by (-1)^(x+y) to shift the center back to the origin 
for i = 1:M,
   for j = 1:N,
         d=(i-1)+(j-1);
         fff(i,j)=ff(i,j)*(-1)^d;
   end
end

% the result (equal with the input image) 
figure
image(fff); colormap(gray); axis image; title('the result f obtained by taking the IFT'); 
