% AM_dsb_ideal.m: Simulation of DSB with perfect carrier synchronization.

time = .3;                                  % length of simulation (sec)
Ts = 1/10000;                               % sampling interval
t = Ts:Ts:time; lent=length(t);             % define a "time" vector 
fc = 1000;                                  % carrier freq (Hz)
c = cos(2*pi*fc*t);                         % define the carrier at freq fc

% Create "message". It will be a sum of linear and sinusoidal terms
fm = 20;                                    % freq of sinuoidal term
w = 0.0007*(1:lent) + cos(2*pi*fm*t);       % create "message" 

v = c .* w;                                 % modulate with carrier

c2 = cos(2*pi*fc*t);                        % create cosine for demod

x = v .* c2;                                % demod received signal

fbe=[0 0.1 0.2 1]; damps=[1 1 0 0]; fl=100; % low pass filter design 
b=remez(fl,fbe,damps);                      % impulse response of LPF
m=2*filter(b,1,x);                          % LPF the demodulated signal

figure(1)
% used to plot figure
subplot(4,1,1), plot(t,w)
ylabel('amplitude'); title('(a) message signal');
subplot(4,1,2), plot(t,v)
ylabel('amplitude'); title('(b) message after modulation');
subplot(4,1,3), plot(t,x)
ylabel('amplitude'); 
title('(c) demodulated signal');
subplot(4,1,4), plot(t,m)
ylabel('amplitude'); title('(d) recovered message is a LPF applied to (c)');

[ssf,wfxs] = JVK2_plotspec(w,Ts);
[ssf,vfxs] = JVK2_plotspec(v,Ts);
[ssf,xfxs] = JVK2_plotspec(x,Ts);
[ssf,mfxs] = JVK2_plotspec(m,Ts);

% used to plot freq domain picture
figure(2)
subplot(4,1,1), plot(ssf,abs(wfxs))
ylabel('amp. spec.'); title('(a) message signal');
subplot(4,1,2), plot(ssf,abs(vfxs))
ylabel('amp. spec.'); title('(b) message after modulation');
subplot(4,1,3), plot(ssf,abs(xfxs))
ylabel('amp. spec.'); 
title('(c) demodulated signal before LPF');
subplot(4,1,4), plot(ssf,abs(mfxs))
ylabel('amp. spec.'); title('(d) recovered message is a LPF applied to (c)');
