%% Week 2: conventional AM (DSB-TC)
Fs = 100e3;
T = 0.02;
t = 0:1/Fs:T-1/Fs;
Am = 1;
fm = 1e3;
Ac = 1;
fc = 10e3;
mu = 0.6;

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

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

%% Double-sided magnitude spectrum
N = length(s);
S = fft(s)/N;
f2 = (-N/2:N/2-1)*(Fs/N);
figure;
plot(f2/1e3,abs(fftshift(S)));
grid on; xlim([-15 15]);
xlabel('Frequency (kHz)'); ylabel('Magnitude');
title('Centered double-sided spectrum');

%% Single-sided magnitude spectrum
P2 = abs(S);
P1 = P2(1:floor(N/2)+1);
P1(2:end-1) = 2*P1(2:end-1);
f1 = (0:floor(N/2))*(Fs/N);
figure;
plot(f1/1e3,P1);
grid on; xlim([0 15]);
xlabel('Frequency (kHz)'); ylabel('Magnitude');
title('Single-sided spectrum');
