%------------------------------------------------------------
% Tony Hyun Kim
% 6.003 PS #2, Matlab assignment
%------------------------------------------------------------

% Store the audio sample "lineup" (sampled at 8192Hz) 
% in the vector y.
load lineup.mat;

%------------------------------------------------------------
% Part (a)
%------------------------------------------------------------
he = zeros(1,1000);
he(1)    = 1;
he(1000) = 1/2; 

%------------------------------------------------------------
% Part (b): Completed by hand. (See following page)
%------------------------------------------------------------


%------------------------------------------------------------
% Part (c)
%------------------------------------------------------------

% Prepare the coefficients in a_k*y(n-(k-1)) = b_m*x(n-(m-1))
% where k and m are Matlab indices (starts with 1), and 
% a sum over k and m is assumed.
a       = zeros(1,1001);
a(1)    = 1;
a(1001) = 1/2;
b = 1;

% Prepare the unit sample input
d = [1 zeros(1,4000)];
her = filter(b,a,d);
subplot(211);
stem(her);
xlabel('n');
ylabel('h[n]');
title('Impulse response of the echo remover system');

%------------------------------------------------------------
% Part (d)
%------------------------------------------------------------
z = filter(1,a,y);
subplot(212);
plot(z);
xlabel('n');
ylabel('z[n], echo-removed sound file');
title('Removed echo by ERS');
