% Generation of PM for Waveforms of m1(t) and m2(t)
clear;clc;clf;close all
fs = 1e3;tstart = 0;tend = 4;t = tstart:tend/fs:tend-1/fs;Am = 1.0;
Ac = 1.0;fc = 2;kp = 5;kf= 5;
T = t(end);t1 = numel(t)/2;
ct = Ac*cos(2*pi*fc*t);
m1t = [Am*ones(1,t1) -Am*ones(1,t1)];
m2t = [Am*t(1:t1) -2*Am*(t(t1+1:end)-T)/(T-t(t1))];
yt = Ac*cos(2*pi*fc*t + kp*m1t);
subplot(2,1,1);plot(t,m1t,'LineWidth',4,'Color','Red')
ylabel('Amplitude');title('Message Signal  m_1(t)')
axis ([-(max(t)-min(t))*0.0 max(t)*1.0 min(m1t)*1.1 max(m1t)*1.1])
subplot(2,1,2);plot(t,yt)
xlabel('t (Time - Second)'); ylabel('Amplitude')
title('PM Modulation for m_1(t)')
set(gcf,'Color',[1 1 1]);pause;clf reset
yt = Ac*cos(2*pi*fc*t + kp*m2t);
subplot(2,1,1);plot(t,m2t,'LineWidth',4,'Color','Red')
ylabel('Amplitude');title('Message Signal  m_2(t)')
axis ([-(max(t)-min(t))*0.0 max(t)*1.0 min(m2t)*1.1 max(m2t)*1.1])
subplot(2,1,2);plot(t,yt)
xlabel('t (Time - Second)'); ylabel('Amplitude')
title('PM Modulation for m_2(t)')
set(gcf,'Color',[1 1 1]);pause;clf reset
% Generation of FM for Waveforms of m1(t) and m2(t)
m1ti = [Am*t(1:t1) -2*Am*(t(t1+1:end)-T)/(T-t(t1))];m1td = m1t;%m1td = m1ti;
m2ti = [Am*t(1:t1).^2/2 2+Am*(-t(t1+1:end).^2+2*T*t(t1+1:end)+t(t1)^2-2*T*t(t1))/(T-t(t1))];m2td = m2t;%m2td = m2ti;
yt = Ac*cos(2*pi*fc*t + kf*m1ti);
subplot(2,1,1);plot(t,m1td,'LineWidth',4,'Color','Red')
ylabel('Amplitude');title('Message Signal  m_1(t)')
axis ([-(max(t)-min(t))*0.0 max(t)*1.0 min(m1t)*1.1 max(m1t)*1.1])
subplot(2,1,2);plot(t,yt)
xlabel('t (Time - Second)'); ylabel('Amplitude')
title('FM Modulation for m_1(t)')
set(gcf,'Color',[1 1 1]);pause;clf reset
yt = Ac*cos(2*pi*fc*t + kf*m2ti);
subplot(2,1,1);plot(t,m2td,'LineWidth',4,'Color','Red')
ylabel('Amplitude');title('Message Signal  m_2(t)')
axis ([-(max(t)-min(t))*0.0 max(t)*1.0 min(m2t)*1.1 max(m2t)*1.1])
subplot(2,1,2);plot(t,yt)
xlabel('t (Time - Second)'); ylabel('Amplitude')
title('FM Modulation for m_2(t)')
set(gcf,'Color',[1 1 1]);pause;clf reset
% Frequency Spectrum of Sinosoidal FM Waveform
tstart = 0;tend = 0.1;
fm = 100 ;fc = 1e3;Ac = 1.0;Am = 0.5;fs = 6*fc;kf = 1000;betaf = kf*Am/fm;
t = tstart:1/fs:tend;
mt = Am*sin(2*pi*fm*t);ct = Ac*cos(2*pi*fc*t);
ut = Ac*cos(2*pi*fc*t + kf*mt/fm);
subplot(2,1,1);plot(t,mt,'LineWidth',2,'Color','Red')
ylabel('Amplitude');title('Message Signal  m(t)')
%axis ([-(max(t)-min(t))*0.05 max(t)*1.05 min(m1t)*1.1 max(m1t)*1.1])
subplot(2,1,2);plot(t,ut)
xlabel('t (Time - Second)'); ylabel('Amplitude')
title('FM Modulation for m(t)')
set(gcf,'Color',[1 1 1]);pause;clf reset
JnB = [];Jstr = [];
for i = 1:10
    JnB = [JnB;besselj(i-1,betaf)];
end
JnB = abs(JnB/max(JnB)); 
for i = 1:10
    Jchar=['J_' num2str(i-1) '(' num2str(betaf) ') = '];
    Jstr = [Jstr;Jchar num2str(JnB(i),'%4.2f')];
end    
Uf = fft(ut,512); Uff = Uf.*conj(Uf)/512;Uff = sqrt(Uff/max(Uff));
f = fs*(0:256)/512;
plot(f,Uff(1:257));set(gcf,'Color',[1 1 1])
title('Frequency Spectrum for FM Signal U(f)')
xlabel('frequency (Hz)');ylabel('Normalized Amplitude');
legend(Jstr,1);pause;