%------------------------------------------------------------
% Tony Kim
% 6.003 PS4 Matlab assignment
%------------------------------------------------------------

%------------------------------------------------------------
% Part a
%------------------------------------------------------------
load splat;
N = 8192;
y = y(1:N);
fs = 8192;

Y = fftshift(fft(y));
w = [-pi:2*pi/N:pi-pi/N]*fs;

plot(w,abs(Y));
xlabel('w');
ylabel('abs(Y)');
title('Part a: Fourier tranform of the sound file "splat"');

%------------------------------------------------------------
% Part b
% The sound that we hear is the original file in reverse. This
% makes sense since Y*(jw) = Y(j(-w)) for real signals. The 
% latter is the transform of y(-t).
%------------------------------------------------------------
Y1 = conj(Y);
y1 = ifft(fftshift(Y1));
y1 = real(y1);

%sound(y1,fs);

%------------------------------------------------------------
% Part c
% 
% When y(t) is real, then |Y(jw)| = |Y(j(-w))| and phi(w) = -phi(-w).
% It then follows that Y2*(jw) = Y2(j(-w)) and Y3*(jw) = Y3(j(-w)), 
% hence both F.T.s satisfy the conjugate symmetry of transforms
% of real signals. So y2 and y3 are real.
%------------------------------------------------------------

%------------------------------------------------------------
% Part d
%------------------------------------------------------------
Y2 = abs(Y);
y2 = ifft(fftshift(Y2));
y2 = real(y2);

sound(y2,fs);

%------------------------------------------------------------
% Part e
%------------------------------------------------------------
Y3 = exp(j*angle(Y));
y3 = ifft(fftshift(Y3));
y3 = real(y3);

sound(y3,fs);

%------------------------------------------------------------
% Part f
% The PHASE signal seems more accurate to the original, if 
% a bit inaudible.
%------------------------------------------------------------