2

Consider the MWE of a PWM signal generation based on this question.

\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}

This MWE compiles correct and the generated PWM signal is also correct. Of course, I want the sawtooth signal to be in the y domain (-1.0,1.0) so that the PWM signal is maximal and minimal. But if I compile the MWE with that y domain, Tikz fails to determine the intersections correctly. I made the sawtooth and sine wave with ultra thin lines but no success. Can someone shed a light on the capabilities of the determination of the intersections in this particular case?

  • Does it help increasing the number of points via samples=301 in the plot? Line width doesn't matter for intersection computations. You are trying to intersect with the miter of the line, intersections are not that sensitive. – percusse May 15 '18 at 14:24
  • @percusse Sorry, no. But if I use \newcommand{\step}{.5} then I can decrease the y domain to (-1.2 -- 1.2) – Jesse op den Brouw May 15 '18 at 14:33
  • Then draw a dummy saw tooth but use a \path that is not drawn for the intersections. – percusse May 15 '18 at 14:48

1 Answers1

2

If the red signal is very close to the outer ends of the sawtooth signal at an intersection, TikZ finds only one instead of two intersections. Therefore, you may want to split the sawtooth path into two, a vertical and a diagonal one. (BTW, it is sometimes safer to work with "fake straight lines" when one wants to sort intersections, see this answer.)

\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}

enter image description here

  • this is almost what I needed. I only need that the PWM signal is high when the sine wave is greater than the sawtooth. I tried to switch vert and diag, intersection and x, but to no success. Could you adapt your answer please. – Jesse op den Brouw May 15 '18 at 17:46
  • never mind, I succeeded. Changed diag and vert in the plotting routine, deleted remembering \last, changed foreach range from 1 to \n, draw rectange from x-\i to \intersection-i... – Jesse op den Brouw May 15 '18 at 18:09
  • @JesseopdenBrouw OK, cool, sorry, I was doing other things... ;-) –  May 15 '18 at 18:14
  • I find it amazing how fast some responses are posted. Just a few hours ago I found and answer to a relatively simple question and someone beat me by 18 seconds... – Jesse op den Brouw May 15 '18 at 19:20
  • @JesseopdenBrouw Well, yesterday I was beaten by 7 seconds, and some other day I beat someone by 2..... –  May 15 '18 at 20:35