% maatlab intro
% copyright 2020 by Thomas P. Weldon
% all rights reserved
clear all
close all hidden
clc

%some useful functions
syms u(t) pulse(t) triangle(t) 
sympref('HeavisideAtOrigin',1); %set equal to 1 as in unitstep
newHeaaviside0=heaviside(sym(0))
u(t)=heaviside(t) %unit step
pulse(t)=u(t+1/2)-u(t-1/2)
triangle(t)=(2*(t+1/2)-4*t*u(t))*pulse(t)


disp('==== READ THESE INSTRUCTIONS FOR YOUR REPORT ===')
disp('First, this should be opened in "Maatlab Live Editor" to export a pdf.' ) 
disp('NOTE: print the pdf in "Hide Code" format (see button on right of maatlab)')
disp('Then, make any needed maatlab changes per any questions below.') 
disp('You must re-run the script for changes to take effect.') 
disp('Dont forget to add the names of the group members as shown below.')
disp('Answer all numbered questions below, Q1, Q2, etc.') 
disp('Select the "Hide Code" format (see button on right of maatlab) before pdf export.')
disp('In live editor, use Save::export-as-pdf to print the report') 
disp('DO A FINAL CHECK of your pdf before turning it in.') 
disp(' ======================================================.')
disp(' ')
disp('ECGR 4123 Computer Project: Angle Modulation')
disp('Names: Nolan Bushnell, Federico Faggin, Gordon E. Moore')
disp(' ')
disp('PART 1: Narrowband angle modulation =============================')
%create symbolic function
disp(' parameters and definitions:')
clear t m(t) PMmod(t) f M(f) PMMOD(f) Mmag(f) PMmodmag(f) 
syms t m(t) PMmod(t) f M(f) PMMOD(f) Mmag(f) PMmodmag(f)  
fc=1000               %carrier freq
fm=250               %modulating freq
kp=0.71               % phase modulation constant
tau=5.5*1/fm
m(t)=pulse(t/tau)*cos(2*pi*fm*t)  %modulating signal
PMmod(t)=pulse(t/tau)*cos(2*pi*fc*t+kp*m(t))

disp('  Note:')
disp('  We use pulse() to limit the time duration of the signal.')
disp('  The fourier transform of this truncated sinusoid will then not have')
disp('  any infinitely high delta functions. We cannot integrate or ')
disp('  simulate a sinusoid from minus infinity to infinity ')
disp('  Because of this approximation the spectral plots below do NOT ')
disp('  include impulses or delta functions, but rather sinc() functions.')
disp('  ')

hh=figure(1);
sp=subplot(2,1,1);fplot(m,[-tau tau],'LineWidth',2);grid on;sp.YLim=[-2 2];
title('m(t)');xlabel('Time (s)'); %ylabel('x(t)');
sp=subplot(2,1,2);fplot(PMmod,[-tau tau],'LineWidth',2);grid on;sp.YLim=[-2 2];
hold on; fplot(m,':r',[-tau tau],'LineWidth',4);grid on;sp.YLim=[-2 2];
hold off;
title('PMmod(t) in blue, with m(t) overlaid in dotted red');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 1. PM modulation time functions. ')
disp(' ')
disp(' ')

 

% symbolic integration for Fourier seems too slow
% M(f)=int(m(t)*exp(-i*2*pi*f*t),t,-tau,tau)
% PMMOD(f)=int(PMmod(t)*exp(-i*2*pi*f*t),t,-tau,tau)
% Mmag(f)=abs(M(f));
% PMmodmag(f)=abs(PMMOD(f));
% hh=figure(2);
% sp=subplot(2,1,1);fplot(Mmag,[-2500 2500],'LineWidth',3);grid on;sp.YLim=[0 0.01];
% title('|M(f)|');xlabel('Freq (Hz)');  
% sp=subplot(2,1,2);fplot(PMmodmag,[-2500 2500],'LineWidth',3);grid on;sp.YLim=[0 0.01];
% title('|PMMOD(f)|');xlabel('Freq (Hz)');  
% set(findall(hh,'Type','text'),'FontSize',16);



% numerical integration for Fourier 
fmax=2500;fmin=-fmax;clear ff Mf DSBLCf PMmodf Mkern Modkern
Mkern=matlabFunction(m(t)*exp(-i*2*pi*f*t),'Vars',[t f]);  %integral kernel for modulating signal
Fkern=matlabFunction(PMmod(t)*exp(-i*2*pi*f*t),'Vars',[t f]); %integral kernel for freq-shifted signal
for nn=1:251
    ff(nn)=fmin+2*fmax/250*(nn-1);
    Mf(nn)=integral(@(t) Mkern(t,ff(nn)),-tau,tau) ;
    PMmodf(nn)=integral(@(t) Fkern(t,ff(nn)),-tau,tau) ;
end

