如何剪辑/限制 tikz 中的正弦波?

如何剪辑/限制 tikz 中的正弦波?

我想使用 pgfplots 创建一个正弦波形,它会“剪辑”顶部波形。我附上了尝试执行的代码,但波形变得平滑并且偏离节点。

\begin{tikzpicture}
\draw[very thick,->](0,0)to++(4,0)node[below right]{$t$};
\draw[very thick,->](0,0)to++(0,4)node[below left]{$V_{out}$};
\draw[magenta, dashed](0,3)node(vout){}node[left]{$V_{out1,CM}$}to++(4,0);
%\draw[cyan, dashed](0,3)node[left]{$V_{out2,CM}$}to++(4,0);
\begin{axis}[
        width=2.75cm, height=2.5cm,
        trig format plots=rad,
        enlarge x limits=false,
        xtick=\empty,
        axis lines*=middle,
        hide y axis,
        hide x axis,
        scale=3,        
%        axis x line=bottom,
        at={(vout.west)},
        anchor=west,
        xshift=3mm,
         xlabel=$t$,
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=west,
        },
        ]

    \addplot [no markers,smooth, domain=0:6*pi, magenta] {(-sin(\x)>=0.5)?0.5:-sin(\x)};
%    \addplot [no markers,smooth, domain=0:6*pi, magenta] {(-sin(\x)>=0.5)?\empty:-sin(\x)};
%    \draw[dashed,black!80] (axis cs: 0,0) -- (axis cs: 2*pi,0);
\end{axis}
\end{tikzpicture}

相应的输出如下所示:

在此处输入图片描述

但我需要这样的图表:

在此处输入图片描述

答案1

您完全走在正确的轨道上。但您使用的采样点太少,这导致了smooth“过冲”。因此,将samples(默认为 25)增加到 201 可获得所需的结果。

(除此之外我稍微简化了你的代码......)

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=left,
        xlabel=$t$,
        ylabel=$V_{out}$,
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=north west,
        },
        every axis y label/.style={
            at={(ticklabel* cs:1)},
            anchor=north east,
        },
        ymin=-1.5,
        ymax=1,
        trig format plots=rad,
        enlarge x limits=false,
        xtick=\empty,
        ytick=\empty,
        clip=false,
        % moved common `\addplot` options here
        no markers,
        smooth,
        domain=0:6*pi,
        % increased number of samples to reduce "overshooting" caused from `smooth`
        samples=201,
    ]

%        \addplot [dashed,samples=2] {0.5};
        \addplot [dashed,samples=2,magenta] {0}
            node [at start,left] {$V_{out1,CM }$}
        ;
        \addplot+ [magenta] {(-sin(x)>=0.5)?0.5:-sin(x)};
        % (a bit simpler way to do what you want)
        \addplot+ [blue,dashed,restrict y to domain*=-1:0.5] {-sin(x)};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容