# Animated cosine waveform with FM modulation using the animate package part 2

This question is building off the elegant solution given here.

I have updated the equations. For a Frequency Modulated (FM) signal, these are the equations:

Carrier = cos(2*pi*fc*t)

Modulation = sin(2*pi*fm*t)

FM = cos(2*pi*fc*t+5*sin(2*pi*fm*t))

I am trying to get the FM of this plot:

Instead, I get this:

Can you assist me in getting the correct output? Thank you!

This is the code that I modified from the original solution:

Code

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{animate}
\usepackage{xsavebox} % xlrbox
\usepackage{calc} % \widthof{...}, \real{...}

\usepackage{amsmath}

\begin{document}
%
%save two cycles in an xlrbox
\begin{xlrbox}{TwoCycles}
\begin{tikzpicture}
\begin{axis}[hide axis]
\addplot[domain=-2*pi:2*pi,black,samples=501] {sin(x*2*180/pi)};
\addplot[domain=-2*pi:2*pi,blue,samples=501] {cos(x*10*180/pi)-2.5};
\addplot[domain=-2*pi:2*pi,red,samples=501] {cos(x*10*180/pi + 5*sin(x*2*180/pi))-5};
\end{axis}
\end{tikzpicture}
\end{xlrbox}%
%
\begin{animateinline}[controls,loop,width=12cm,height=6cm]{10}
\multiframe{36}{i=0+1}{
\makebox[\widthof{\theTwoCycles}][l]{% window
\makebox[\widthof{\theTwoCycles}/\real{72}*\real{-\i}]{}% offset
\theTwoCycles\theTwoCycles%
}
}
\end{animateinline}

\end{document}


## 1 Answer

Trigonometric functions of PGF expect their argument in degrees. Thus, the whole argument must be multiplied by 180/pi for the third plotted curve.

Moreover, the size was a bit further optimized by saving only one cycle [0:pi] of the base signal in an xlrbox, and then moving five cycles in a window of four cycles width. The line join was set to round to prevent sparks from occurring in the plots.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% uncomment \def\export{} below to export animation
%% to multipage PDF a.pdf and run
%%
%%  convert -density 300 -delay 4 -loop 0 -alpha remove a.pdf b.gif
%%
%% to get an animated GIF b.gif at 100/4 = 25 frames per s
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\def\export{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifdefined\export
\documentclass[export]{standalone}
\else
\documentclass{standalone}
\fi

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{animate}
\usepackage{xsavebox} % xlrbox
\usepackage{calc} % \widthof{...}, \real{...}

\usepackage{amsmath}

\begin{document}
%
%save ONE cycle in an xlrbox
\begin{xlrbox}{OneCycle}
\begin{tikzpicture}
\begin{axis}[
hide axis,
x=1cm,y=1cm,
/tikz/line cap=rect, /tikz/line join=round
]
\addplot[domain=0:pi,black,samples=250] {cos(x*2*180/pi)};
\addplot[domain=0:pi,blue,samples=500] {cos(x*20*180/pi)-2.5};
\addplot[domain=0:pi,red,samples=500] {cos((x*20 + 6*sin(x*2*180/pi))*180/pi)-5};
\end{axis}
\end{tikzpicture}
\end{xlrbox}%
%
\begin{animateinline}[controls,loop]{10}
\multiframe{18}{i=0+1}{
\makebox[\widthof{\theOneCycle}*\real{4}][l]{% window = FOUR cycles
\makebox[\widthof{\theOneCycle}/\real{18}*\real{-\i}]{}% offset
\theOneCycle\theOneCycle\theOneCycle\theOneCycle\theOneCycle% moving FIVE cycles
}
}
\end{animateinline}

\end{document}

• Thanks for your solution! Sincerely appreciated. Is there a way to output the gif file directly as how you did to post the graphic in your solution? Thanks again! – Joe Jul 13 '18 at 11:53
• Uncomment the code section as explained in the updated code and use convert from ImageMagick.org. Now, I used x and y to scale the plot (same unit in x and y). – AlexG Jul 13 '18 at 12:12