%Code to downsample the Handel Hallelujah Chorus

load Handel;          %Built in sample of Hallelujah
load anti-aliasing;   %Filter provided by TAs

%y now contains the wav data points.
%Fs now contains the sampling frequency: 8192
%Hs now contains the filter

%Proceed with the aliased version
aliased_down_sample=y(1:2:length(y));
aliased_fft=fft(aliased_down_sample);

%Proceed with filtered, unaliased signal
fy=fft(y);
pause;
%Apply low pass filter
filteredfy=fy.*Hs;

%Get filtered signal
filteredy=ifft(filteredfy);

%Sample at half the rate: 4096
unaliased_down_sample=filteredy(1:2:length(filteredy));
unaliased_fft=fft(unaliased_down_sample);

subplot(2,2,1);
freq_xcoords=(1:length(unaliased_fft));
plot(freq_xcoords,real(aliased_fft),'g');
xlabel('s');ylabel('F(s)');title('Aliased');

subplot(2,2,2);
plot(freq_xcoords,real(unaliased_fft),'b');
xlabel('s');ylabel('F(s)');title('Unaliased');

subplot(2,2,3);
xcoords=(1:length(unaliased_down_sample));
plot(xcoords,real(unaliased_down_sample));
xlabel('t');ylabel('f(t)')

subplot(2,2,4);
plot(xcoords,real(aliased_down_sample));
xlabel('t');ylabel('f(t)');

%play the sounds and compare
sound(real(unaliased_down_sample),Fs/2);

sound(real(aliased_down_sample),Fs/2);
