# Frequency modulation in MATLAB

I am trying to frequency modulate a cosine signal(for assignment). I have written the following code but not getting the required output.

dt = 0.0002;
t = 0:dt:0.05;
fm = 50;
fc = 1000;
beta1 = 1;
beta2 = 2;

%making sine and m(t)
m_t = 5.*cosd(2.*pi.*fm.*t);
sin_fm = sind(2.*pi.*fm.*t); %integral of m_t. A_m adjusted in beta

%fmmod signal
fmmod1 = cosd(2.*pi.*fc.*t + beta1.*sin_fm);
fmmod2 = cosd(2.*pi.*fc.*t + beta2.*sin_fm);

%frequency_deviation
kf_1 = (beta1*fm)/5; %5 is amplitude of message signal(Am)
kf_2 = (beta2*fm)/5;

%frequency modulated signal of matlab
fmmod1_m = fmmod(m_t,fc,1/dt,kf_1);
fmmod2_m = fmmod(m_t,fc,1/dt,kf_2);

figure(1);
plot(t,fmmod1);
title('Without using fmmod beta=3');

figure(2);
plot(t,fmmod2);
title('Without using fmmod beta=5');

figure(3);
plot(t,fmmod1_m);
title('With using fmmod beta=3');

figure(4);
plot(t,fmmod2_m);
title('With using fmmod beta=5');


I have used sind in place of sin in the code because for earlier code of Amplitude Modulation sin was not giving the required waveform but when I used sind it was giving the required waveform.

AM code

dt1 = 0.0001;
fc1 = 500*1000;
fm1 = 5*1000;
t1 = 0:dt1:0.1;

%making cosine for message and carrier for higher frequency
cos_fm1 = cosd(2*pi*fm1.*t1);
cos_fc1 = cosd(2*pi*fc1.*t1);

%making Udsb_am signal for higher frequecny
Udsb_am2h = 2.*cos_fc1 + cos_fc1.*cos_fm1;
Udsb_am1h = 1.*cos_fc1 + cos_fc1.*cos_fm1;
Udsb_am_half_h = (0.5).*cos_fc1 + cos_fc1.*cos_fm1;

figure(1);
plot(t1,Udsb_am2h);
title('AM for Ac=2');
figure(2);
plot(t1,Udsb_am1h);
title('AM for Ac=1');
figure(3);
plot(t1,Udsb_am_half_h);
title('AM for Ac=0.5');


What I want to ask from the question? When should I use sind and sin?(I know sind is for degree and sin is for radian).Asking question about sind and sin because when I saw the code on internet they have used sin not sind.

What is wrong in the code of FM?

@A Q. To make your life easier, I suggest you stop using the cosd() command and only use MATLAB's cos() command. For your AM code, if variable dt1 is measured in seconds then your Fs sampling rate is 10,000 samples per second. But your carrier frequency fc1 is set to 500,000. If your fc1 = 500,000 is measured in Hz (cycles/second) then your fc1 value violates the Nyquist criterion. To continue your AM modeling, try these commands (notice the 0.3 "modulation index"):

fc1 = 1000;

fm1 = 20;

cos_fm1 = cos(2*pi*fm1.*t1);

cos_fc1 = cos(2*pi*fc1.*t1);

Udsb_am2h = 2.*cos_fc1 + .3*cos_fc1.*cos_fm1;

Udsb_am1h = 1.*cos_fc1 + .3*cos_fc1.*cos_fm1;

Udsb_am_half_h = (0.5).*cos_fc1 + 0.3*cos_fc1.*cos_fm1;

The difference is the expected input. The cosd function expects the input to be expressed in degrees, and the cos function expects the input to be in radians. So you have cosd(theta)==cos(deg2rad(theta)). This is just from the MATLAB documentation page.

Are you using these correctly? For example, you do cosd(2*pi*F*t). So we have $$\frac{2\pi \text{ radians}}{\text{cycle}}.\frac{F \text{ cycles}}{\text{second}}.[\text{seconds}]=2\pi F \text{ radians}$$, as the input. You should be using cos not cosd because your input is expressed in radians.

BTW, I do not check the correctness of the actual code. That is on you.

• If $2\pi F$ is in radians then why does the AM code does not work for sin and cos but works for sind and cosd? – A Q Feb 26 '20 at 15:22
• BTW, I do not check the correctness of the actual code. That is on you. – Engineer Feb 26 '20 at 16:03
• Only checking the code can solve the issue. Main code is only 4 to 6 lines. – A Q Feb 26 '20 at 16:05