% illustrates the aliasing effect on an image

% get the image 

im = imread('house.tif','tif');
map = [(0:255)' (0:255)' (0:255)'] / 255;

% subsample the image

sub = im(1:2:256, 1:2:256);  

% show the image and the subsampled one

figure(1), image(im)
colormap(map)
title('original image')

figure(2)
image(sub)
colormap(map)
title('subsampled image')

%B = fir1(10,0.5), % low pass filter from signal toolbox
B = [5.060317124844847e-03 -3.250615490734487e-18 -4.194287943134473e-02 ...
     1.321043449104680e-17 2.884848263026376e-01 4.967954720077247e-01 ...
     2.884848263026376e-01 1.321043449104680e-17 -4.194287943134475e-02 ...
     -3.250615490734488e-18 5.060317124844847e-03];
     
%[Bf,W]=freqz(B,1,256); % freqz from signal toolbox
%figure(3),plot(W,abs(Bf)) % plots the lowpass filter used

Bf = abs(DTFTfast(B, 256)); Bf = Bf(258 : end);

figure(3), plot(Bf)
title('the lowpass filter used')

% Construct the symmetric filter
% to avoid the use of the signal toolbox, this has been saved

%H = ftrans2(B); % generate symmetric filter based on B (signal toolbox)
%save ftrans2 H 

load ftrans2 H

% apply the low pas filter and subsample, and show the result

ff = filter2(H, im);
ffsub = ff(1 : 2 : 256, 1 : 2 : 256);

figure(4)
image(ffsub)
title('lowpass filter on subsampled image')
colormap(map)


