%===========================================================================
%=====                      Frequency Modulation                       =====
%=====                        << Expression >>                         =====
%===== ModulatedSignal = X * cos(w(carrier)*t + m*sin(w(Modulator)*t)) =====
%===========================================================================

clear
clc

SamplingRate=400;
Duration=5;
Time=0:1/SamplingRate:Duration;

CarrierAmplitude=2;
CarrierFrequency=20;
CarrierAngularFrequency=2*pi*CarrierFrequency;

Carrier=CarrierAmplitude*sin(CarrierAngularFrequency.*Time);

ModulatorAmplitude=2;
ModulatorFrequency=3;
ModulatorAngularFrequency=2*pi*ModulatorFrequency;

Modulator=ModulatorAmplitude*sin(ModulatorAngularFrequency.*Time);

ModulatorSensitivity=5;
MaximumCarrierDeviation=ModulatorSensitivity*ModulatorAmplitude;
ModulationIndex=MaximumCarrierDeviation/ModulatorFrequency;

FrequencyModulation=CarrierAmplitude*cos(CarrierAngularFrequency*Time+ModulationIndex*cos(ModulatorAngularFrequency*Time));

figure(1);
subplot(3,1,1),plot(Time,Carrier);
title('The Carrier Signal');
ylabel(['Ampitude  ',num2str(CarrierAmplitude),'  [in Volts]']);
grid on
subplot(3,1,2),plot(Time,Modulator);
title('The Modulator Signal');
ylabel(['Amplitude  ',num2str(ModulatorAmplitude),'  [in Volts]']);
grid on
subplot(3,1,3),plot(Time,FrequencyModulation);
title('The Frequency Modulated Carrier');
ylabel(['Amplitude  ',num2str(CarrierAmplitude),'  [in Volts]']);
xlabel(['Time  ',num2str(Duration),'  [in Seconds]']);
grid on