% SC_AM - demo script for suppressed carrier AM

%% initialization and message signal
fc = 8e3;

% read in message signal
[mm, fs] = wavread('kennedy_moon.wav');

% lowpass filter to limit to 3KHz
hh_mm = firpm(51, [0 2.5e3 3.0e3 fs/2]/(fs/2), [1, 1, 0, 0]);
mm = conv(mm,hh_mm);

% resample to higher sampling rate
mm = mm.';
mm = resample(mm, 4, 1);
fs = 4*fs;

% play sound and display spectrogram
spectrogram(mm,1024,0,1024,fs,'yaxis');
soundsc(mm,fs);

pause

%% AM
% carrier
tt = (0:length(mm)-1)/fs;
cc = cos(2*pi*fc*tt);

% AM signal
ss = mm .* cc;

% play sound and display spectrogram
spectrogram(ss,1024,0,1024,fs,'yaxis');
soundsc(ss,fs);

pause

%% Coherent Demod
% local carrier with phase error - coherent demodulation does not work (well) if
% the carriers at transmitter and receiver are not synchronized!
% theta = 0;
% theta = pi/2;
theta = 2*pi*10*tt + pi/2;
ccd = cos(2*pi*fc*tt + theta);

rr = ss .* ccd;

disp('Before LPF')
spectrogram(rr,1024,0,1024,fs,'yaxis');

pause

hh_lpf = firpm(51, [0, 3e3, 5e3, fs/2]/(fs/2), [1, 1, 0, 0]);

mm_hat = conv(rr, hh_lpf);

disp('After LPF')
spectrogram(mm_hat,1024,0,1024,fs,'yaxis');
soundsc(mm_hat,fs);



