6

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

enter image description here

Here is the code that I have so far:

Code

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\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]
    \addplot[black,samples=101] {5*cos(x*180./pi)};
  \end{axis}

% Modulating Signal
    \begin{axis}[
    hide axis,
    %xlabel={$x$},
    %ylabel={$\text{sin}(x)$},
    xmin=-4*pi,xmax=4*pi]
    \addplot[blue,samples=501] {5*cos(6*x*180./pi)};
  \end{axis}

% Modulated Signal
    \begin{axis}[
    hide axis,
    %xlabel={$x$},
    %ylabel={$\text{sin}(x)$},
    xmin=-4*pi,xmax=4*pi]
    \addplot[red,samples=501] {5*cos(x*180./pi)*5*cos(6*x*180./pi)};
  \end{axis}


\end{animateinline}

\end{document} 
7

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.

enter image description here

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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] {0.8*cos(x*2*180/pi)};
      \addplot[domain=0:pi,blue,samples=500] {cos(x*20*180/pi)-2};
      \addplot[domain=0:pi,red,samples=500] {(1+0.8*cos(x*2*180/pi))*cos(x*20*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. 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
6

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

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\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,
        ]
            \addplot[domain=-360:360,black,samples=101] {carrier(x+\PhaseShift)};
            \addplot[domain=-360:360,blue,samples=501] {modulator(x+\PhaseShift)-2.5};
            \addplot[domain=-360:360,red,samples=501] {modulator(x+\PhaseShift)*carrier(x+\PhaseShift) - 5};
        \end{axis}
    \end{tikzpicture}%
}

\begin{document}
    \begin{animateinline}[label=graph_switch,controls]{10}
        \multiframe{\NumFrames}{iFrame=0+1}{\drawModulatedAM{\iFrame}}
    \end{animateinline}
\end{document} 

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.