21

Everything is in the title...

I'm trying to draw a simple square wave , but I'm reading that tikz cannot draw the vertical line of the discontinuity...

Is there a way to overpass this ? PSTricks maybe ?

This is what I'm trying to represent :

what I'm trying to do

3isenHeim
  • 2,107
  • Seriously? I would be very surprised if tikz wasn't able to draw vertical lines... What might be true is that you will need to draw each segment separately (I unfortunately don't know tikz, otherwise I would propose an answer). – Xavier May 07 '13 at 19:07
  • Yes, tikz can actually draw vertical lines in general ^^, but not for a piece-defined function... At least, it what's I saw on the net... – 3isenHeim May 07 '13 at 19:19
  • 6
    ?? TikZ can certainly draw this. What might be trickier is to draw it as the graph of a function. A simple \draw (0,0) \foreach \k in {0,...,5} { let \n1={-2*mod(\k,2) + 1} in -- ++(0,\n1) -- ++(1,0)}; will draw the wave. – Andrew Stacey May 07 '13 at 19:28
  • @AndrewStacey: Your method requires the use of the calc library – Herr K. May 07 '13 at 19:34
  • 2
    @KevinC True, but then I left it as a comment to show that it was possible and that I felt that the question needed making more precise to show where the problem lay, not to provide an answer. – Andrew Stacey May 07 '13 at 19:35
  • 5
    A square wave doesn't have vertical lines. (No function does.) – Kaz May 07 '13 at 21:05
  • 2
    @Kaz Try with an oscilloscope hence the name wave :) – percusse May 07 '13 at 21:52
  • 2
    @percusse My analog scope has a 1Khz square vave test signal point right on the front panel where you can attach a probe. All you see is is flat tops and bottoms. – Kaz May 07 '13 at 22:00
  • @Kaz It is not analog enough if you know what I mean – percusse May 08 '13 at 04:46
  • @LoopSpace I used your code to plot this, and got this. But I wanted to to have step 1(amplitude as it is after z=L). Can I have it in the begining? Means same starting point as the end points. – L.K. Apr 27 '17 at 10:19
  • @LoopSpace This is the code \draw[ultra thick,cyan] (0,0) \foreach \k in {0,...,8} { let \n1={-2*mod(\k,2) + 1.0} in -- ++(0,\n1) -- ++(1,0)}; – L.K. Apr 27 '17 at 10:19

8 Answers8

27

A suggestion with pgfplots:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=4cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={Square wave},
xticklabels={},
ymax = 1.5,xmax=7.5,
axis lines*=center,
ytick={0.5,1},
xlabel={Time $\rightarrow$},
ylabel={Amplitude},
xlabel near ticks,
ylabel near ticks]
\addplot+[thick,mark=none,const plot]
coordinates
{(0,0) (0,1) (1,0) (2,1) (3,0) (4,1) (5,0) (6,1) (7,0)};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Torbjørn T.
  • 206,688
22

Here's one possibility using TikZ and a simple \foreach loop (I didn't drew the axes since the problem seemed to be the wave itself):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \ini [evaluate=\ini as \inieval using 2*\ini] in {0,...,6}
\draw[ultra thick,cyan] (\inieval,0) -- ++(0,1) -| (\inieval+1,0) -- (\inieval+2,0);
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • 1
    Is there a reason that you use evaluate instead of {0,2,...,12}? – Tobi May 08 '13 at 16:48
  • @Tobi I was going to give a more general approach, allowing to control the amplitude and the time interval of each pulse. Finally I went for a simple direct answer so, in this case, the use of evaluate seems superfluous; in the more general setting (that can easily be obtained with minor changes from my code) I had originally devised, it could be useful. – Gonzalo Medina May 08 '13 at 16:56
  • Just a naive question, if I would like to start from top line instead of fall, how to go about? I am just trying without any success(as I didn't understand your code) – L.K. Apr 26 '17 at 18:17
16
\documentclass{article}
\usepackage{pst-plot}
\makeatletter
\def\func{ x T1 gt { 
      T1 yVal \tx@ScreenCoor 
      2 copy 4 2 roll L pop yVal L 
      /yVal yVal 0 gt {0}{yMax}ifelse def 
      /T1 T1 T0 add def } if yVal }
\makeatother
\begin{document}

\psset{xAxisLabel=t, yAxisLabel=U}
\begin{psgraph}{->}(0,0)(0,-0.5)(11,1.5){12cm}{5cm}
\psplot[plotpoints=200,linecolor=red,linewidth=2pt]{0}{10}
  [ /yMax 1 def /T0 1 def /T1 T0 def /yVal yMax def ]{ \func }
\psplot[plotpoints=500,linecolor=blue,linewidth=2pt]{0}{10}
  [ /yMax 0.75 def /T0 1.5 def /T1 T0 def /yVal yMax def ]{ \func }
\end{psgraph}
\end{document}

enter image description here

14

Cheating with pgfplots (verticals are not true verticals :) but gets better with increased sample number).

