1

In the solution to the post of tikz plot of two sine waves, I was trying to modify the code to include the product of the two signals. How do you code the product of the two signals? I tried as an initial attempt to just do the sum of the signals, however, I did not get an output. Can you help me get the product of the two signals? Thanks!

Here is the code with slight modification to add the sum of the two signals:

\documentclass[pstricks,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\usepackage{xcolor}

\usepackage[nomessages]{fp}

\FPeval\XMin{0-pi/6}
\FPeval\XMax{2*pi}
\FPeval\YMin{0-3/2}
\FPeval\YMax{5/2}

\FPeval\XOL{0-1/2} % of DeltaX
\FPeval\XOR{1/2} % of DeltaX
\FPeval\YOB{0-1/5} % of DeltaY
\FPeval\YOT{1/5} % of DeltaY

\FPset\xTrigLabelBase{6}
\FPeval\yTrigLabelBase{pi}
\FPset\Dx{1}
\FPset\Dy{1}

\FPeval\dx{pi/xTrigLabelBase*Dx}
\FPeval\dy{pi/yTrigLabelBase*Dy}

\FPeval\AxisL{XMin+dx*XOL}
\FPeval\AxisR{XMax+dx*XOR}
\FPeval\AxisB{YMin+dy*YOB}
\FPeval\AxisT{YMax+dy*YOT}


\newlength\Width\Width=12cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    xtrigLabels=true,
    %ytrigLabels=true,
    xtrigLabelBase=\xTrigLabelBase,
    %ytrigLabelBase=\yTrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
}

\newpsstyle{mygrid}
{
        dx=\dx,
        dy=\dy,
        %Dx=\Dx,
        %Dy=\Dy,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Red!30,
        subtickcolor=ForestGreen!30,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
}

\def\f{sin(x)}
\def\g{sin(x+Pi/6)}
\def\h{sin(x) + sin(x+Pi/6)}


\begin{document}
\pslegend[rt]{%
    \color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\sin x$ \\
    \color{Red}\rule{12pt}{1pt} & \color{Red} $y=\sin(x+\pi/6) x$
}
\begin{psgraph}
    [
        dx=\dx,
        dy=\dy,
        Dx=\Dx,
        Dy=\Dy,
        linecolor=gray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
    \psaxes[style=mygrid](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
    \psplot[linecolor=Red]{\XMin}{\XMax}{\g}
    \psplot[linecolor=Cyan]{\XMin}{\XMax}{\h}
    \pcline[linestyle=dashed,offset=.5,linecolor=Magenta]{|*-|*}(*{Pi 3 div} {\f})(*{Pi 2 div} {\g})\naput{ $\scriptstyle\Delta x = \tfrac{\pi}{6}$}
\end{psgraph}
\end{document}
  • This is PSTricks. You want the pgfplots equal? – percusse Jan 18 '16 at 19:07
  • Sure, I think that I can work with pgfplots! Thanks. – Joe Jan 18 '16 at 19:11
  • @percusse, Hello, any luck with the pgfplots implementation of my MWE? – Joe Jan 18 '16 at 23:47
2

Here a solution using the datavisulization library from tikz. For creating the pi-based x-axis i used a custom tick typesetter as described under 77.4.11 in the pgfmanual.

In your question you asked about the sum and the product for the functions. Because i was not sure which one you really need, i plotted both. Also your plot label shows the function g gets an additional * x whereas your function definition of g does not perform this calculation. If you need the * x in the functions you can adjust the following code to your needs.

Updated version with minor grid as requested per comment

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfmathparse{#1/pi}
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{
    \pgfmathprintnumber{\pgfmathresult}$\pi$
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac denom=6, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},        
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=4,
      }},
      x axis={
        label=$x$,
        ticks and grid={
          stack,
          step=(pi/6),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$y$, 
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1, sin2, prod, sum},
      sin1={label in legend={text={$\sin(x)$}}},
      sin2={label in legend={text={$\sin\left(x+\frac{\pi}{6}\right)$}}},
      prod={label in legend={text={$\sin(x) \sin\left(x+\frac{\pi}{6}\right)$}}},
      sum={label in legend={text={$\sin(x) + \sin\left(x+\frac{\pi}{6}\right)$}}},
    ]
    data [set=sin1, format=function] {
      var x : interval [-pi/6:2*pi] samples 100;
      func y = sin(\value x r);
    }
    data [set=sin2, format=function] {
      var x : interval [-pi/6:2*pi] samples 100;
      func y = sin((\value x + pi/6)r);
    }
    data [set=prod, format=function] {
      var x : interval [-pi/6:2*pi] samples 100;
      func y = sin(\value x r) * sin((\value x + pi/6)r);
    }
    data [set=sum, format=function] {
      var x : interval [-pi/6:2*pi] samples 100;
      func y = sin(\value x r) + sin((\value x + pi/6)r);    
    };

    \draw [{Bar[]}-{Bar[]}, thick, dashed] (2*pi/6,1.25) -- ++(pi/6,0) node [above, midway] {\scriptsize $\Delta x = \frac{\pi}{6}$};
  \end{tikzpicture}
\end{document}

rendered image

  • 1
    \sin(x) not sin(x), that way the operator is typeset upright as it should be. – Torbjørn T. Jan 19 '16 at 14:19
  • thx, changed it. – moospit Jan 19 '16 at 14:28
  • @moospit, Thanks for your solution. How do I go about getting the grid like in the original MWE? Thanks! – Joe Jan 19 '16 at 17:59
  • 1
    @Joe see my updated answer – moospit Jan 20 '16 at 10:06

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.