# Animated cosine waveform with FM modulation using the animate package

I would like to get some help to get an animation of a modulated FM signal like this:

Here is the code that I have so far:

Code

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{media9}
\usepackage{animate}[2014/11/27]

\usepackage{amsmath}

\begin{document}

\begin{animateinline}[
%poster=1, % sin 2x (frames are numbered in zero-based manner)
width=12cm,
height=6cm,
label=graph_switch,
begin={\begin{tikzpicture}},
end={\end{tikzpicture}},
step
]{0}

% Carrier Signal
\begin{axis}[
hide axis,
%xlabel={$x$},
%ylabel={$\text{sin}(x)$},
xmin=-4*pi,xmax=4*pi]
\end{axis}

% Modulating Signal
\begin{axis}[
hide axis,
%xlabel={$x$},
%ylabel={$\text{sin}(x)$},
xmin=-4*pi,xmax=4*pi]
\end{axis}

% Modulated Signal
\begin{axis}[
hide axis,
%xlabel={$x$},
%ylabel={$\text{sin}(x)$},
xmin=-4*pi,xmax=4*pi]
\end{axis}

\end{animateinline}

\end{document}


You are looking for an AM example. (For FM, see ↗follow-up Q.)

The plotted equations were modified somewhat (acc. to Wikipedia page on ↗Amplitude Modulation).

Here is an approach using the xsavebox package. This saves final PDF file size and a lot of compilation time.

1. We save one cycle [0:pi] of the base signal into an xlrbox, outside animateinline.
2. Then, the animation is created by moving 5 cycles in a window of 4 cycles width.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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}
\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
]
\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. How do you save the image directly to a gif file? Thanks! – Joe Jul 13 '18 at 2:28
• The plotted functions have been adjusted a bit according to Wikipedia. Gif creation explained in the code. – AlexG Jul 13 '18 at 12:50

Like this? I added the controls for debugging, you can set step or autoplay as you wish.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{media9}
\usepackage{animate}[2014/11/27]

\usepackage{amsmath}

\tikzset{
declare function={
carrier(\t) = cos(\t);
modulator(\t) = cos(6*\t);
},
}

\pgfmathsetmacro\StepSize{10}
\pgfmathtruncatemacro\NumFrames{360/\StepSize}

\newcommand{\drawModulatedAM}[1]{%
\begin{tikzpicture}
\pgfmathsetmacro\PhaseShift{#1*\StepSize}
\begin{axis}[
hide axis,
scale only axis,
width = 12cm,
height = 6cm,
xmin=-360,
xmax=360,
]