% Filtering in the frequency domain (Butterworth Lowpass Filter)

f = imread( 'lenna256.jpg' ); % Unsigned 8-bit integer for storage
f = double( f ); % double precision variable for floating point operations

f_P = padarray(f, [256,256], 'post');

imagesc( f_P )
colormap( gray ( 256 ) )
axis image
F = fftshift(fft2(f_P));

H = zeros(512);

% Construct Butterworth Lowpass Filter, H

D_0 = 40;
n = 1;

for u = 1:512
    for v = 1:512
        D = sqrt(((u-256)^2)+((v-256)^2));
        H(u,v) = 1/(1+(D/D_0)^(2*n));  
    end
end

imagesc( H )
colormap( gray ( 256 ) )
axis image
G = H.*F;

g_P = real(ifft2(fftshift(G)));

g = g_P(1:256,1:256);

imagesc( g )
colormap( gray ( 256 ) )
axis image