function [t,f,s,S] = am(fm,fc,Am,Ac)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ECOMMS Spring 00 Class Demo
%S. Mandayam, ECE Dept., Rowan University
%
%Function Usage: [t,f,s,S] = am(fm,fc,Am,Ac)
%
%Purpose:
%Demonstrate Single Tone Amplitude Modulation in  
%time and frequency Domains
%
%s(t)=Ac*[1+Am*cos(2*pi*fm*t)]*cos(2*pi*fc*t)
%
%Function Inputs:
%fm: modulating signal frequency
%fc: carrier signal frequency
%Am: Modulating signal amplitude
%Ac: Carrier signal amplitude
%
%Function Outputs
%t: time axis
%f: frequency axis
%s: time domain AM signal
%S: frequency domain AM signal
%S is a complex quantity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;

%sampling parameters
T=3/fm;
dt1=1/(fc*100);
N=round(T/dt1);
dt=T/N;

%samples index
n=0:N-1;

%corresponding time index
t=n*dt;

%time domain AM signal
s=Ac*(1+Am*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
figure(1);
hold;
plot(t,Am*cos(2*pi*fm*t),'r');
plot(t,Ac*cos(2*pi*fc*t),'g');
plot(t,s);
legend('Message: m(t)','Carrier: c(t)','AM Signal: s(t)');

title(['Time Domain AM: s(t) = ' int2str(Ac) '[1 + ' int2str(Am) 'cos(2\pi' int2str(fm) 't)]cos(2\pi' int2str(fc) 't)']);
xlabel('t in s');

pause;

%sampling frequency
fs=1/dt;
%calculate the FFT
S = dt*fft(s);
%calculate the frequency axis
f=n/T;
f_plot_start=fc-4*fm;
f_plot_end=fc+4*fm;
N_start=round(f_plot_start*T);
N_end=round(f_plot_end*T);
figure(2);
plot(f(N_start:N_end), abs(S(N_start:N_end)));
title(['AM Amplitude Spectrum: f_m = ' int2str(fm) 'Hz, f_c = ' int2str(fc) 'Hz']);
xlabel(['f in Hz: Sampling Frequency f_s = ' int2str(fs) 'Hz']);
ylabel('|S(f)|');