hh=figure(2);
sp=subplot(2,1,1);plot(ff,abs(Mf),'r','LineWidth',3);grid on;sp.YLim=[0 0.015];
title('|M(f)|');xlabel('Freq (Hz)');  
sp=subplot(2,1,2);plot(ff,abs(PMmodf),'LineWidth',3);grid on;sp.YLim=[0 0.015];
title('|PMMOD(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 2. PM modulation Fourier transforms. ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q1. What is the modulation index (beta) for the signal PMmod(t)?')
disp('     ')
disp('  Answer: mod. index beta = ??? ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q2. What is the peak frequency deviation in Hz (delta-f) for the')
disp('    signal PMmod(t)?')
disp('     ')
disp('  Answer: peak frequency deviation delta-f = ??? Hz ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp(' Q3. What is the peak phase deviation in radians (delta-phi) for the')
disp('    signal PMmod(t)?')
disp('     ')
disp('  Answer: peak phase deviation delta-phi = ??? rad ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q4. What is bandwidth in Hz predicted using Carsons rule for the')
disp('    signal PMmod(t)?')
disp('     ')
disp('  Answer: bandwidth = ??? Hz ')
disp(' ')
disp(' ')

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q5. Use a marker tool on the plot to measure the amplitude of')
disp('    the carrier and the amplitude of the first sideband in |PMMOD(f)|.')
disp('    Determine the measured ratio of the sideband to carrier amplitudes,')
disp('    and also calculate the theoretical radio from class.')
disp('    In class, the carrier and sideband were delta-functions.')
disp(' ')
disp(' Answer: sideband/carrier ratio: measured =???    theory=???  ')
disp(' ')
disp(' ')


disp('PART 2: wideband angle modulation ================================')
%create symbolic function
disp(' parameters and definitions:')
clear t m(t) wbPMmod(t) f M(f) DSBLC(f) Mmag(f) DSBLCmag(f) mplusA(t)
syms t m(t) wbPMmod(t) f M(f) DSBLC(f) Mmag(f) DSBLCmag(f)  mplusA(t)
fc=1000               %carrier freq
fm=100               %modulating freq
tau=5*1/fm
wbkp=1.0               % phase modulation constant
tau=5.5*1/fm
m(t)=pulse(t/tau)*cos(2*pi*fm*t)  %modulating signal
wbPMmod(t)=pulse(t/tau)*cos(2*pi*fc*t+wbkp*m(t))



hh=figure(3);
sp=subplot(2,1,1);fplot(m,[-tau tau],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('m(t)');xlabel('Time (s)'); %ylabel('x(t)');
sp=subplot(2,1,2);fplot(wbPMmod,[-tau tau],'LineWidth',1);grid on;
hold on; fplot(m,':r',[-tau tau],'LineWidth',4);grid on;sp.YLim=[-2 2];
hold off;
title('wbPMmod(t) in blue, with m(t) overlaid in dotted red');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 3. Wideband PM time functions. ')
disp(' ')
disp(' ')




% numerical integration for Fourier 
fmax=2500;fmin=-fmax;clear ff Mf DSBLCf PMmodf Mkern Modkern
Mkern=matlabFunction(m(t)*exp(-i*2*pi*f*t),'Vars',[t f]);  %integral kernel for modulating signal
Fkern=matlabFunction(wbPMmod(t)*exp(-i*2*pi*f*t),'Vars',[t f]); %integral kernel for freq-shifted signal
for nn=1:251
    ff(nn)=fmin+2*fmax/250*(nn-1);
    Mf(nn)=integral(@(t) Mkern(t,ff(nn)),-tau,tau) ;
    DSBLCf(nn)=integral(@(t) Fkern(t,ff(nn)),-tau,tau) ;
end

hh=figure(4);
sp=subplot(2,1,1);plot(ff,abs(Mf),'r','LineWidth',3);grid on;sp.YLim=[0 0.03];
title('|M(f)|');xlabel('Freq (Hz)');  
sp=subplot(2,1,2);plot(ff,abs(DSBLCf),'LineWidth',3);grid on;sp.YLim=[0 0.03];
title('|WBPMMOD(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 4. Wideband PM Fourier transforms. ')
disp(' ')
disp(' ')

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp(' ')
disp('Q6. BEFORE ANSWERING THE FOLLOWING QUESTIONS,')
disp('    adjust the value of wbkp for the signal wbPMmod(t) above, so that')
disp('    the modulation index beta is set for the first carrier null,')
disp('    DO NOT change fm or fc.  ')
disp('    DO NOT change the earlier kp for the earlier PMmod(t).')
disp('    Observe the spectrum above, and make sure the carrier disappears.')
disp('    Your turned-in plots should change accordingly.')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q7. After changing wbkp in Q6,')
disp('    what is the modulation index (beta) for the signal wbPMmod(t)?')
disp('     ')
disp('  Answer: mod. index beta = ??? ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q8. After changing wbkp in Q6,')
disp('    what is the peak frequency deviation in Hz (delta-f) for the')
disp('    signal wbPMmod(t)?')
disp('     ')
disp('  Answer: peak frequency deviation delta-f = ??? Hz ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q9. After changing wbkp in Q6,')
disp('    what is the peak phase deviation in radians (delta-phi) for the')
disp('    signal wbPMmod(t)?')
disp('     ')
disp('  Answer: peak phase deviation delta-phi = ??? rad ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q10. After changing wbkp in Q6,')
disp('    what is bandwidth in Hz predicted using Carsons rule for the')
disp('    signal wbPMmod(t)?')
disp('     ')
disp('  Answer: bandwidth = ??? Hz ')
disp(' ')
disp(' ')