%% Week 3: DSB-SC and DSB-TC comparison
Fs = 200e3;
T = 0.02;
t = 0:1/Fs:T-1/Fs;
fm = 2e3;
fc = 15e3;
Am = 1;
Ac = 1;
mu = 0.5;

m = Am*cos(2*pi*fm*t);
c = Ac*cos(2*pi*fc*t);
sSC = m.*c;
sTC = Ac*(1 + mu*cos(2*pi*fm*t)).*cos(2*pi*fc*t);

%% Time-domain comparison
figure;
subplot(4,1,1); plot(t*1e3,m); xlim([0 2]); grid on;
title('Message'); ylabel('Amplitude');
subplot(4,1,2); plot(t*1e3,c); xlim([0 0.5]); grid on;
title('Carrier'); ylabel('Amplitude');
subplot(4,1,3); plot(t*1e3,sSC); xlim([0 2]); grid on;
title('DSB-SC'); ylabel('Amplitude');
subplot(4,1,4); plot(t*1e3,sTC); xlim([0 2]); grid on;
title('DSB-TC'); xlabel('Time (ms)'); ylabel('Amplitude');

%% Centered double-sided spectra
N = length(t);
f = (-N/2:N/2-1)*(Fs/N);
SSC = fftshift(fft(sSC)/N);
STC = fftshift(fft(sTC)/N);
figure;
subplot(2,1,1); plot(f/1e3,abs(SSC)); xlim([-22 22]); grid on;
title('DSB-SC: no carrier line'); ylabel('Magnitude');
subplot(2,1,2); plot(f/1e3,abs(STC)); xlim([-22 22]); grid on;
title('DSB-TC: carrier plus sidebands'); xlabel('Frequency (kHz)'); ylabel('Magnitude');

%% Explore under-modulation, 100% modulation, and overmodulation
for mu = [0.5 1 1.5]
    s = Ac*(1 + mu*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
    figure;
    plot(t*1e3,s);
    xlim([0 2]); grid on;
    title(sprintf('DSB-TC, modulation index mu = %.1f',mu));
    xlabel('Time (ms)'); ylabel('Amplitude');
end
