%% blur and add noise to an image
% display original, blurred image, blurry and noisy image
% display mask and |abs(DFT)| of the mask 

% S. Allen Broughton

%% load image
load mandrill
[m,n]= size(X);

%% create masks
% column filtering
ge = [1,1]; gb =[1,ge];
g = [gb,zeros(1,m-length(gb)-length(ge)),ge];
gw = sum(abs(g));
g = g/gw;

% row filtering
he = [1,1,1]; hb =[1,he];
h = [hb,zeros(1,n-length(hb)-length(he)),he];
hw = sum(abs(h));
h = h/hw;

% 2D mask
mask = g'*h;

%% blur by filtering 
% transform images
Y = circconvs2(X,gb'*hb, gb'*he,ge'*hb,ge'*he); % blur image
Z = Y+ 10*(2*rand(size(X))-1);                           % add noise

%% show images
figure(1) 
imagesc(X);
colormap(gray)
title('image before blur')

figure(2)
imagesc(Y);
colormap(gray)
title('blurred image');

figure(3)
imagesc(Z);
colormap(gray)
title('blurred and noisy image');

%% Show mask and dft
figure(4)
imagesc(abs(fftshift(mask)));
colormap(gray)
title('shifted mask');

figure(5)
imagesc(log(1+abs(fftshift(fft2(mask)))));
colormap(gray)
title('shifted log(1+|dft|) of mask');