Jake has brought me to my senses :)

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,xmin=0,width=7cm,height=4cm,
title=Square Wave,xlabel={Time$\rightarrow$},ylabel=Amplitude]
\addplot+[thick,const plot, no marks,samples at={0,1,...,10}] {(mod(x,2)>0?0:1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
9

Just for the (my) fun of it, a fairly automatic TikZ solution which also incorporates shifts and the duty factor:

Code

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows}

\begin{document}

\pgfmathsetmacro{\mydutyfactor}{0.11}
\pgfmathsetmacro{\myperiod}{1.5}
\pgfmathtruncatemacro{\myxlowborder}{-4}
\pgfmathtruncatemacro{\myxtopborder}{3}
\pgfmathsetmacro{\myamplitude}{2.1}
\pgfmathsetmacro{\myxshift}{0.3}
\pgfmathsetmacro{\myyshift}{0.4}
\newcommand{\myxlabel}{$t[s]$}
\newcommand{\myylabel}{$U[V]$}

\begin{tikzpicture}
    \draw[thick,-latex] (\myxlowborder,0) -- (\myxtopborder+0.5,0) node[right] {\myxlabel};
    \pgfmathtruncatemacro{\myminy}{floor(\myyshift-\myamplitude/2)}
    \pgfmathtruncatemacro{\mymaxy}{ceil(\myyshift+\myamplitude/2)}
    \draw[thick,-latex] (0,\myminy) -- (0,\mymaxy+0.5) node[above] {\myylabel};
    \foreach \x in {\myxlowborder,...,\myxtopborder} \draw (\x,0.03) -- (\x,-0.03) node[below ] {\x};
    \foreach \y in {\myminy,...,\mymaxy} \draw (0.03,\y) -- (-0.03,\y) node[left] {\y};
    \pgfmathtruncatemacro{\myminindex}{ceil((\myxlowborder-\myxshift)/\myperiod)-1}
    \pgfmathtruncatemacro{\mymaxindex}{floor((\myxtopborder-\myxshift)/\myperiod)}
    \pgfmathsetmacro{\mylowlevel}{\myyshift-\myamplitude/2}
    \pgfmathsetmacro{\myhihlevel}{\myyshift+\myamplitude/2}
    \clip (\myxlowborder,\myminy) rectangle (\myxtopborder,\mymaxy);
    \foreach \x in {\myminindex,...,\mymaxindex} \draw[red] (\x*\myperiod+\myxshift,\mylowlevel) -- (\x*\myperiod+\myxshift,\myhihlevel) -- (\x*\myperiod+\myxshift+\mydutyfactor*\myperiod,\myhihlevel) -- (\x*\myperiod+\myxshift+\mydutyfactor*\myperiod,\mylowlevel) -- (\x*\myperiod+\myxshift+\myperiod,\mylowlevel);
\end{tikzpicture}

\end{document}

Output

enter image description here

Tom Bombadil
  • 40,123
9

Without TikZ.

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-1,-1)(9,3)   
    \psaxes{->}(0,0)(-1,-1)(8.5,2.5)[$x$,0][$y$,90]
    \multips(0,0)(2,0){4}{\psline[linecolor=red](0,2)(1,2)(1,0)(2,0)(2,2)}
\end{pspicture}
\end{document}
5

using gnuplottex package with -shell-escape enabled and gnuplot 4.4

enter image description here

% Code compiled with pdflatex engine via frozen texlive 2012 on Linux 
% Need "-shell-escape" enabled and gnuplot 4.4 
\documentclass[preview=true,12pt]{standalone}
\usepackage{gnuplottex} % http://www.ctan.org/pkg/gnuplottex
\begin{document} 
\begin{gnuplot}[terminal=epslatex,terminaloptions=color]
# xy co-ordinates range
xmin=-19;xmax=6;ymin=-1;ymax=1;
unset border   # border off
unset xtics    # remove xaxis tics
set ytics ("0" -1, "0.5" 0, "1" 1) nomirror  # remove rightside yaxis ticks
set samples 300  # number of sample points
set arrow from xmin,ymin to xmax,ymin linewidth 1.5 # x-axis 
set arrow from xmin+0.15,ymin to xmin+0.15,ymax+0.2 linewidth 1.5 # y-axis 
set arrow from -5,ymin-0.1 to -3,ymin-0.1 # Time arrow
set title 'Square wave'
set xlabel "Time" 
set ylabel "Amplitude"  
# based on "Other definitions" at http://en.wikipedia.org/wiki/Square_wave
plot [xmin:xmax] [ymin:ymax] sgn(sin(x)) linecolor 3 linewidth 3  notitle;
\end{gnuplot}
\end{document}
3

Using TikZ decorations (this example is from TikZ manual):

\usetikzlibrary{decorations}
\pgfdeclaredecoration{example}{initial}
{
    \state{initial}[width=10pt]{
        \pgfpathlineto{\pgfpoint{0pt}{5pt}}
        \pgfpathlineto{\pgfpoint{5pt}{5pt}}
        \pgfpathlineto{\pgfpoint{5pt}{-5pt}}
        \pgfpathlineto{\pgfpoint{10pt}{-5pt}}
        \pgfpathlineto{\pgfpoint{10pt}{0pt}}
    }
    \state{final}
    {
        \pgfpathlineto{\pgfpointdecoratedpathlast}
    }
}

\tikz { \drawdecorate,decoration=example-- (3,0); \draw[red,decorate,decoration=example] (0,0)to [out=45,in=135] (3,0); }

tikz-decorator

DurandA
  • 176