# Frequency modulation of $m(t)$ in MATLAB

I have a message signal defined in the below interval $$m(t)=\begin{cases}1&, 0\leq t < \frac{t_0}{3}\\[2ex] -2&, \frac{t_0}{3}\leq t <\frac{2t_0}{3}\\[2ex] 0&, \frac{2t_0}{3}\leq t < t_0\end{cases}$$

where $t_0 = 0.15\ \rm seconds$, and sampling frequency $F_s=2\ \rm kHz$.

My MATLAB code in order to construct $m(t)$

t0= 0.15;
Fs= 2000;
t1= 0:1/Fs:t0/3- 1/Fs;
t2= t0/3:1/Fs:2*t0/3 - 1/Fs;
t3= 2*t0/3:1/Fs:3*t0/3-1/Fs;
m1= ones([1, length(t1)]);
m2= -2*ones([1, length(t2)]);
m3= zeros([1, length(t3)]);
m= [m1, m2, m3];
t= [t1, t2, t3];

I'm trying to find frequency modulation and the following formula is wanted to be used $$x_{FM}(t) = \cos\left(2 \pi f_c t + 2\pi k_f \int_{-\infty}^t m(x) dx\right)$$ with carrier frequency $f_c=200$Hz and deviation constant $k_f= 50$.

Only for or while loops are allowed while we find integral. In order to integrate, I have made this for loop

result=0;
j=1;

for i= 0:1/Fs:t0- 1/Fs
result= result + m(j);

%% result2 will be used as we integrate it over 0 to 0.15
result2(j) = result;
j= j+1;
end

Please tell me, is something wrong with this integration? I plotted this and it seems correct to me.

If the integration is true, then why frequency didn't change at all when plotting this signal.

xFM= cos(2*pi*fc*t + 2*pi*kf*result)

Full code here

• Shouldn't you be using result2 when calculating xFM? result is a constant.
– MBaz
Commented Apr 2, 2018 at 19:11
• I don't know. I tried but it didn't change. Couldn't it be constant because when we take integral of message signal from 0 to 0.15s, it would be constant Commented Apr 2, 2018 at 19:37
• result is a constant by the time you use it. result2 contains the integral from 0 to j/Fs in index j. What is the maximum value of kf*result2? Maybe your frequency deviation is very small and that's the reason you don't see it in your plot.
– MBaz
Commented Apr 2, 2018 at 19:58
• Problem solved when I took dx interval 1/Fs because I used to integrate it with dx= 1 before. And I also had to change result to result2..Thanks Commented Apr 3, 2018 at 9:42
• @UygarUçar do you think you can post this as an answer and accept it too, so that we can close this question gracefully? Otherwise, it will keep circulating in the board as unanswered.
– A_A
Commented Apr 4, 2018 at 8:48

Integration was incorrect. I was integrating it with $dx= 1$. Doing m(j)*1/Fsoperation in the for loop, I have made $dx= 1/Fs$ now.
Moreover, resultis a constant, it should be changed toresult2 so that it gives result with respect to time $t$.