[m,fs]=audioread('hw6.wav'); %fs is the sampling frequency used when sampling the message -- note that m is a vector of real numbers
L=length(m); %length of the message
Ts=1/fs;
f=[-1/(2*Ts):1/(L*Ts):1/(2*Ts)-1/(L*Ts)];

plot(f,fftshift(abs((fft(m)))*Ts));
xlabel('f [Hz]');
ylabel('|M(f)|');

waitforbuttonpress

%%%%%%%%%%%%%%%%

s=0.1;
WI=sqrt(s)*randn(L,1); %This generates a realization of the in-phase noise sequence 
figure
plot(WI);
xlabel('t');ylabel('W_I(t)')
sound(WI,fs);

waitforbuttonpress

%%%%%%%%%%%%%%%%%

WQ=sqrt(s)*randn(L,1); %This generates a realization of the quadrature noise sequence 
Wz=WI+j*WQ;

Yz=m*exp(j*4/9*pi)+Wz;
Mhat=real(Yz);
sound(Mhat,fs);

waitforbuttonpress

%%%%%%%%%%%%%%%%%

Mhat=real(Yz*exp(-j*4/9*pi)); %phase compensation
sound(Mhat,fs);

waitforbuttonpress

%%%%%%%%%%%%%%%%%

H=(f>=-2000)&(f<=2000); 
YzFourier=fftshift(fft(Yz))*Ts; 
YzFourierfiltered=YzFourier.*H';
Yzfiltered=(ifft(ifftshift(YzFourierfiltered)))*1/Ts; 
Mhat1=real(Yzfiltered*exp(-j*4/9*pi));
sound(Mhat1,fs);

waitforbuttonpress

