考虑基于以下公式的 PWM 信号生成的 MWE这问题。
\documentclass[tikz, border=6mm]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\newcommand{\step}{.25}
\begin{tikzpicture}[>=latex]
\draw [->] (0,-1.4) -- ++(8,0) node [right] {$t$};
\draw [->] (0,-1.4) node [left] {$0$} -- ++(0,3) ;
\draw [name path=sawtooth, line width=0.1pt,line join=miter]
\foreach \x in {0,\step,...,6} {
% (\x,1.0) -- ++(0,-2.0) -- ++(\step,2.0) % y domain = -1.0 -- 1.0
(\x,1.4) -- ++(0,-2.8) -- ++(\step,2.8) % y domain = -1.4 -- 1.4
};
\draw [red,smooth, domain=0:2*pi, name path global=wave-1,line width=0.1pt] plot ({\x},{sin((\x+0) r)}) node [below right, font=\scriptsize] {$sig_1$};
\draw [blue,name intersections={of={wave-1} and sawtooth, total=\n}]
\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,\n} {
\ifodd\i {}
\else
(intersection-\lasti |- 0,-2*1) rectangle (intersection-\i |- 0,-3)
\fi
};
\draw [->] (0,-3) -- ++(8,0) node [right] {$t$};
\draw [->] (0,-3) node [left] {0} -- ++(0,1.2) ;
\end{tikzpicture}
\end{document}
这个 MWE 编译正确,生成的 PWM 信号也正确。当然,我希望锯齿信号在 y 域 (-1.0,1.0) 中,以便 PWM 信号最大和最小。但如果我用那个 y 域编译 MWE,Tikz 就无法正确确定交叉点。我用超细线制作了锯齿波和正弦波,但没有成功。有人能解释一下在这种特殊情况下确定交叉点的能力吗?
答案1
如果红灯信号非常接近交叉路口锯齿形信号的外端,Ti钾Z 只会找到一个交叉点,而不是两个。因此,您可能需要将锯齿状路径分成两条,一条垂直的,一条对角的。(顺便说一句,当想要对交叉点进行排序时,使用“假直线”有时更安全,请参阅这个答案。
\documentclass[tikz, border=6mm]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\newcommand{\step}{.25}
\begin{tikzpicture}[>=latex]
\draw [->] (0,-1.4) -- ++(8,0) node [right] {$t$};
\draw [->] (0,-1.4) node [left] {$0$} -- ++(0,3) ;
\draw [name path=sawtooth vert, line width=0.1pt,line join=miter]
\foreach \x in {0,\step,...,6} {
(\x,1.0) to[bend left=0] ++(0,-2.0) % y domain = -1.0 -- 1.0
%(\x,1.4) -- ++(0,-2.8) -- ++(\step,2.8) % y domain = -1.4 -- 1.4
};
\draw [name path=sawtooth diag, line width=0.1pt,line join=miter]
\foreach \x in {0,\step,...,6} {
(\x,-1.0) to[bend left=0] ++(\step,2.0)};
\draw [red,smooth, domain=0:2*pi, name path global=wave-1,line width=0.1pt] plot ({\x},{sin((\x+0) r)}) node [below right, font=\scriptsize] {$sig_1$};
\path [name intersections={of={wave-1} and {sawtooth diag}, name=x, total=\n}]
\pgfextra{\typeout{\n\space intersections\space in\space vert}};
\draw [blue,name intersections={of={wave-1} and {sawtooth vert}, total=\n}]
\pgfextra{\typeout{\n\space intersections}}
\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,\n} {
% \ifodd\i {}
% \else
(x-\lasti |- 0,-2*1) rectangle (intersection-\i |- 0,-3)
% \fi
};
\draw [->] (0,-3) -- ++(8,0) node [right] {$t$};
\draw [->] (0,-3) node [left] {0} -- ++(0,1.2) ;
\end{tikzpicture}
\end{document}