% Collecting point from the screen
% Pleas try to start from the origin
clear all;
close all;
clc;
figure
axis([0 1 0 1]);
hold on
points = mousetrack();
hold off 

if (size(points,1) > 512 )
    N = 1024;
else
    N = 512;
end;


if ( size(points,1) < N )
    points = padarray(points,[round((N-size(points,1))/2)]);
else
    points(N+1:end,:) = [];
end;

% Adding random noise to the shape
noisy_points = points + 0.05*rand(size(points));
figure(1);
axis([0 1 0 1]);
hold on;
plot(points(:,1), points(:,2),'LineWidth',3);
plot(noisy_points(:,1), noisy_points(:,2),'-rx');
hold off;

pause;

% Fourier Transform of the noisy shape
Fpoints  = fft(noisy_points,N);
Fpoints_original  = fft(points,N);
figure(2);
subplot(2,1,1);plot(log(1+abs(fftshift(Fpoints(:,1))))); title('Fourier Spectrum Of The Noisy Data');
subplot(2,1,2);plot(log(1+abs(fftshift(Fpoints_original(:,1))))); title('Fourier Spectrum Of The Original Data');

pause;

% Filtering the Fourier Spectrum with ideal low-pass filter
filter = zeros(size(Fpoints));
middle = round(N/2);
filter(middle-30:middle+30,:) = 1;
Filtered_Fpoints = Fpoints.*fftshift(filter);
figure(3);
plot(log(1+abs(fftshift(Filtered_Fpoints(:,1)))));

IFpoints = abs(ifft(Filtered_Fpoints)); title('Filtered Fourier Spectrum');

pause;

% Displaying the results
figure(4);
subplot(1,3,1); 
hold on;
plot(points(:,1),points(:,2),'-','LineWidth',2);axis([0 1 0 1]); title('Original image');
axis square;
pause;
subplot(1,3,2); 
hold on;
plot(noisy_points(:,1), noisy_points(:,2),'-r','LineWidth',2);axis([0 1 0 1]); title('Image corrupted with noise');
axis square;
pause;
subplot(1,3,3); 
plot(IFpoints(:,1), IFpoints(:,2),'g','LineWidth',3); title('Reconstructed image');
axis square;
axis([0 1 0 1]);
hold off;
