1

I am a beginner in TikZ and trying to illustrate different signal outputs (base line + a single pulse, baseline + multiple pulse and baseline + sustained pulse, and so on) but have quite struggled with it.

There are other questions regarding this topic and I try different methods like draw, plot smooth and the hobby but the output is never like I expected it and/or really complicated to achieve.

Following some example images, I would like to replicate in TikZ. They dosn't need to be exactly like this, they are more for ilustrating porpuses.

single pulse

sustained pulse

enter image description here

My question to the more experienced user of TikZ, how would you approach this problem? Which methods is suited best? I often ran into the problem that hobby and other packages make a dip in my "baseline" before the peak.

2 Answers2

4

This type of sketch plots can be done by just \draw and some use of controls. First example code (I used a grid to position the control points):

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[ultra thin, gray!30](0,0) grid (5,5);
  \draw[thick](0,5) |- (5,0);
  \draw[thick,red](0,0.5) -- (1,0.5) ..controls(1.75,0.5) and (1.25,4).. (2,4) ..controls(2.75,4) and (2.25,0.5).. (3,0.5) -- (5,0.5);
\end{tikzpicture}
\hspace{5mm}
\begin{tikzpicture}
  \draw[ultra thin, gray!30](0,0) grid (5,5);
  \draw[thick](0,5) |- (5,0);
  \draw[thick,green](0,0.5) -- (1,0.5) ..controls(2.5,0.5) and (1.5,4).. (3,4) -- (5,4);
\end{tikzpicture}
\hspace{5mm}
\begin{tikzpicture}
  \draw[ultra thin, gray!30](0,0) grid (5,5);
  \draw[thick](0,5) |- (5,0);
  \draw[thick,blue](0,0.5) -- (0.5,0.5) 
  ..controls (1.5,0.5) and (1.25,4).. (1.5,4) 
  ..controls (1.75,4) and (1.75,0.5).. (2,0.5)
  ..controls (2.25,0.5) and (2.25,4).. (2.5,4)
  ..controls (2.75,4) and (2.75,0.5).. (3,0.5)
  ..controls (3.25,0.5) and (3.25,4).. (3.5,4)
  ..controls (3.75,4) and (3.5,0.5).. (4.5,0.5) -- (5,0.5);
\end{tikzpicture}
\end{document}

enter image description here

The important part here is how controls work. In the example below, the curved line starts at A in the direction towards B, and ends in D in the direction from C. With such components you can make the most common base functions.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[ultra thin,gray!30](0,0) grid (5,5);
  \draw[very thick,blue!30] (0,1) coordinate(start) --
  (1,1) coordinate(A) --
  (2.5,1) coordinate(B) --   
  (2.5,4) coordinate(C) -- 
  (4,4) coordinate(D) -- 
  (5,4) coordinate(end);
  \foreach \Point in {start,A,B,C,D,end}{
    \fill[blue] (\Point) circle(2pt);
    \node[above] at (\Point) {\Point};
  }
  \draw[red] (start) -- (A) ..controls(B) and (C).. (D) -- (end);
\end{tikzpicture}
\end{document}

enter image description here

StefanH
  • 13,823
3

You can define little helpers (pics) for that.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/graph/.style={code={
 \draw[thick,pic actions] plot[variable=\x,smooth,domain=0:1] (\x,{#1});
 \draw[thick,black] (0,1) |- (1,0);
}}]
 \path[scale=4,transform shape]
  (0,3) pic[red]{graph={0.8*exp(-20*(\x-0.4)*(\x-0.4)))}}
  (0,1.5) pic[green!60!black]{graph={0.5*(1+tanh(8*(\x-0.4)))}}
  (0,0) pic[blue]{graph={0.8*exp(-100*(\x-0.2)*(\x-0.2)))+
    0.8*exp(-100*(\x-0.5)*(\x-0.5)))+0.8*exp(-100*(\x-0.8)*(\x-0.8)))}};
\end{tikzpicture}
\end{document}

enter image description here