% NBFM and AM
fc=1000; Ac=1;          % carrier frequency (Hz) and magnitude
fm=200; Am=0.5;   % message frequency (Hz) and magnitude
kf=120;  beta=kf*Am/fm;                 % modulation parameter

t=0:1/10000:1;           % time with sampling at 10KHz
mt=Am*cos(2*pi*fm*t);   % message signal
sam=Ac*(1+mt).*cos(2*pi*fc*t);   % AM signal
sfm=Ac*cos(2*pi*fc*t+beta*sin(2*pi*fm*t));        % FM signal

subplot(221), plot(t,sam,'b'), grid, title('AM Signal')
axis([0 0.02 -2 2])
subplot(223), plot(t,sfm,'b'), grid, ylabel('NBFM Signal')
axis([0 0.02 -2 2])

% spectrum
w=((0:length(t)-1)/(length(t)-1)-0.5)*10000;
Fam=abs(fftshift(fft(sam)));     % spectrum of AM signal
Ffm=abs(fftshift(fft(sfm)));     % spectrum of FM signal

subplot(222), plot(w,Fam,'r'),
axis([-2000 2000 min(Fam) max(Fam)]), grid, title('AM Signal spectrum'),
subplot(224), plot(w,Ffm,'r'), 
axis([-2000 2000 min(Ffm) max(Ffm)]), grid, ylabel('NBFM spectrum')
