tikzlibrarydsp.code.tex (8793B)
1 % ------------------------------------------------------------------------- 2 % This library for block diagrams and signal flow graphs was inspired by 3 % the library "signalflow" of Dr. Karlheinz Ochs, Ruhr-University of Bochum, 4 % Germany. Furthermore, some ideas were taken from the library "circuitikz" 5 % of Massimo A. Redaelli and from the PGF library itself. 6 % 7 % Copyright 2012 by Matthias Hotz 8 % 9 % This work is licensed under the Creative Commons Attribution 2.5 Generic 10 % License. To view a copy of this license, visit 11 % http://creativecommons.org/licenses/by/2.5/ 12 % or send a letter to Creative Commons, 444 Castro Street, Suite 900, 13 % Mountain View, California, 94041, USA. 14 % ------------------------------------------------------------------------- 15 16 \usetikzlibrary{arrows, calc, positioning, decorations.markings} 17 18 % ------------------------------------------------------------------------- 19 % Parameters for the library 20 21 \newcommand{\dsplinewidth}{0.25mm} % Line width for connections 22 \newcommand{\dspblocklinewidth}{0.3mm} % Line width for blocks 23 \newcommand{\dspoperatordiameter}{4mm} % Diameter for adder, multiplier, mixer 24 \newcommand{\dspoperatorlabelspacing}{2mm} % Distance from symbol to label for adder, multiplier, mixer 25 \newcommand{\dspnoderadius}{1mm} % Filled and empty node 26 \newcommand{\dspsquareblocksize}{8mm} % Size for square blocks, e.g. for delay elements, decimator, expander 27 \newcommand{\dspfilterwidth}{14mm} % Width of a filter block 28 29 % ------------------------------------------------------------------------- 30 % Define new arrow heads 31 32 \pgfarrowsdeclare{dsparrow}{dsparrow} 33 { 34 \arrowsize=0.25pt 35 \advance\arrowsize by .5\pgflinewidth 36 \pgfarrowsleftextend{-4\arrowsize} 37 \pgfarrowsrightextend{4\arrowsize} 38 } 39 { 40 \arrowsize=0.25pt 41 \advance\arrowsize by .5\pgflinewidth 42 \pgfsetdash{}{0pt} % Solid line (do not dash) 43 \pgfsetmiterjoin % Fixed miter join of line 44 \pgfsetbuttcap % Fixed butt cap of line 45 \pgfpathmoveto{\pgfpoint{-4\arrowsize}{2.5\arrowsize}} 46 \pgfpathlineto{\pgfpoint{4\arrowsize}{0pt}} 47 \pgfpathlineto{\pgfpoint{-4\arrowsize}{-2.5\arrowsize}} 48 \pgfpathclose 49 \pgfusepathqfill 50 } 51 52 \pgfarrowsdeclare{dsparrowmid}{dsparrowmid} 53 { 54 \arrowsize=0.25pt 55 \advance\arrowsize by .5\pgflinewidth 56 \pgfarrowsleftextend{-4\arrowsize} 57 \pgfarrowsrightextend{4\arrowsize} 58 } 59 { 60 \arrowsize=0.25pt 61 \advance\arrowsize by .5\pgflinewidth 62 \pgfsetdash{}{0pt} 63 \pgfsetmiterjoin 64 \pgfsetbuttcap 65 \pgfpathmoveto{\pgfpoint{0}{2.5\arrowsize}} 66 \pgfpathlineto{\pgfpoint{8\arrowsize}{0pt}} 67 \pgfpathlineto{\pgfpoint{0}{-2.5\arrowsize}} 68 \pgfpathclose 69 \pgfusepathqfill 70 } 71 72 % ------------------------------------------------------------------------- 73 % Define new node shapes 74 75 \makeatletter 76 77 \pgfkeys{/tikz/dsp/label/.initial=above} 78 79 % Generic shape generator for operators, i.e. nodes with a circular 80 % shape with an additional customizable drawing and a text label 81 \long\def\dspdeclareoperator#1#2{ 82 \pgfdeclareshape{#1} 83 { 84 % Saved anchors, macros and dimensions 85 \savedanchor\centerpoint{\pgfpointorigin} 86 \savedmacro\label{\def\label{\pgfkeysvalueof{/tikz/dsp/label}}} 87 \saveddimen\radius 88 { 89 \pgfmathsetlength\pgf@xa{\pgfshapeminwidth} 90 \pgfmathsetlength\pgf@ya{\pgfshapeminheight} 91 \ifdim\pgf@xa>\pgf@ya 92 \pgf@x=.5\pgf@xa 93 \else 94 \pgf@x=.5\pgf@ya 95 \fi 96 } 97 98 % Inherit all anchors from the 'circle'-shape: 99 \inheritanchor[from={circle}]{center} 100 \inheritanchor[from={circle}]{mid} 101 \inheritanchor[from={circle}]{base} 102 \inheritanchor[from={circle}]{north} 103 \inheritanchor[from={circle}]{south} 104 \inheritanchor[from={circle}]{west} 105 \inheritanchor[from={circle}]{east} 106 \inheritanchor[from={circle}]{mid west} 107 \inheritanchor[from={circle}]{mid east} 108 \inheritanchor[from={circle}]{base west} 109 \inheritanchor[from={circle}]{base east} 110 \inheritanchor[from={circle}]{north west} 111 \inheritanchor[from={circle}]{south west} 112 \inheritanchor[from={circle}]{north east} 113 \inheritanchor[from={circle}]{south east} 114 \inheritanchorborder[from={circle}] 115 116 % Draw circle and embed additional code 117 \backgroundpath 118 { 119 % Draw circle 120 \pgfpathcircle{\centerpoint}{\radius} 121 122 % Embed additional code 123 % (Note that this code must call e.g. \pgfusepathqstroke 124 #2 125 } 126 127 % Define anchor parametrized by the PGF key /tikz/dsp/label 128 \anchor{text} 129 { 130 \centerpoint 131 % 132 \def\templabelabove{above} 133 \def\templabelbelow{below} 134 \def\templabelleft{left} 135 \def\templabelright{right} 136 \pgfutil@tempdima=\dspoperatorlabelspacing 137 % 138 \ifx\label\templabelabove 139 \advance\pgf@x by -0.5\wd\pgfnodeparttextbox 140 \advance\pgf@y by \radius 141 \advance\pgf@y by \pgfutil@tempdima 142 \fi 143 % 144 \ifx\label\templabelbelow 145 \advance\pgf@x by -0.5\wd\pgfnodeparttextbox 146 \advance\pgf@y by -\radius 147 \advance\pgf@y by -\pgfutil@tempdima 148 \advance\pgf@y by -\ht\pgfnodeparttextbox 149 \fi 150 % 151 \ifx\label\templabelleft 152 \advance\pgf@x by -\radius 153 \advance\pgf@x by -\pgfutil@tempdima 154 \advance\pgf@x by -\wd\pgfnodeparttextbox 155 \advance\pgf@y by -0.5\ht\pgfnodeparttextbox 156 \advance\pgf@y by +0.5\dp\pgfnodeparttextbox 157 \fi 158 % 159 \ifx\label\templabelright 160 \advance\pgf@x by \radius 161 \advance\pgf@x by \pgfutil@tempdima 162 \advance\pgf@y by -0.5\ht\pgfnodeparttextbox 163 \advance\pgf@y by +0.5\dp\pgfnodeparttextbox 164 \fi 165 } 166 } 167 } 168 169 \dspdeclareoperator{dspshapecircle}{\pgfusepathqstroke} 170 171 \dspdeclareoperator{dspshapecirclefull}{\pgfusepathqfillstroke} 172 173 \dspdeclareoperator{dspshapeadder}{ 174 % Coordinate offset for the plus 175 \pgfutil@tempdima=\radius 176 \pgfutil@tempdima=0.55\pgfutil@tempdima 177 178 % Draw plus 179 \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{0pt}{-\pgfutil@tempdima}}} 180 \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{0pt}{ \pgfutil@tempdima}}} 181 182 \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfutil@tempdima}{0pt}}} 183 \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{ \pgfutil@tempdima}{0pt}}} 184 185 \pgfusepathqstroke 186 } 187 188 \dspdeclareoperator{dspshapemixer}{ 189 % Coordinate offset for the cross 190 \pgfutil@tempdima=\radius 191 \pgfutil@tempdima=0.707106781\pgfutil@tempdima 192 193 % Draw cross 194 \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfutil@tempdima}{-\pgfutil@tempdima}}} 195 \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{ \pgfutil@tempdima}{ \pgfutil@tempdima}}} 196 197 \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfutil@tempdima}{ \pgfutil@tempdima}}} 198 \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{ \pgfutil@tempdima}{-\pgfutil@tempdima}}} 199 200 \pgfusepathqstroke 201 } 202 203 \makeatother 204 205 % ------------------------------------------------------------------------- 206 % Define node styles 207 208 \tikzset{dspadder/.style={shape=dspshapeadder,line cap=rect,line join=rect, 209 line width=\dspblocklinewidth,minimum size=\dspoperatordiameter}} 210 \tikzset{dspmultiplier/.style={shape=dspshapecircle,line cap=rect,line join=rect, 211 line width=\dspblocklinewidth,minimum size=\dspoperatordiameter}} 212 \tikzset{dspmixer/.style={shape=dspshapemixer,line cap=rect,line join=rect, 213 line width=\dspblocklinewidth,minimum size=\dspoperatordiameter}} 214 215 \tikzset{dspnodeopen/.style={shape=dspshapecircle,line width=\dsplinewidth,minimum size=\dspnoderadius}} 216 \tikzset{dspnodefull/.style={shape=dspshapecirclefull,line width=\dsplinewidth,fill,minimum size=\dspnoderadius}} 217 218 % The fixed specification of text height and text depth is the somewhat 219 % unaesthetic workaround to align the text in different node at the same 220 % baseline. See the PGF/TikZ manual, ch. 5.1. 221 \tikzset{dspsquare/.style={shape=rectangle,draw,align=center,text depth=0.3em,text height=1em,inner sep=0pt, 222 line cap=round,line join=round,line width=\dsplinewidth,minimum size=\dspsquareblocksize}} 223 \tikzset{dspfilter/.style={shape=rectangle,draw,align=center,text depth=0.3em,text height=1em,inner sep=0pt, 224 line cap=round,line join=round,line width=\dsplinewidth,minimum height=\dspsquareblocksize,minimum width=\dspfilterwidth}} 225 226 % ------------------------------------------------------------------------- 227 % Define "signal flow" lines 228 229 \tikzset{dspline/.style={line width=\dsplinewidth},line cap=round,line join=round} 230 \tikzset{dspconn/.style={->,>=dsparrow,line width=\dsplinewidth},line cap=round,line join=round}%line cap=rect,line join=miter} 231 \tikzset{dspflow/.style={line width=\dsplinewidth,line cap=round,line join=round, 232 decoration={markings,mark=at position 0.5 with {\arrow{dsparrowmid}}},postaction={decorate}}} 233 234 % ------------------------------------------------------------------------- 235 % Define various utility macros 236 237 \newcommand{\downsamplertext}[1]{\raisebox{0.1em}{$\big\downarrow$}#1} 238 \newcommand{\upsamplertext}[1]{\raisebox{0.1em}{$\big\uparrow$}#1}