如何在 LaTex 中绘制简单的正弦波、方波、锯齿波波形?

如何在 LaTex 中绘制简单的正弦波、方波、锯齿波波形?

我想在 LaTex 中绘制一个简单的波形。但是,我只有sine wave,如果可能的话,但dotted line就像下面的例子一样。

在此处输入图片描述

我有如下的基本 MVE:

\documentclass[]{article}

\usepackage{subcaption}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[h!]
    \centering
    \hfill
    \subcaptionbox{(sine wave)
        \label{fig:Simple pure tone (sine wave)}
    }{%
        \resizebox{.45\textwidth}{.3\textwidth}{%
            \begin{tikzpicture}[
                        declare function={%
                        f1(\x) = 1.2*sin(2.5*deg(\x)); f2(\x)=0.6*(1.2*sin(2*deg(\x ))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
                    }
                ]
             \begin{scope}[local bounding box=T]
               \draw[latex-latex](0,2)  node[above]{} |- (8,0) ;
               \draw plot[domain=0:7,variable=\x,samples=51,smooth] ({\x},{f1(\x)});
              \end{scope}

             \path foreach \X in {T} {(\X.east) node[below] {}};
            \end{tikzpicture}
        }
    }

\end{figure}

\end{document} 

其输出:

在此处输入图片描述

答案1

看看这个。我现在没时间做太多解释,但是如果你有什么不明白的,可以问。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[yscale=0.5]

\foreach \y in {0,...,4}
     \draw [densely dotted] (0,-3*\y)  -- +(6,0);

\begin{scope}[local bounding box=sine]
  \draw plot[domain=0:6,variable=\x,samples=51,smooth] (\x,{sin(deg(\x*pi))});
\end{scope}

\begin{scope}[yshift=-3cm, local bounding box=square]
  %\draw (0,0) |- ++(1,1) |- ++(1,-2) |- ++(1,2) |- ++(1,-2) |- ++(1,2) |- ++(1,-2) |- ++(0,1);
  % the above line does the same as the following one, but without the foreach loop
  \draw (0,0) foreach \x in {1,2,3}  {|- ++(1,1) |- ++(1,-2) -- ++(0,1)};
\end{scope}

\begin{scope}[yshift=-6cm, local bounding box=ramp]
  %\draw (0,0) |- ++(0.25,1) |- ++(1.75,-1) |- ++(0.25,1) |- ++(1.75,-1) |- ++(0.25,1) |- ++(1.75,-1);
  % the above line does the same as the following one, but without the foreach loop
  \draw (0,0) foreach \x in {1,2,3} {|- ++(0.25,1) |- ++(1.75,-1)};
\end{scope}

\begin{scope}[yshift=-9cm, local bounding box=sawtooth]
  %\draw (0,-1) -- ++(0,2) -- ++(2,-2) -- ++(0,2) -- ++(2,-2) -- ++(0,2) -- ++(2,-2);
  % the above line does the same as the following one, but without the foreach loop
  \draw (0,-1) foreach \x in {1,2,3} {-- ++(0,2) -- ++(2,-2) };
\end{scope}

\begin{scope}[yshift=-12cm, local bounding box=triangle]
  \draw plot[samples at={0,0.5,...,6}] (\x,{sin(deg(\x*pi))});
\end{scope}

\foreach \bb in {sine,square,ramp,sawtooth,triangle}
   \node [below,font=\footnotesize] at (\bb.south) {\bb};
\end{tikzpicture}
\end{document}

要获取子图标签,您可以\subcaption{..}直接在放置标签的节点中使用,因为您已经在使用该包。请注意,这需要您为节点subcaption设置。text width

在下面的例子中,我在每个里面添加了虚线的绘制scope,而不是使用循环,这样可以更容易地调整间距,只需改变的数量即可yshift

我还修改了制作字幕的循环,以展示如何在字幕中添加自定义文本。屏幕截图未更新。

\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\captionsetup[subfigure]{skip=0pt} % to reduce space above subcaption
\begin{tikzpicture}[yscale=0.5]
\begin{scope}[local bounding box=sine]
  \draw [densely dotted] (0,0)  -- +(6,0);
  \draw plot[domain=0:6,variable=\x,samples=51,smooth] (\x,{sin(deg(\x*pi))});
\end{scope}

\begin{scope}[yshift=-3.3cm, local bounding box=square]
  \draw [densely dotted] (0,0)  -- +(6,0);
  \draw (0,0) foreach \x in {1,2,3}  {|- ++(1,1) |- ++(1,-2) -- ++(0,1)};
\end{scope}

\begin{scope}[yshift=-6.5cm, local bounding box=ramp]
  \draw [densely dotted] (0,0)  -- +(6,0);
  \draw (0,0) foreach \x in {1,2,3} {|- ++(0.25,1) |- ++(1.75,-1)};
\end{scope}

\begin{scope}[yshift=-9cm, local bounding box=sawtooth]
  \draw [densely dotted] (0,0)  -- +(6,0);
  \draw (0,-1) foreach \x in {1,2,3} {-- ++(0,2) -- ++(2,-2) };
\end{scope}

\begin{scope}[yshift=-12cm, local bounding box=triangle]
  \draw [densely dotted] (0,0)  -- +(6,0);
  \draw plot[samples at={0,0.5,...,6}] (\x,{sin(deg(\x*pi))});
\end{scope}

\foreach \bb/\txt in {sine/thingamajig,square/foo,ramp/bar,sawtooth/baz,triangle/bodkin}
   \node [below,font=\footnotesize,text width=6cm] at (\bb.south) {\subcaption{\txt}};
\end{tikzpicture}
\caption{stuff}
\end{figure}
\end{document}

相关内容