summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/bodeplot/bodeplot.dtx
blob: a5cdfbf6db81ab7531540c3e8f36af9ec1a01fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
% \iffalse meta-comment
%
% Copyright (C) 2021 by Rushikesh Kamalapurkar <rlkamalapurkar@gmail.com>
% -----------------------------------------------------------
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3c of this license
% or (at your option) any later version. The latest version of this
% license is in:
%
% http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2006/05/20 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2006/05/20]
%<package>\ProvidesPackage{bodeplot}
%<package>\RequirePackage{pdftexcmds}
%<package>\RequirePackage{ifplatform}
%<package>\RequirePackage{pgfplots}
%<package>  \pgfplotsset{compat=1.18}
%<package>  \usepgfplotslibrary{groupplots}
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{bodeplot,cprotect}
\usepackage[colorlinks]{hyperref}
\usepackage{iftex}
  \iftutex % LuaTeX, XeTeX
    \usepackage{fontspec}
    \setmonofont{DejaVuSansMono}[Scale=MatchUppercase]
  \else % old engines
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage[scaled]{DejaVuSansMono}
  \fi
\usepackage{showexpl}
  \lstset{%
    explpreset={numbers=none},
    language=[LaTeX]Tex,
    basicstyle=\ttfamily\tiny,
    commentstyle=\itshape\ttfamily\tiny,
    showspaces=false,
    showstringspaces=false,
    breaklines=true,
    backgroundcolor=\color{white!90!black},
    breakautoindent=true,
    captionpos=t
  }
\usepackage{geometry}
  \geometry{lmargin=2in,rmargin=1in,tmargin=1in,bmargin=1in}
\usetikzlibrary{decorations.markings,arrows.meta,spy,backgrounds}
\usepackage[nottoc]{tocbibind}
\sloppy
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{bodeplot.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{1404}
%
% \changes{v1.0}{2021/10/25}{Initial release}
% \changes{v1.0.4}{2021/11/05}{Fixed unintended optional argument macro expansion}
% \changes{v1.0.6}{2021/11/18}{Fixed issue \#3}
% \changes{v1.0.7}{2021/12/02}{Removed unnecessary semicolons}
% \changes{v1.0.7}{2022/01/18}{Updated documentation}
% \changes{v1.0.8}{2022/07/06}{Added a new class option `declutter'}
% \changes{v1.1.0}{2022/07/06}{Fixed phase wrapping in gnuplot mode}
%
% \GetFileInfo{bodeplot.sty}
% \DoNotIndex{\newcommand,\xdef,\gdef,\def,\edef,\addplot,\approx,\arabic,\opt,\typ,\obj,\else,\if@pgfarg,\fi,\begin,\end,\feature,\footnotesize,\draw,\detokenize,\DeclareOption,\foreach,\ifdim,\ifodd,\Im,\Re,\let,\newif,\nextgroupplot,\noexpand,\expandafter,\unexpanded,\PackageError,\PackageWarning,\relax,\RequirePackage,\tikzset,\pgfmathsetmacro,\pgfmathtruncatemacro,\ProcessOptions}
%
% \title{The \textsf{bodeplot} package\thanks{This document corresponds to \textsf{bodeplot}~v1.1.0, dated July 20, 2022.}}
% \author{Rushikesh Kamalapurkar \\ \texttt{rlkamalapurkar@gmail.com}}
%
% \maketitle
% \tableofcontents
% \clearpage
% \section{Introduction}
%
% Generate Bode, Nyquist, and Nichols plots for transfer functions in the canonical (TF) form \begin{equation}G(s) = e^{-Ts}\frac{b_ms^m+\cdots+b_1s+b_0}{a_ns^n+\cdots+a_1s+a_0}\label{eq:TF}\end{equation} and the zero-pole-gain (ZPK) form \begin{equation}G(s) = Ke^{-Ts}\frac{(s-z_1)(s-z_2)\cdots(s-z_m)}{(s-p_1)(s-p_2)\cdots(s-p_n)}.\label{eq:ZPK}\end{equation} In the equations above, $b_m,\cdots,b_0$ and $a_n,\cdots,a_0$ are real coefficients, $T\geq 0$ is the loop delay, $z_1,\cdots,z_m$ and $p_1,\cdots,p_n$ are complex zeros and poles of the transfer function, respectively, and $K\in \Re$ is the loop gain. For transfer functions in the ZPK format in (\ref{eq:ZPK}) \emph{with zero delay}, this package also supports linear and asymptotic approximation of Bode plots. By default, all phase plots use degrees as units. Use the |rad| package option to generate plots in radians.
% \subsection{External Dependencies}
% By default, the package uses |gnuplot| to do all the computations. If |gnuplot| is not available, the |pgf| package option can be used to do the calculations using the native |pgf| math engine. Compilation using the |pgf| math engine is typically slower, but the end result should be the identical (other than phase wrapping in the TF format, see limitations below).
%\subsection{Directory Structure}
% Since version 1.0.8, the |bodeplot| package places all |gnuplot| temporary files in the working directory. The package option |declutter| restores the original behavior where the temporary files are placed in a folder called |gnuplot|.
% \subsection{Limitations} 
% \begin{itemize}
%   \item When plotting Nichols charts in TF form, the phase angles are wrapped between 0 and 360$^\circ$. As such, the Nichols charts will have phase wrapping discontinuities. Phase wrapping in Bode plots was fixed in v1.1.0 using |gnuplot|. In |pgf| mode, Bode phase plots, plotted using the TF form, will also show phase wrapping discontinuities.
%   \item Use of the |declutter| option with other directory management tools such as a |tikzexternalize| prefix is not recommended.
% \end{itemize}
% \clearpage
% \section{TL;DR}
% All Bode plots in this section are for the transfer function (with and without a transport delay)
% \begin{equation}
%   G(s) = 10\frac{s(s+0.1+0.5\mathrm{i})(s+0.1-0.5\mathrm{i})}{(s+0.5+10\mathrm{i})(s+0.5-10\mathrm{i})} = \frac{s(10s^2+2s+2.6)}{(s^2+s+100.25)}.
% \end{equation}
% \iffalse
%<*ignore>
% \fi

\hrulefill

{\centering Bode plot in ZPK format
\begin{LTXexample}[pos=l,hsep=20pt,width=0.5\textwidth]
\BodeZPK{%
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/10
}
{0.01}
{100}
\end{LTXexample}

\hrulefill

Bode plot in TF format with arrow decoration, transport delay, and color customization (the phase plot will show wrapping if the |pgf| package option is used)
\begin{LTXexample}[pos=r,width=0.5\textwidth]
\BodeTF[%
  samples=1000,
  plot/mag/{blue,thick},
  plot/ph/{green,thick},
  tikz/{>=latex},
  commands/mag/{
    \draw[->](axis cs:1,40) -- (axis cs:10,60);
    \node at (axis cs: 0.8,30) {\tiny Resonant Peak};
  }%
]
{num/{10,2,2.6,0},den/{1,1,100.25},d/0.01}
{0.01}
{500}
\end{LTXexample}

\hrulefill
\clearpage
\hrulefill

Linear approximation with customization
\begin{LTXexample}[pos=l,hsep=20pt,width=0.5\textwidth]
\BodeZPK[%
  plot/mag/{red,thick},
  plot/ph/{blue,thick},
  axes/mag/{ytick distance=40},
  axes/ph/{ytick distance=90},
  approx/linear%
]{%
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/10
}
{0.01}
{100}
\end{LTXexample}

\hrulefill

Plot with delay and customization
\begin{LTXexample}[pos=r,hsep=20pt,width=0.5\textwidth]
\BodeZPK[
  plot/mag/{blue,thick},
  plot/ph/{green,thick},
  axes/mag/ytick distance=40,
  axes/ph/ytick distance=90
]{%
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/10,
  d/0.01
}
{0.01}
{100}
\end{LTXexample}

\hrulefill
\clearpage
\hrulefill

Individual gain and phase plots with more customization

\begin{minipage}[t]{0.45\textwidth}
\begin{LTXexample}[pos=t,width=\columnwidth]
\begin{BodeMagPlot}[%
  axes/{height=2cm,
  width=4cm}
]
{0.01}
{100}
  \addBodeZPKPlots[%
    true/{black,thick},
    linear/{red,dashed,thick},
    asymptotic/{blue,dotted,thick}%
  ]
  {magnitude}
  {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/10
  }
\end{BodeMagPlot}
\end{LTXexample}
\end{minipage}\hfill
\begin{minipage}[t]{0.45\textwidth}
\begin{LTXexample}[pos=t,width=\columnwidth]
\begin{BodePhPlot}[%
  height=2cm,
  width=4cm,
  ytick distance=90
]
{0.01}
{100}
  \addBodeZPKPlots[%
    true/{black,thick},
    linear/{red,dashed,thick},
    asymptotic/{blue,dotted,thick}%
  ]
  {phase}
  {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/10
  }
\end{BodePhPlot}
\end{LTXexample}
\end{minipage}

\hrulefill

Nichols chart
\begin{LTXexample}[pos=l,hsep=20pt,width=0.5\textwidth]
\NicholsZPK[samples=1000]
{%
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/10,
  d/0.01
}
{0.001}
{500}
\end{LTXexample}

\hrulefill

Same Nichols chart in TF format (shows phase wrapping discontinuity)
\begin{LTXexample}[pos=r,hsep=20pt,width=0.5\textwidth]
\NicholsTF[samples=1000]
{num/{10,2,2.6,0},den/{1,1,100.25},d/0.01}
{0.001}
{500}
\end{LTXexample}

\hrulefill
\clearpage
\hrulefill

Multiple Nichols charts with customization
\begin{LTXexample}[pos=l,hsep=20pt,width=0.5\textwidth]
\begin{NicholsChart}[%
  ytick distance=20,
  xtick distance=30
]
{0.001}
{100}
  \addNicholsZPKChart [red,samples=1000] {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/10
  };
  \addNicholsZPKChart [blue,samples=1000] {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/5
  };
\end{NicholsChart}
\end{LTXexample}

\hrulefill

Nyquist plot
\begin{LTXexample}[pos=r,hsep=20pt,width=0.5\textwidth]
\NyquistZPK[plot/{red,thick,samples=1000}]
{%
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/10
}
{-30}
{30}
\end{LTXexample}

\hrulefill

Nyquist plot in TF format with arrows
\begin{LTXexample}[pos=l,width=0.5\textwidth]
\NyquistTF[%
  plot/{%
    samples=1000,
    postaction=decorate,
    decoration={%
      markings,
      mark=between positions 0.1 and 0.9 step 5em with {%
        \arrow{Stealth [length=2mm, blue]}
      }
    }
  }%
]
{num/{10,2,2.6,0},den/{1,1,100.25}}
{-30}
{30}
\end{LTXexample}

\hrulefill
\clearpage
\hrulefill

Multiple Nyquist plots with customization
\begin{LTXexample}[pos=r,hsep=20pt,width=0.5\textwidth]
\begin{NyquistPlot}{-30}{30}
  \addNyquistZPKPlot [red,samples=1000] {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/10
  };
  \addNyquistZPKPlot [blue,samples=1000] {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/5
  };
\end{NyquistPlot}
\end{LTXexample}

\hrulefill

Nyquist plots with additional commands, using two different macros

\begin{minipage}[t]{0.48\textwidth}
\begin{LTXexample}[pos=t,width=\columnwidth]
\begin{NyquistPlot}[%
  tikz/{%
    spy using outlines={%
      circle,
      magnification=3, 
      connect spies,
      size=2cm
    }%
  }%
]
{-30}{30}
  \addNyquistZPKPlot [blue,samples=1000] {%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/0.5
  };
  \coordinate (spyon) at (axis cs:0,0);
  \coordinate (spyat) at (axis cs:-22,5);
  \spy [green] on (spyon) in 
    node [fill=white] at (spyat);
\end{NyquistPlot}
\end{LTXexample}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\textwidth}
\begin{LTXexample}[pos=t,width=\columnwidth]
\NyquistZPK[%
  plot/{blue,samples=1000},
  tikz/{%
    spy using outlines={%
      circle,
      magnification=3, 
      connect spies,
      size=2cm
    }%
  },
  commands/{%
    \coordinate (spyon) at (axis cs:0,0);
    \coordinate (spyat) at (axis cs:-22,5);
    \spy [green] on (spyon) in 
      node [fill=white] at (spyat);
  }%
]%
{
  z/{0,{-0.1,-0.5},{-0.1,0.5}},
  p/{{-0.5,-10},{-0.5,10}},
  k/0.5
}
{-30}
{30}
\end{LTXexample}
\end{minipage}}

\hrulefill
\clearpage

% \iffalse
%</ignore>
% \fi
% 
% \section{Usage}
% \subsection{Bode plots}
% \DescribeMacro{\BodeZPK}
% |\BodeZPK| \oarg{obj1/typ1/\marg{opt1},obj2/typ2/\marg{opt2},...}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq}
%
% \noindent Plots the Bode plot of a transfer function given in ZPK format using the |groupplot| environment. The three mandatory arguments include: (1) a list of tuples, comprised of the zeros, the poles, the gain, and the transport delay of the transfer function, (2) the lower end of the frequency range for the $x-$axis, and (3) the higher end of the frequency range for the $x-$axis. The zeros and the poles are complex numbers, entered as a comma-separated list of comma-separated lists, of the form |{{real part 1,imaginary part 1},| |{real part 2,imaginary part 2},...}|. If the imaginary part is not provided, it is assumed to be zero.
%
% The optional argument is comprised of a comma separated list of tuples, either |obj/typ/{opt}|, or |obj/{opt}|, or just |{opt}|. Each tuple passes options to different |pgfplots| macros that generate the group, the axes, and the plots according to:
% \begin{itemize}
% \item Tuples of the form |obj/typ/{opt}|:
% \begin{itemize}
%  \item |plot/typ/{opt}|: modify plot properties by adding options |{opt}| to the |\addplot| macro for the magnitude plot if |typ| is |mag| and the phase plot if |typ| is |ph|.
%  \item |axes/typ/{opt}|: modify axis properties by adding options |{opt}| to the |\nextgroupplot| macro for the magnitude plot if |typ| is |mag| and the phase plot if |typ| is |ph|.
%  \item |commands/typ/{opt}|: add any valid TikZ commands (including the the parametric function generator macros in this package, such as |\addBodeZPKPlots|, |\addBodeTFPlot|, and |\addBodeComponentPlot|) to the magnitude plot if |typ| is |mag| and the phase plot if |typ| is |ph|. The commands passed to |opt| need to be valid TikZ commands, separated by semicolons as usual. For example, a TikZ command is used in the description of the |\BodeTF| macro below to mark the gain crossover frequency on the Bode Magnitude plot. 
% \end{itemize}
% \item Tuples of the form |obj/{opt}|:
% \begin{itemize}
%  \item |plot/{opt}|: adds options |{opt}| to |\addplot| macros for both the magnitude and the phase plots.
%  \item |axes/{opt}|: adds options |{opt}| to |\nextgroupplot| macros for both the magnitude and the phase plots.
%  \item |group/{opt}|: adds options |{opt}| to the |groupplot| environment.
%  \item |tikz/{opt}|: adds options |{opt}| to the |tikzpicture| environment.
%  \item |approx/linear|: plots linear approximation.
%  \item |approx/asymptotic|: plots asymptotic approximation.
% \end{itemize}
% \item Tuples of the form |{opt}| add all of the supplied options to |\addplot| macros for both the magnitude and the phase plots.
% \end{itemize}
% The options |{opt}| can be any |key=value| options that are supported by the |pgfplots| macros they are added to.

% For example, given a transfer function \begin{equation}G(s) = 10\frac{s(s+0.1+0.5\mathrm{i})(s+0.1-0.5\mathrm{i})}{(s+0.5+10\mathrm{i})(s+0.5-10\mathrm{i})},\label{eq:ZPKExample}\end{equation} its Bode plot over the frequency range $[0.01,100]$ can be generated using\\
% |\BodeZPK [blue,thick]|\\
% |  {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}|\\
% |  {0.01}{100}|\\
% which generates the plot in Figure \ref{simpleBode}. If a delay is not specified, it is assumed to be zero. If a gain is not specified, it is assumed to be 1. By default, each of the axes, excluding ticks and labels, are 5cm wide and 2.5cm high. The width and the height, along with other properties of the plots, the axes, and the group can be customized using native |pgf| keys as shown in the example below.
%
% \begin{figure}
% \begin{center}
% \BodeZPK[blue,thick]{z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}{0.01}{100}
% \cprotect\caption{\label{simpleBode}Output of the default |\BodeZPK| macro.}
% \end{center}
% \end{figure}
%  As demonstrated in this example, if a single comma-separated list of options is passed, it applies to both the magnitude and the phase plots. Without any optional arguments, we gets a thick black Bode plot.
%
% A linear approximation of the Bode plot with customization of the plots, the axes, and the group can be generated using\\
% |\BodeZPK[plot/mag/{red,thick},plot/ph/{blue,thick},|\\
% |  axes/mag/{ytick distance=40,xmajorticks=true,|\\
% |  xlabel={Frequency (rad/s)}},axes/ph/{ytick distance=90},|\\
% |  group/{group style={group size=2 by 1,horizontal sep=2cm,|\\
% |  width=4cm,height=2cm}},approx/linear]|\\
% |  {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}|\\
% |  {0.01}{100}|\\
% which generates the plot in Figure \ref{customBode}.
%
% \begin{figure}
% \begin{center}
% \BodeZPK[plot/mag/{red,thick},plot/ph/{blue,thick},axes/mag/{ytick distance=40,xmajorticks=true,xlabel={Frequency (rad/s)}},axes/ph/{ytick distance=90},approx/linear,group/{group style={group size = 2 by 1,horizontal sep=2cm},width=4cm,height=2cm}] {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},g/10} {0.01} {100}
% \cprotect\caption{\label{customBode}Customization of the default |\BodeZPK| macro.}
% \end{center}
% \end{figure}
%
% \DescribeMacro{\BodeTF}
% |\BodeTF| \oarg{obj1/typ1/\marg{opt1},obj2/typ2/\marg{opt2},...}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq} \rmfamily
%
% \noindent Plots the Bode plot of a transfer function given in TF format. The three mandatory arguments include: (1) a list of tuples comprised of the coefficients in the numerator and the denominator of the transfer function and the transport delay, (2) the lower end of the frequency range for the $x-$ axis, and (3) the higher end of the frequency range for the $x-$axis. The coefficients are entered as a comma-separated list, in order from the highest degree of $s$ to the lowest, with zeros for missing degrees. The optional arguments are the same as |\BodeZPK|, except that linear/asymptotic approximation is not supported, so |approx/...| is ignored.
%
% For example, given the same transfer function as (\ref{eq:ZPKExample}) in TF form and with a small transport delay, \begin{equation}G(s) = e^{-0.01s}\frac{s(10s^2+2s+2.6)}{(s^2+s+100.25)},\label{eq:TFExample}\end{equation} its Bode plot over the frequency range $[0.01,100]$ can be generated using\\
% |\BodeTF[commands/mag/{\node at (axis cs: 2.1,0) |\\
% |  [circle,fill,inner sep=0.05cm,label=below:{$\omega_{gc}$}]{};}]|\\
% |  {num/{10,2,2.6,0},den/{1,1,100.25},d/0.01}|\\
% |  {0.01}{100}|\\
% which generates the plot in Figure \ref{simpleBodeTF}. Note the $0$ added to the numerator coefficients to account for the fact that the numerator does not have a constant term in it. Note the semicolon after the TikZ command passed to the |\commands| option.
%
% \begin{figure}
% \begin{center}
% \BodeTF[commands/mag/{\node at (axis cs: 2.1,0) [circle,fill,inner sep=0.05cm,label=below:{$\omega_{gc}$}] {};}]{num/{10,2,2.6,0},den/{1,1,100.25},d/0.01}{0.01}{100}
% \cprotect\caption{\label{simpleBodeTF}Output of the |\BodeTF| macro with an optional TikZ command used to mark the gain crossover frequency.}
% \end{center}
% \end{figure}
%
% \DescribeEnv{BodeMagPlot}
% |\begin{BodeMagPlot}|\oarg{obj1/\marg{opt1},obj2/\marg{opt2},...}\\
% \hspace*{3em}\marg{min-frequency}\marg{max-frequency}\\
% \hspace*{2em}|\addBode...|\\
% \hspace*{1.5em}|\end{BodeMagPlot}|\\
% The |BodeMagPlot| environment works in conjunction with the parametric function generator macros |\addBodeZPKPlots|, |\addBodeTFPlot|, and |\addBodeComponentPlot|, intended to be used for magnitude plots. The optional argument is comprised of a comma separated list of tuples, either |obj/{opt}| or just |{opt}|. Each tuple passes options to different |pgfplots| macros that generate the axes and the plots according to:
% \begin{itemize}
%  \item Tuples of the form |obj/{opt}|:
%   \begin{itemize}
%    \item |tikz/{opt}|: modify picture properties by adding options |{opt}| to the |tikzpicture| environment.
%    \item |axes/{opt}|: modify axis properties by adding options |{opt}| to the |semilogaxis| environment.
%    \item |commands/{opt}|: add any valid TikZ commands inside |semilogaxis| environment. The commands passed to |opt| need to be valid TikZ commands, separated by semicolons as usual.
%   \end{itemize}
%  \item Tuples of the form |{opt}| are passed directly to the |semilogaxis| environment.
% \end{itemize}
% The frequency limits are translated to the x-axis limits and the domain of the |semilogaxis| environment. Example usage in the description of |\addBodeZPKPlots|, |\addBodeTFPlot|, and |\addBodeComponentPlot|.
%
%\DescribeEnv{BodePhPlot}
% |\begin{BodePhPlot}|\oarg{obj1/\marg{opt1},obj2/\marg{opt2},...}\\
	% \hspace*{3em}\marg{min-frequency}\marg{max-frequency}\\
	% \hspace*{2em}|\addBode...|\\
	% \hspace*{1.5em}|\end{BodePhPlot}|\\
% Intended to be used for phase plots, otherwise same as the |BodeMagPlot| environment
%
% \DescribeMacro{\addBodeZPKPlots}
% |\addBodeZPKPlots| \oarg{approx1/\marg{opt1},approx2/\marg{opt2},...}\\
% \hspace*{2em}\marg{plot-type}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}
%
% \noindent Generates the appropriate parametric functions and supplies them to multiple |\addplot| macros, one for each |approx/{opt}| pair in the optional argument. If no optional argument is supplied, then a single |\addplot| command corresponding to a thick true Bode plot is generated. If an optional argument is supplied, it needs to be one of |true/{opt}|, |linear/{opt}|, or |asymptotic/{opt}|. This macro can be used inside any |semilogaxis| environment as long as a domain for the x-axis is supplied through either the |approx/{opt}| interface or directly in the optional argument of the |semilogaxis| environment. Use with the |BodePlot| environment supplied with this package is recommended. The second mandatory argument, |plot-type| is either |magnitude| or |phase|. If it is not equal to |phase|, it is assumed to be |magnitude|. The last mandatory argument is the same as |\BodeZPK|.
%
% For example, given the transfer function in (\ref{eq:ZPKExample}), its linear, asymptotic, and true Bode plots can be superimposed using
%\begin{verbatim}
%\begin{BodeMagPlot}[height=2cm,width=4cm] {0.01} {100}
%  \addBodeZPKPlots[
%    true/{black,thick},
%    linear/{red,dashed,thick},
%    asymptotic/{blue,dotted,thick}]
%    {magnitude}
%    {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}
%\end{BodeMagPlot}
%
%\begin{BodePhPlot}[height=2cm, width=4cm, ytick distance=90] {0.01} {100}
%  \addBodeZPKPlots[
%    true/{black,thick},
%    linear/{red,dashed,thick},
%    asymptotic/{blue,dotted,thick}]
%    {phase}
%    {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}
%\end{BodePhPlot}
%\end{verbatim}
% \begin{figure}
%  \begin{center}
%    \begin{BodeMagPlot}[height=2cm,width=4cm]{0.01}{100}
%      \addBodeZPKPlots[
%        true/{black,thick},
%        linear/{red,dashed,thick},
%        asymptotic/{blue,dotted,thick}]
%      {magnitude}
%      {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}
%    \end{BodeMagPlot}
%    \begin{BodePhPlot}[height=2cm,width=4cm,ytick distance=90]{0.01}{100}
%      \addBodeZPKPlots[
%        true/{black,thick},
%        linear/{red,dashed,thick},
%        asymptotic/{blue,dotted,thick}]
%      {phase}
%      {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}
%    \end{BodePhPlot}
%  \end{center}
%  \caption{\label{multiBodeZPK}Superimposed approximate and true Bode plots using the \texttt{BodeMagPlot} and \texttt{BodePhPlot} environments and the \texttt{\textbackslash addBodeZPKPlots} macro.}
% \end{figure}
% which generates the plot in Figure \ref{multiBodeZPK}.
%
% \DescribeMacro{\addBodeTFPlot}
% |\addBodeTFPlot|\oarg{plot-options}\\
% \hspace*{2em}\marg{plot-type}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}
%
% \noindent Generates a single parametric function for either Bode magnitude or phase plot of a transfer function in TF form. The generated parametric function is passed to the |\addplot| macro. This macro can be used inside any |semilogaxis| environment as long as a domain for the x-axis is supplied through either the |plot-options| interface or directly in the optional argument of the container |semilogaxis| environment. Use with the |BodePlot| environment supplied with this package is recommended. The second mandatory argument, |plot-type| is either magnitude or |phase|. If it is not equal to |phase|, it is assumed to be |magnitude|. The last mandatory argument is the same as |\BodeTF|.
%
% \DescribeMacro{\addBodeComponentPlot}
% |\addBodeComponentPlot|\oarg{plot-options}\marg{plot-command}
%
% \noindent Generates a single parametric function corresponding to the mandatory argument |plot-command| and passes it to the |\addplot| macro. The plot command can be any parametric function that uses |t| as the independent variable. The parametric function must be |gnuplot| compatible (or |pgfplots| compatible if the package is loaded using the |pgf| option). The intended use of this macro is to plot the parametric functions generated using the basic component macros described in Section \ref{sec:BasicComponents} below.
%
% \subsubsection{Basic components up to first order\label{sec:BasicComponents}}
%
% \DescribeMacro{\TypeFeatureApprox}
% |\TypeFeatureApprox|\marg{real-part}\marg{imaginary-part}
%
% \noindent This entry describes 20 different macros of the form |\TypeFeatureApprox| that take the real part and the imaginary part of a complex number as arguments. The |Type| in the macro name should be replaced by either |Mag| or |Ph| to generate a parametric function corresponding to the magnitude or the phase plot, respectively. The |Feature| in the macro name should be replaced by one of |K|, |Pole|, |Zero|, or |Del|, to generate the Bode plot of a gain, a complex pole, a complex zero, or a transport delay, respectively. If the |Feature| is set to either |K| or |Del|, the |imaginary-part| mandatory argument is ignored. The |Approx| in the macro name should either be removed, or it should be replaced by |Lin| or |Asymp| to generate the true Bode plot, the linear approximation, or the asymptotic approximation, respectively. If the |Feature| is set to |Del|, then |Approx| has to be removed. For example, 
% \begin{itemize}
% \item |\MagK{k}{0}| or |\MagK{k}{400}| generates a parametric function for the true Bode magnitude of $ G(s) = k $
% \item |\PhPoleLin{a}{b}| generates a parametric function for the linear approximation of the Bode phase of $ G(s) = \frac{1}{s-a-\mathrm{i}b} $.
% \item |\PhDel{T}{200}| or |\PhDel{T}{0}| generates a parametric function for the Bode phase of $ G(s) = e^{-Ts} $.
% \end{itemize}
% All 20 of the macros defined by combinations of |Type|, |Feature|, and |Approx|, and any |gnuplot| (or |pgfplot| if the |pgf| class option is loaded) compatible function of the 20 macros can be used as |plot-command| in the |addBodeComponentPlot| macro. This is sufficient to generate the Bode plot of any rational transfer function with delay. For example, the Bode phase plot in Figure \ref{multiBodeZPK} can also be generated using:
%\begin{verbatim}
%\begin{BodePhPlot}[ytick distance=90]{0.01}{100}
%  \addBodeComponentPlot[black,thick]{\PhZero{0}{0} + \PhZero{-0.1}{-0.5} +
%    \PhZero{-0.1}{0.5} + \PhPole{-0.5}{-10} + \PhPole{-0.5}{10} +
%    \PhK{10}{0}}
%  \addBodeComponentPlot[red,dashed,thick] {\PhZeroLin{0}{0} + 
%    \PhZeroLin{-0.1}{-0.5} + \PhZeroLin{-0.1}{0.5} +
%    \PhPoleLin{-0.5}{-10} + \PhPoleLin{-0.5}{10} + \PhKLin{10}{20}}
%  \addBodeComponentPlot[blue,dotted,thick] {\PhZeroAsymp{0}{0} + 
%    \PhZeroAsymp{-0.1}{-0.5} + \PhZeroAsymp{-0.1}{0.5} + 
%    \PhPoleAsymp{-0.5}{-10} + \PhPoleAsymp{-0.5}{10} + \PhKAsymp{10}{40}}
%\end{BodePhPlot}
%\end{verbatim}
%\begin{figure}
%  \begin{center}
%    \begin{BodePhPlot}[ytick distance=90]{0.01}{100}
%      \addBodeComponentPlot[black,thick] {\PhZero{0}{0} + \PhZero{-0.1}{-0.5} + \PhZero{-0.1}{0.5} + \PhPole{-0.5}{-10} + \PhPole{-0.5}{10} + \PhK{10}{0}}
%      \addBodeComponentPlot[red,dashed,thick] {\PhZeroLin{0}{0} + \PhZeroLin{-0.1}{-0.5} + \PhZeroLin{-0.1}{0.5} + \PhPoleLin{-0.5}{-10} + \PhPoleLin{-0.5}{10} + \PhKLin{10}{20}}
%      \addBodeComponentPlot[blue,dotted,thick] {\PhZeroAsymp{0}{0} + \PhZeroAsymp{-0.1}{-0.5} + \PhZeroAsymp{-0.1}{0.5} + \PhPoleAsymp{-0.5}{-10} + \PhPoleAsymp{-0.5}{10} + \PhKAsymp{10}{40}}
%    \end{BodePhPlot}
%  \end{center}
%  \caption{\label{multiBodeComponents}Superimposed approximate and true Bode Phase plot using the \texttt{BodePhPlot} environment, the \texttt{\textbackslash addBodeComponentPlot} macro, and several macros of the \texttt{\textbackslash TypeFeatureApprox} form.}
%\end{figure}
% which gives us the plot in Figure \ref{multiBodeComponents}.
%
% \subsubsection{Basic components of the second order}
%
% \DescribeMacro{\TypeSOFeatureApprox}
% |\TypeSOFeatureApprox|\marg{a1}\marg{a0}
%
% \noindent This entry describes 12 different macros of the form |\TypeSOFeatureApprox| that take the coefficients $ a_1 $ and $ a_0 $ of a general second order system as inputs. The |Feature| in the macro name should be replaced by either |Poles| or |Zeros| to generate the Bode plot of $G(s)=\frac{1}{s^2+a_1 s+a_0}$ or $G(s)=s^2+a_1 s+a_0$, respectively. The |Type| in the macro name should be replaced by either |Mag| or |Ph| to generate a parametric function corresponding to the magnitude or the phase plot, respectively. The |Approx| in the macro name should either be removed, or it should be replaced by |Lin| or |Asymp| to generate the true Bode plot, the linear approximation, or the asymptotic approximation, respectively.
% 
% \DescribeMacro{\MagSOFeaturePeak}
% |\MagSOFeaturePeak|\oarg{draw-options}\marg{a1}\marg{a0}
%
% \noindent This entry describes 2 different macros of the form |\MagSOFeaturePeak| that take the the coefficients $ a_1 $ and $ a_0 $ of a general second order system as inputs, and draw a resonant peak using the |\draw| TikZ macro. The |Feature| in the macro name should be replaced by either |Poles| or |Zeros| to generate a peak for poles and a valley for zeros, respectively.  For example, the command
%\begin{verbatim}
%\begin{BodeMagPlot}[xlabel={}]{0.1}{10}
%  \addBodeComponentPlot[red,dashed,thick]{\MagSOPoles{0.2}{1}}
%  \addBodeComponentPlot[black,thick]{\MagSOPolesLin{0.2}{1}}
%  \MagSOPolesPeak[thick]{0.2}{1}
%\end{BodeMagPlot}
%\end{verbatim}
% generates the plot in Figure \ref{BodePeak}.
%
% \begin{figure}
% \begin{center}
%  \begin{BodeMagPlot}[xlabel={}]{0.1}{10}
%    \addBodeComponentPlot[red,dashed,thick]{\MagSOPoles{0.2}{1}}
%    \addBodeComponentPlot[black,thick]{\MagSOPolesLin{0.2}{1}}
%    \MagSOPolesPeak[very thick]{0.2}{1}
%  \end{BodeMagPlot}
% \end{center}
% \cprotect\caption{\label{BodePeak} Resonant peak in asymptotic Bode plot using |\MagSOPolesPeak|.}
% \end{figure}
%
% \DescribeMacro{\TypeCSFeatureApprox}
% |\TypeCSFeatureApprox|\marg{zeta}\marg{omega-n}
%
% \noindent This entry describes 12 different macros of the form |\TypeCSFeatureApprox| that take the damping ratio, $ \zeta $, and the natural frequency, $ \omega_n $ of a canonical second order system as inputs. The |Type| in the macro name should be replaced by either |Mag| or |Ph| to generate a parametric function corresponding to the magnitude or the phase plot, respectively. The |Feature| in the macro name should be replaced by either |Poles| or |Zeros| to generate the Bode plot of $G(s)=\frac{1}{s^2+2\zeta\omega_n s+\omega_n^2}$ or $G(s)=s^2+2\zeta\omega_n s+\omega_n^2$, respectively. The |Approx| in the macro name should either be removed, or it should be replaced by |Lin| or |Asymp| to generate the true Bode plot, the linear approximation, or the asymptotic approximation, respectively.
%
% \DescribeMacro{\MagCSFeaturePeak}
% |\MagCSFeaturePeak|\oarg{draw-options}\marg{zeta}\marg{omega-n}
%
% \noindent This entry describes 2 different macros of the form |\MagCSFeaturePeak| that take the damping ratio, $ \zeta $, and the natural frequency, $ \omega_n $ of a canonical second order system as inputs, and draw a resonant peak using the |\draw| TikZ macro. The |Feature| in the macro name should be replaced by either |Poles| or |Zeros| to generate a peak for poles and a valley for zeros, respectively.
%
% \DescribeMacro{\MagCCFeaturePeak}
% |\MagCCFeaturePeak|\oarg{draw-options}\marg{real-part}\marg{imaginary-part}
%
% \noindent This entry describes 2 different macros of the form |\MagCCFeaturePeak| that take the real and imaginary parts of a pair of complex conjugate poles or zeros as inputs, and draw a resonant peak using the |\draw| TikZ macro. The |Feature| in the macro name should be replaced by either |Poles| or |Zeros| to generate a peak for poles and a valley for zeros, respectively.
%
% \subsection{Nyquist plots}
% \DescribeMacro{\NyquistZPK}
% |\NyquistZPK| \oarg{plot/\marg{opt},axes/\marg{opt}}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq}
%
% \noindent Plots the Nyquist plot of a transfer function given in ZPK format with a thick red $+$ marking the critical point (-1,0). The mandatory arguments are the same as |\BodeZPK|. Since there is only one plot in a Nyquist diagram, the |\typ| specifier in the optional argument tuples is not needed. As such, the supported optional argument tuples are |plot/{opt}|, which passes |{opt}| to |\addplot|, |axes/{opt}|, which passes |{\opt}| to the |axis| environment, and |tikz/{opt}|, which passes |{\opt}| to the |tikzpicture| environment. Asymptotic/linear approximations are not supported in Nyquist plots. If just |{opt}| is provided as the optional argument, it is interpreted as |plot/{opt}|. Arrows to indicate the direction of increasing $\omega$ can be added by adding |\usetikzlibrary{decorations.markings}| and |\usetikzlibrary{arrows.meta}| to the preamble and then passing a tuple of the form\\
%|plot/{postaction=decorate,decoration={markings,|\\
%|  mark=between positions 0.1 and 0.9 step 5em with|\\
%|  {\arrow{Stealth| |[length=2mm, blue]}}}}|\\
%\textbf{Caution:} with a high number of samples, adding arrows in this way may cause the error message |! Dimension too big|.
%
% For example, the command\\
% |\NyquistZPK[plot/{red,thick,samples=2000},axes/{blue,thick}]|\\
% |  {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10}|\\
% |  {-30}{30}|\\
% generates the Nyquist plot in Figure \ref{simpleNyquistZPK}.
%
% \begin{figure}
% \begin{center}
% \NyquistZPK[plot/{red,thick,samples=2000},axes/{blue,thick}] {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10} {-30} {30}
% \cprotect\caption{\label{simpleNyquistZPK}Output of the |\NyquistZPK| macro.}
% \end{center}
% \end{figure}
%
% % \DescribeMacro{\NyquistTF}
% |\NyquistTF| \oarg{plot/\marg{opt},axes/\marg{opt}}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq}
%
% \noindent Nyquist plot of a transfer function given in TF format. Same mandatory arguments as |\BodeTF| and same optional arguments as |\NyquistZPK|. For example, the command\\
% |\NyquistTF[plot/{green,thick,samples=500,postaction=decorate,|\\
% |  decoration={markings,|\\
% |  mark=between positions 0.1 and 0.9 step 5em|\\
% |  with{\arrow{Stealth[length=2mm, blue]}}}}]|\\
% |  {num/{10,2,2.6,0},den/{1,1,100.25}}|\\
% |  {-30}{30}|\\
% generates the Nyquist plot in Figure \ref{simpleNyquistTF}.
%
% \begin{figure}
% \begin{center}
% \NyquistTF[plot/{green,thick,samples=500,postaction=decorate,decoration={markings,mark=between positions 0.1 and 0.9 step 5em with {\arrow{Stealth[length=2mm, blue]}}}}] {num/{10,2,2.6,0},den/{1,1,100.25},d/0.01} {-30} {30}
% \cprotect\caption{\label{simpleNyquistTF}Output of the |\NyquistTF| macro with direction arrows. Increasing the number of samples can cause |decorations.markings| to throw errors.}
% \end{center}
% \end{figure}
%
% \DescribeEnv{NyquistPlot}
% |\begin{NyquistPlot}|\oarg{obj1/\marg{opt1},obj2/\marg{opt2},...}\\
% \hspace*{3em}\marg{min-frequency}\marg{max-frequency}\\
% \hspace*{2em}|\addNyquist...|\\
% \hspace*{1.5em}|\end{NyquistPlot}|\\
% The |NyquistPlot| environment works in conjunction with the parametric function generator macros |\addNyquistZPKPlot| and |\addNyquistTFPlot|. The optional argument is comprised of a comma separated list of tuples, either |obj/{opt}| or just |{opt}|. Each tuple passes options to different |pgfplots| macros that generate the axes and the plots according to:
% \begin{itemize}
%  \item Tuples of the form |obj/{opt}|:
%   \begin{itemize}
%    \item |tikz/{opt}|: modify picture properties by adding options |{opt}| to the |tikzpicture| environment.
%    \item |axes/{opt}|: modify axis properties by adding options |{opt}| to the |axis| environment.
%    \item |commands/{opt}|: add any valid TikZ commands inside |axis| environment. The commands passed to |opt| need to be valid TikZ commands, separated by semicolons as usual.
%   \end{itemize}
%  \item Tuples of the form |{opt}| are passed directly to the |axis| environment.
% \end{itemize}
% The frequency limits are translated to the x-axis limits and the domain of the |axis| environment.
%
% \DescribeMacro{\addNyquistZPKPlot}
% |\addNyquistZPKPlot|\oarg{plot-options}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}
%
% \noindent Generates a twp parametric functions for the magnitude and the phase a transfer function in ZPK form. The generated magnitude and phase parametric functions are converted to real and imaginary part parametric functions and passed to the |\addplot| macro. This macro can be used inside any |axis| environment as long as a domain for the x-axis is supplied through either the |plot-options| interface or directly in the optional argument of the container |axis| environment. Use with the |NyquistPlot| environment supplied with this package is recommended. The mandatory argument is the same as |\BodeZPK|.
%
% \DescribeMacro{\addNyquistTFPlot}
% |\addNyquistTFPlot|\oarg{plot-options}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}
%
% \noindent Similar to |\addNyquistZPKPlot|, with a transfer function input in the TF form.
%
% \subsection{Nichols charts}
% \DescribeMacro{\NicholsZPK}
% |\NicholsZPK| \oarg{plot/\marg{opt},axes/\marg{opt}}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq}
%
% \noindent Nichols chart of a transfer function given in ZPK format. Same arguments as |\NyquistZPK|.
%
% \DescribeMacro{\NicholsTF}
% |\NicholsTF| \oarg{plot/\marg{opt},axes/\marg{opt}}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}\\
% \hspace*{2em}\marg{min-freq}\marg{max-freq}
%
% \noindent Nichols chart of a transfer function given in TF format. Same arguments as |\NyquistTF|. For example, the command\\
% |\NicholsTF[plot/{green,thick,samples=2000}]|\\
% |  {num/{10,2,2.6,0},den/{1,1,100.25},d/0.01}|\\
% |  {0.001}{100}|\\
% generates the Nichols chart in Figure \ref{simpleNicholsTF}.
%
% \begin{figure}
% \begin{center}
% \NicholsTF[plot/{green,thick,samples=2000}] {num/{10,2,2.6,0},den/{1,1,100.25},d/0.01} {0.001} {100}
% \cprotect\caption{\label{simpleNicholsTF}Output of the |\NyquistZPK| macro.}
% \end{center}
% \end{figure}
%
%
% \DescribeEnv{NicholsChart}
% |\begin{NicholsChart}|\oarg{obj1/\marg{opt1},obj2/\marg{opt2},...}\\
% \hspace*{3em}\marg{min-frequency}\marg{max-frequency}\\
% \hspace*{2em}|\addNichols...|\\
% \hspace*{1.5em}|\end{NicholsChart}|\\
% The |NicholsChart| environment works in conjunction with the parametric function generator macros |\addNicholsZPKChart| and |\addNicholsTFChart|. The optional argument is comprised of a comma separated list of tuples, either |obj/{opt}| or just |{opt}|. Each tuple passes options to different |pgfplots| macros that generate the axes and the plots according to:
% \begin{itemize}
%  \item Tuples of the form |obj/{opt}|:
%   \begin{itemize}
%    \item |tikz/{opt}|: modify picture properties by adding options |{opt}| to the |tikzpicture| environment.
%    \item |axes/{opt}|: modify axis properties by adding options |{opt}| to the |axis| environment.
%    \item |commands/{opt}|: add any valid TikZ commands inside |axis| environment. The commands passed to |opt| need to be valid TikZ commands, separated by semicolons as usual.
%   \end{itemize}
%  \item Tuples of the form |{opt}| are passed directly to the |axis| environment.
% \end{itemize}
% The frequency limits are translated to the x-axis limits and the domain of the |axis| environment.
%
% \DescribeMacro{\addNicholsZPKChart}
% |\addNicholsZPKChart|\oarg{plot-options}\\
% \hspace*{2em}\marg{z/\marg{zeros},p/\marg{poles},k/\marg{gain},d/\marg{delay}}
%
% \noindent Generates a twp parametric functions for the magnitude and the phase a transfer function in ZPK form. The generated magnitude and phase parametric functions are passed to the |\addplot| macro. This macro can be used inside any |axis| environment as long as a domain for the x-axis is supplied through either the |plot-options| interface or directly in the optional argument of the container |axis| environment. Use with the |NicholsChart| environment supplied with this package is recommended. The mandatory argument is the same as |\BodeZPK|.
%
% \DescribeMacro{\addNicholsTFChart}
% |\addNicholsTFChart|\oarg{plot-options}\\
% \hspace*{2em}\marg{num/\marg{coeffs},den/\marg{coeffs},d/\marg{delay}}
%
% \noindent Similar to |\addNicholsZPKChart|, with a transfer function input in the TF form.
%
% \StopEventually{\PrintIndex}
% \clearpage
% \section{Implementation}
% \subsection{Initialization}
% \begin{macro}{\pdfstrcmp}
% The package makes extensive use of the |\pdfstrcmp| macro to parse options. Since that macro is not available in |lualatex|, this code is needed.
%    \begin{macrocode}
\RequirePackage{ifluatex}%
\ifluatex
  \let\pdfstrcmp\pdf@strcmp
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\n@mod}
% \begin{macro}{\n@pow}
% \begin{macro}{gnuplot@id}
% \begin{macro}{gnuplot@prefix}
% \changes{v1.0.3}{2021/11/03}{Added jobname to gnuplot prefix}
% \changes{v1.0.8}{2022/07/06}{Fixed issue \#6}
% This code is needed to support both |pgfplots| and |gnuplot| simultaneously. New macros are defined for the |pow| and |mod| functions to address differences between the two math engines. We start by processing the class options. 
%    \begin{macrocode}
\newif\if@pgfarg\@pgfargfalse
\DeclareOption{pgf}{%
  \@pgfargtrue
}
\newif\if@declutterarg\@declutterargfalse
\DeclareOption{declutter}{%
  \@declutterargtrue
}
\newif\if@radarg\@radargfalse
\DeclareOption{rad}{%
  \@radargtrue
}
\ProcessOptions\relax
%    \end{macrocode}
% Then, we define two new macros to unify |pgfplots| and |gnuplot|.
%    \begin{macrocode}
\newcommand{\n@mod}[2]{(#1)-(floor((#1)/(#2))*(#2))}
\if@pgfarg
  \newcommand{\n@pow}[2]{(#1)^(#2)}
  \pgfplotsset{%
    trig format plots=rad%
  }
\else
  \newcommand{\n@pow}[2]{(#1)**(#2)}
%    \end{macrocode}
% Then, we create a counter so that a new data table is generated and for each new plot. If the plot macros have not changed, the tables, once generated, can be reused by |gnuplot|, which reduces compilation time. The |declutter| option is used to enable the |gnuplot| directory to declutter the working directory.
%    \begin{macrocode}
  \newcounter{gnuplot@id}
  \setcounter{gnuplot@id}{0}
  \if@declutterarg
    \tikzset{%
      gnuplot@prefix/.style={%
        id=\arabic{gnuplot@id},
        prefix=gnuplot/\jobname
      }%
    }
  \else
    \tikzset{%
      gnuplot@prefix/.style={%
        id=\arabic{gnuplot@id},
        prefix=\jobname
      }%
    }
  \fi
%    \end{macrocode}
% If the operating system is not Windows, and if the |declutter| option is not passed, we create the |gnuplot| folder if it does not already exist. \changes{v1.0.2}{2021/11/01}{Fixed issue \#1}
%    \begin{macrocode}
  \ifwindows\else
    \if@declutterarg
      \immediate\write18{mkdir -p gnuplot}
    \fi
  \fi
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{bode@style}
% Default axis properties for all plot macros are collected in this |pgf| style.
%    \begin{macrocode}
\pgfplotsset{%
  bode@style/.style = {%
    label style={font=\footnotesize},
    tick label style={font=\footnotesize},
    grid=both,
    major grid style={color=gray!80},
    minor grid style={color=gray!20},
    x label style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
    y label style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
    scale only axis,
    samples=200,
    width=5cm,
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{ph@filter}
% \begin{macro}{ph@x@filter}
% These macros create |pgf| filters to convert plots from radians to degrees.
%    \begin{macrocode}
\if@radarg
  \pgfplotsset{ph@filter/.style = {ytick distance=pi/4, ylabel={Phase (rad)}}}%
  \pgfplotsset{ph@x@filter/.style = {xlabel={Phase (rad)}}}%
\else
  \pgfplotsset{ph@filter/.style = {y filter/.expression={y*180/pi}, ytick distance=45, ylabel={Phase (deg)}}}%
  \pgfplotsset{ph@x@filter/.style = {x filter/.expression={x*180/pi}, xlabel={Phase (deg)}}}%
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \subsection{Parametric function generators for poles, zeros, gains, and delays.}
% \begin{macro}{\MagK}
% \begin{macro}{\MagKAsymp}
% \begin{macro}{\MagKLin}
% \begin{macro}{\PhK}
% \begin{macro}{\PhKAsymp}
% \begin{macro}{\PhKLin}
% True, linear, and asymptotic magnitude and phase parametric functions for a pure gain $G(s)=k+0\mathrm{i}$. The macros take two arguments corresponding to real and imaginary part of the gain to facilitate code reuse between delays, gains, poles, and zeros, but only real gains are supported. The second argument, if supplied, is ignored.
%    \begin{macrocode}
\newcommand*{\MagK}[2]{(20*log10(abs(#1)))}
\newcommand*{\MagKAsymp}{\MagK}
\newcommand*{\MagKLin}{\MagK}
\newcommand*{\PhK}[2]{(#1<0?-pi:0)}
\newcommand*{\PhKAsymp}{\PhK}
\newcommand*{\PhKLin}{\PhK}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\PhKAsymp}
% \begin{macro}{\PhKLin}
% True magnitude and phase parametric functions for a pure delay $G(s)=e^{-Ts}$. The macros take two arguments corresponding to real and imaginary part of the gain to facilitate code reuse between delays, gains, poles, and zeros, but only real gains are supported. The second argument, if supplied, is ignored.
%    \begin{macrocode}
\newcommand*{\MagDel}[2]{0}
\newcommand*{\PhDel}[2]{-#1*t}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\MagPole}
% \begin{macro}{\MagPoleAsymp}
% \begin{macro}{\MagPoleLin}
% \begin{macro}{\PhPole}
% \begin{macro}{\PhPoleAsymp}
% \begin{macro}{\PhPoleLin}
% These macros are the building blocks for most of the plotting functions provided by this package. We start with Parametric function for the true magnitude of a complex pole.
%    \begin{macrocode}
\newcommand*{\MagPole}[2]
  {(-20*log10(sqrt(\n@pow{#1}{2} + \n@pow{t - (#2)}{2})))}
%    \end{macrocode}
% Parametric function for linear approximation of the magnitude of a complex pole.
%    \begin{macrocode}
\newcommand*{\MagPoleLin}[2]{(t < sqrt(\n@pow{#1}{2} + \n@pow{#2}{2}) ?
  -20*log10(sqrt(\n@pow{#1}{2} + \n@pow{#2}{2})) :
  -20*log10(t)
  )}
%    \end{macrocode}
% Parametric function for asymptotic approximation of the magnitude of a complex pole, same as linear approximation.
%    \begin{macrocode}
\newcommand*{\MagPoleAsymp}{\MagPoleLin}
%    \end{macrocode}
% Parametric function for the true phase of a complex pole.
%    \begin{macrocode}
\newcommand*{\PhPole}[2]{(#1 > 0 ? (#2 > 0 ? 
  (\n@mod{-atan2((t - (#2)),-(#1))}{2*pi}) : 
  (-atan2((t - (#2)),-(#1)))) : 
  (-atan2((t - (#2)),-(#1))))}
%    \end{macrocode}
% Parametric function for linear approximation of the phase of a complex pole.
%    \begin{macrocode}
\newcommand*{\PhPoleLin}[2]{%
  (abs(#1)+abs(#2) == 0 ? -pi/2 :
  (t < (sqrt(\n@pow{#1}{2} + \n@pow{#2}{2}) /
    (\n@pow{10}{sqrt(\n@pow{#1}{2}/(\n@pow{#1}{2} + \n@pow{#2}{2}))})) ?
  (-atan2(-(#2),-(#1))) :
  (t >= (sqrt(\n@pow{#1}{2} + \n@pow{#2}{2}) *
    (\n@pow{10}{sqrt(\n@pow{#1}{2}/(\n@pow{#1}{2} + \n@pow{#2}{2}))})) ?
  (#2>0?(#1>0?3*pi/2:-pi/2):-pi/2) :
  (-atan2(-(#2),-(#1)) + (log10(t/(sqrt(\n@pow{#1}{2} + \n@pow{#2}{2}) /
    (\n@pow{10}{sqrt(\n@pow{#1}{2}/(\n@pow{#1}{2} +
    \n@pow{#2}{2}))}))))*((#2>0?(#1>0?3*pi/2:-pi/2):-pi/2) + atan2(-(#2),-(#1)))/
    (log10(\n@pow{10}{sqrt((4*\n@pow{#1}{2})/
    (\n@pow{#1}{2} + \n@pow{#2}{2}))}))))))}
%    \end{macrocode}
% Parametric function for asymptotic approximation of the phase of a complex pole.
%    \begin{macrocode}
\newcommand*{\PhPoleAsymp}[2]{(t < (sqrt(\n@pow{#1}{2} + \n@pow{#2}{2})) ? 
  (-atan2(-(#2),-(#1))) :
  (#2>0?(#1>0?3*pi/2:-pi/2):-pi/2))}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\MagZero}
% \begin{macro}{\MagZeroAsymp}
% \begin{macro}{\MagZeroLin}
% \begin{macro}{\PhZero}
% \begin{macro}{\PhZeroAsymp}
% \begin{macro}{\PhZeroLin}
% Plots of zeros are defined to be negative of plots of poles. The |0-| is necessary due to a bug in |gnuplot| (fixed in version 5.4, patchlevel 3).
%    \begin{macrocode}
\newcommand*{\MagZero}{0-\MagPole}
\newcommand*{\MagZeroLin}{0-\MagPoleLin}
\newcommand*{\MagZeroAsymp}{0-\MagPoleAsymp}
\newcommand*{\PhZero}{0-\PhPole}
\newcommand*{\PhZeroLin}{0-\PhPoleLin}
\newcommand*{\PhZeroAsymp}{0-\PhPoleAsymp}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \subsection{Second order systems.}
% Although second order systems can be dealt with using the macros defined so far, the following dedicated macros for second order systems involve less computation. 
% \begin{macro}{\MagCSPoles}
% \begin{macro}{\MagCSPolesAsymp}
% \begin{macro}{\MagCSPolesLin}
% \begin{macro}{\PhCSPoles}
% \begin{macro}{\PhCSPolesAsymp}
% \begin{macro}{\PhCSPolesLin}
% \begin{macro}{\MagCSZeros}
% \begin{macro}{\MagCSZerosAsymp}
% \begin{macro}{\MagCSZerosLin}
% \begin{macro}{\PhCSZeros}
% \begin{macro}{\PhCSZerosAsymp}
% \begin{macro}{\PhCSZerosLin}
% Consider the canonical second order transfer function $G(s) = \frac{1}{s^2 + 2 \zeta w_n s + w_n^2}$. We start with true, linear, and asymptotic magnitude plots for this transfer function.
%    \begin{macrocode}
\newcommand*{\MagCSPoles}[2]{(-20*log10(sqrt(\n@pow{\n@pow{#2}{2} 
    - \n@pow{t}{2}}{2} + \n@pow{2*#1*#2*t}{2})))}
\newcommand*{\MagCSPolesLin}[2]{(t < #2 ? -40*log10(#2) : - 40*log10(t))}
\newcommand*{\MagCSPolesAsymp}{\MagCSPolesLin}
%    \end{macrocode}
% Then, we have true, linear, and asymptotic phase plots for the canonical second order transfer function.
%    \begin{macrocode}
\newcommand*{\PhCSPoles}[2]{(-atan2((2*(#1)*(#2)*t),(\n@pow{#2}{2} 
  - \n@pow{t}{2})))}
\newcommand*{\PhCSPolesLin}[2]{(t < (#2 / (\n@pow{10}{abs(#1)})) ? 
  0 :
  (t >= (#2 * (\n@pow{10}{abs(#1)})) ? 
  (#1>0 ? -pi : pi) :
  (#1>0 ? (-pi*(log10(t*(\n@pow{10}{#1})/#2))/(2*#1)) : 
    (pi*(log10(t*(\n@pow{10}{abs(#1)})/#2))/(2*abs(#1))))))}
\newcommand*{\PhCSPolesAsymp}[2]{(#1>0?(t<#2?0:-pi):(t<#2?0:pi))}
%    \end{macrocode}
% Plots of the inverse function $G(s)=s^2+2\zeta\omega_n s+\omega_n^2$ are defined to be negative of plots of poles. The |0-| is necessary due to a bug in |gnuplot| (fixed in version 5.4, patchlevel 3).
%    \begin{macrocode}
\newcommand*{\MagCSZeros}{0-\MagCSPoles}
\newcommand*{\MagCSZerosLin}{0-\MagCSPolesLin}
\newcommand*{\MagCSZerosAsymp}{0-\MagCSPolesAsymp}
\newcommand*{\PhCSZeros}{0-\PhCSPoles}
\newcommand*{\PhCSZerosLin}{0-\PhCSPolesLin}
\newcommand*{\PhCSZerosAsymp}{0-\PhCSPolesAsymp}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\MagCSPolesPeak}
% \begin{macro}{\MagCSZerosPeak}
% These macros are used to add a resonant peak to linear and asymptotic plots of canonical second order poles and zeros. Since the plots are parametric, a separate |\draw| command is needed to add a vertical arrow.
%    \begin{macrocode}
\newcommand*{\MagCSPolesPeak}[3][]{%
  \draw[#1,->] (axis cs:{#3},{-40*log10(#3)}) -- 
  (axis cs:{#3},{-40*log10(#3)-20*log10(2*abs(#2))})
}
\newcommand*{\MagCSZerosPeak}[3][]{%
  \draw[#1,->] (axis cs:{#3},{40*log10(#3)}) -- 
  (axis cs:{#3},{40*log10(#3)+20*log10(2*abs(#2))})
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\MagSOPoles}
% \begin{macro}{\MagSOPolesAsymp}
% \begin{macro}{\MagSOPolesLin}
% \begin{macro}{\PhSOPoles}
% \begin{macro}{\PhSOPolesAsymp}
% \begin{macro}{\PhSOPolesLin}
% \begin{macro}{\MagSOZeros}
% \begin{macro}{\MagSOZerosAsymp}
% \begin{macro}{\MagSOZerosLin}
% \begin{macro}{\PhSOZeros}
% \begin{macro}{\PhSOZerosAsymp}
% \begin{macro}{\PhSOZerosLin}
% Consider a general second order transfer function $G(s) = \frac{1}{s^2 + a s + b}$. We start with true, linear, and asymptotic magnitude plots for this transfer function.
%    \begin{macrocode}
\newcommand*{\MagSOPoles}[2]{%
  (-20*log10(sqrt(\n@pow{#2 - \n@pow{t}{2}}{2} + \n@pow{#1*t}{2})))}
\newcommand*{\MagSOPolesLin}[2]{%
  (t < sqrt(abs(#2)) ? -20*log10(abs(#2)) : - 40*log10(t))}
\newcommand*{\MagSOPolesAsymp}{\MagSOPolesLin}
%    \end{macrocode}
% Then, we have true, linear, and asymptotic phase plots for the general second order transfer function.
%    \begin{macrocode}
\newcommand*{\PhSOPoles}[2]{(-atan2((#1)*t,((#2) - \n@pow{t}{2})))}
\newcommand*{\PhSOPolesLin}[2]{(#2>0 ?
  \PhCSPolesLin{(#1/(2*sqrt(#2)))}{(sqrt(#2))} :
  (#1>0 ? -pi : pi))}
\newcommand*{\PhSOPolesAsymp}[2]{(#2>0 ?
  \PhCSPolesAsymp{(#1/(2*sqrt(#2)))}{(sqrt(#2))} :
  (#1>0 ? -pi : pi))}
%    \end{macrocode}
% Plots of the inverse function $G(s)=s^2+as+b$ are defined to be negative of plots of poles. The |0-| is necessary due to a bug in |gnuplot| (fixed in version 5.4, patchlevel 3).
%    \begin{macrocode}
\newcommand*{\MagSOZeros}{0-\MagSOPoles}
\newcommand*{\MagSOZerosLin}{0-\MagSOPolesLin}
\newcommand*{\MagSOZerosAsymp}{0-\MagSOPolesAsymp}
\newcommand*{\PhSOZeros}{0-\PhSOPoles}
\newcommand*{\PhSOZerosLin}{0-\PhSOPolesLin}
\newcommand*{\PhSOZerosAsymp}{0-\PhSOPolesAsymp}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\MagSOPolesPeak}
% \begin{macro}{\MagSOZerosPeak}
% These macros are used to add a resonant peak to linear and asymptotic plots of general second order poles and zeros. Since the plots are parametric, a separate |\draw| command is needed to add a vertical arrow.
%    \begin{macrocode}
\newcommand*{\MagSOPolesPeak}[3][]{%
  \draw[#1,->] (axis cs:{sqrt(abs(#3))},{-20*log10(abs(#3))}) -- 
  (axis cs:{sqrt(abs(#3))},{-20*log10(abs(#3)) - 
    20*log10(abs(#2/sqrt(abs(#3))))});
}
\newcommand*{\MagSOZerosPeak}[3][]{%
  \draw[#1,->] (axis cs:{sqrt(abs(#3))},{20*log10(abs(#3))}) -- 
  (axis cs:{sqrt(abs(#3))},{20*log10(abs(#3)) + 
    20*log10(abs(#2/sqrt(abs(#3))))});
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \subsection{Commands for Bode plots}
% \subsubsection{User macros}
% \begin{macro}{\BodeZPK}
% This macro takes lists of complex poles and zeros of the form |{re,im}|, and values of gain and delay as inputs and constructs parametric functions for the Bode magnitude and phase plots. This is done by adding together the parametric functions generated by the macros for individual zeros, poles, gain, and delay, described above. The parametric functions are then plotted in a |tikzpicture| environment using the |\addplot| macro. Unless the package is loaded with the option |pgf|, the parametric functions are evaluated using |gnuplot|. \changes{v1.0.1}{2021/10/29}{Pass arbitrary TikZ commands as options.}
%    \begin{macrocode}
\newcommand{\BodeZPK}[4][approx/true]{%
%    \end{macrocode}
% Most of the work is done by the |\parse@opt| and the |\build@ZPK@plot| macros, described in the 'Internal macros' section. The former is used to parse the optional arguments and the latter to extract poles, zeros, gain, and delay from the first mandatory argument and to generate macros |\func@mag| and |\func@ph| that hold the magnitude and phase parametric functions.
%    \begin{macrocode}
  \parse@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@ZPK@plot{\func@mag}{\func@ph}{\opt@approx}{#2}%
%    \end{macrocode}
% The |\noexpand| macros below are needed to so that only the macro |\opt@group| is expanded. \changes{v1.0.3}{2021/11/03}{Added Tikz option}
%    \begin{macrocode}
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{groupplot}[%
      bode@style,
      xmin={#3},
      xmax={#4},
      domain=#3:#4,
      height=2.5cm,
      xmode=log,
      group style = {group size = 1 by 2,vertical sep=0.25cm},
      \opt@group
    ]%
  }%
  \temp@cmd
%    \end{macrocode}
% To ensure frequency tick marks on magnitude and the phase plots are always aligned, we use the |groupplot| library. The |\expandafter| chain below is used to expand macros in the plot and group optional arguments.
%    \begin{macrocode}
      \edef\temp@mag@cmd{\noexpand\nextgroupplot[ytick distance=20, ylabel={Gain (dB)}, xmajorticks=false, \optmag@axes]
      \noexpand\addplot[variable=t, thick, \optmag@plot]}
      \edef\temp@ph@cmd{\noexpand\nextgroupplot[ph@filter, xlabel={Frequency (rad/s)}, \optph@axes]
      \noexpand\addplot[variable=t, thick, \optph@plot]}
%    \end{macrocode}
% In |gnuplot| mode, we increment the |gnuplot@id| counter before every plot to make sure that new and reusable |.gnuplot| and |.table| files are generated for every plot.
%    \begin{macrocode}
      \if@pgfarg\else
        \edef\temp@mag@cmd{\noexpand\stepcounter{gnuplot@id} \unexpanded\expandafter{\temp@mag@cmd} gnuplot[gnuplot@prefix]}
        \edef\temp@ph@cmd{\noexpand\stepcounter{gnuplot@id} \unexpanded\expandafter{\temp@ph@cmd} gnuplot[gnuplot@prefix]}
      \fi
      \temp@mag@cmd {\func@mag};
      \optmag@commands
      \temp@ph@cmd {\func@ph};
      \optph@commands
    \end{groupplot}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\BodeTF}
% Implementation of this macro is very similar to the |\BodeZPK| macro above. The only difference is the lack of linear and asymptotic plots and slightly different parsing of the mandatory arguments. \changes{v1.0.3}{2021/11/03}{Added Tikz option} \changes{v1.1.0}{2022/07/06}{Fixed phase wrapping in gnuplot mode}
%    \begin{macrocode}
\newcommand{\BodeTF}[4][]{%
  \parse@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@TF@plot{\func@mag}{\func@ph}{#2}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{groupplot}[%
      bode@style,
      xmin={#3},
      xmax={#4},
      domain=#3:#4,
      height=2.5cm,
      xmode=log,
      group style = {group size = 1 by 2,vertical sep=0.25cm},
      \opt@group
    ]%
  }%
  \temp@cmd
      \edef\temp@mag@cmd{\noexpand\nextgroupplot[ytick distance=20, ylabel={Gain (dB)}, xmajorticks=false, \optmag@axes]
      \noexpand\addplot[variable=t, thick, \optmag@plot]}
      \edef\temp@ph@cmd{\noexpand\nextgroupplot[ph@filter, xlabel={Frequency (rad/s)}, \optph@axes]
      \noexpand\addplot[variable=t, thick, \optph@plot]}
      \if@pgfarg\else
        \edef\temp@mag@cmd{\noexpand\stepcounter{gnuplot@id} \unexpanded\expandafter{\temp@mag@cmd} gnuplot[gnuplot@prefix]}
        \edef\temp@ph@cmd{\noexpand\stepcounter{gnuplot@id} \unexpanded\expandafter{\temp@ph@cmd} gnuplot[gnuplot@prefix]}
      \fi
      \temp@mag@cmd {\func@mag};
      \optmag@commands
      \if@pgfarg
        \temp@ph@cmd {\n@mod{\func@ph}{2*pi}};
      \else
        \temp@ph@cmd {'+' using (t):\func@ph smooth unwrap};
      \fi
      \optph@commands
    \end{groupplot}
  \end{tikzpicture}
}
%    \end{macrocode} 
% \end{macro}
% \begin{macro}{\addBodeZPKPlots}
% This macro is designed to issues multiple |\addplot| macros for the same set of poles, zeros, gain, and delay. All of the work is done by the |\build@ZPK@plot| macro. \changes{v1.0.1}{2021/10/29}{Improved optional argument handling.}
%    \begin{macrocode}
\newcommand{\addBodeZPKPlots}[3][true/{}]{%
  \foreach \approx/\opt in {#1} {%
    \gdef\plot@macro{}%
    \gdef\temp@macro{}%
    \ifnum\pdfstrcmp{#2}{phase}=0
      \build@ZPK@plot{\temp@macro}{\plot@macro}{\approx}{#3}%
    \else
      \build@ZPK@plot{\plot@macro}{\temp@macro}{\approx}{#3}%
    \fi
    \if@pgfarg
      \edef\temp@cmd{\noexpand\addplot[variable=t,thick,\opt]}%
      \temp@cmd {\plot@macro};
    \else
      \stepcounter{gnuplot@id}%
      \edef\temp@cmd{\noexpand\addplot[variable=t,thick,\opt]}
      \temp@cmd gnuplot[gnuplot@prefix] {\plot@macro};
    \fi
  }%
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\addBodeTFPlot}
% This macro is designed to issues a single |\addplot| macros for the set of coefficients and delay. All of the work is done by the |\build@TF@plot| macro.  \changes{v1.1.0}{2022/07/06}{Fixed phase wrapping in gnuplot mode}
%    \begin{macrocode}
\newcommand{\addBodeTFPlot}[3][thick]{%
  \gdef\plot@macro{}%
  \gdef\temp@macro{}%
  \ifnum\pdfstrcmp{#2}{phase}=0
    \build@TF@plot{\temp@macro}{\plot@macro}{#3}%
  \else
    \build@TF@plot{\plot@macro}{\temp@macro}{#3}%
  \fi
  \if@pgfarg
    \ifnum\pdfstrcmp{#2}{phase}=0
      \addplot[variable=t,#1]{\n@mod{\plot@macro}{2*pi}};
    \else
      \addplot[variable=t,#1]{\plot@macro};
    \fi
  \else
    \stepcounter{gnuplot@id}%
    \ifnum\pdfstrcmp{#2}{phase}=0
      \addplot[variable=t,#1] gnuplot[gnuplot@prefix] {'+' using (t):\plot@macro smooth unwrap}
    \else
      \addplot[variable=t,#1] gnuplot[gnuplot@prefix] {\plot@macro};
    \fi
  \fi
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\addBodeComponentPlot}
% This macro is designed to issue a single |\addplot| macro capable of plotting linear combinations of the basic components described in Section \ref{sec:BasicComponents}. The only work to do here is to handle the |pgf| package option.
%    \begin{macrocode}
\newcommand{\addBodeComponentPlot}[2][thick]{%
  \if@pgfarg
    \addplot[variable=t,#1]{#2};
  \else
    \stepcounter{gnuplot@id}%
    \addplot[variable=t,#1] gnuplot[gnuplot@prefix] {#2};
  \fi
}
%    \end{macrocode}
%\end{macro}
% \begin{environment}{BodePhPlot}
% An environment to host phase plot macros that pass parametric functions to |\addplot| macros. Uses the defaults specified in |bode@style| to create a shortcut that includes the |tikzpicture| and |semilogaxis| environments.\changes{v1.1.0}{2022/07/20}{Added separate environments for phase and magnitude plots}
%    \begin{macrocode}
\newenvironment{BodePhPlot}[3][]{%
  \parse@env@opt{#1}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]
    \noexpand\begin{semilogxaxis}[%
      ph@filter,
      bode@style,
      xmin={#2},
      xmax={#3},
      domain=#2:#3,
      height=2.5cm,
      xlabel={Frequency (rad/s)},
      \unexpanded\expandafter{\opt@axes}
    ]%
  }
  \temp@cmd
}{
    \end{semilogxaxis}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{environment}
% \begin{environment}{BodeMagPlot}
% An environment to host magnitude plot macros that pass parametric functions to |\addplot| macros. Uses the defaults specified in |bode@style| to create a shortcut that includes the |tikzpicture| and |semilogaxis| environments.\changes{v1.1.0}{2022/07/20}{Added separate environments for phase and magnitude plots}
%    \begin{macrocode}
\newenvironment{BodeMagPlot}[3][]{%
  \parse@env@opt{#1}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]
    \noexpand\begin{semilogxaxis}[%
      bode@style,
      xmin={#2},
      xmax={#3},
      domain=#2:#3,
      height=2.5cm,
      xlabel={Frequency (rad/s)},
      ylabel={Gain (dB)},
      ytick distance=40,
      \unexpanded\expandafter{\opt@axes}
    ]%
  }
  \temp@cmd
}{
    \end{semilogxaxis}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{environment}
% \begin{environment}{BodePlot}
% Same as |BodeMagPlot|. The |BodePlot| environment is deprecated as of v1.1.0, please use the |BodePhPlot| and |BodeMagPlot| environments instead.\changes{v1.0.3}{2021/11/03}{Added tikz option to environments}\changes{v1.1.0}{2022/02/20}{Deprecated BodePlot environment}
%    \begin{macrocode}
\newenvironment{BodePlot}[3][]{%
  \PackageWarning{bodeplot}{Since v1.1.0, the BodePlot environment returns phase plots in radian units only. Please use the BodePhPlot environment if degree units are needed.}%
  \parse@env@opt{#1}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]
    \noexpand\begin{semilogxaxis}[%
      bode@style,
      xmin={#2},
      xmax={#3},
      domain=#2:#3,
      height=2.5cm,
      xlabel={Frequency (rad/s)},
      \unexpanded\expandafter{\opt@axes}
    ]%
  }
  \temp@cmd
}{
    \end{semilogxaxis}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{environment}
% \subsubsection{Internal macros}
% \begin{macro}{\add@feature}
% This is an internal macro to add a basic component (pole, zero, gain, or delay), described using one of the macros in Section \ref{sec:BasicComponents} (input |#2|), to a parametric function stored in a global macro (input |#1|). The basic component value (input |#3|) is a complex number of the form |{re,im}|. If the imaginary part is missing, it is assumed to be zero. Implementation made possible by \href{https://tex.stackexchange.com/a/619637/110602}{this StackExchange answer}.
%    \begin{macrocode}
\newcommand*{\add@feature}[3]{%
  \ifcat$\detokenize\expandafter{#1}$%
    \xdef#1{\unexpanded\expandafter{#1 0+#2}}%
  \else
    \xdef#1{\unexpanded\expandafter{#1+#2}}%
  \fi
  \foreach \y [count=\n] in #3 {%
    \xdef#1{\unexpanded\expandafter{#1}{\y}}%
    \xdef\Last@LoopValue{\n}%
  }%
  \ifnum\Last@LoopValue=1%
    \xdef#1{\unexpanded\expandafter{#1}{0}}%
  \fi
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\build@ZPK@plot}
% This is an internal macro to build parametric Bode magnitude and phase plots by concatenating basic component (pole, zero, gain, or delay) macros (Section \ref{sec:BasicComponents}) to global magnitude and phase macros (inputs |#1| and |#2|). The |\add@feature| macro is used to do the concatenation. The basic component macros are inferred from a |feature/{values}| list, where |feature| is one of |z|,|p|,|k|, and |d|, for zeros, poles, gain, and delay, respectively, and |{values}| is a comma separated list of comma separated lists (complex numbers of the form |{re,im}|). If the imaginary part is missing, it is assumed to be zero.
%    \begin{macrocode}
\newcommand{\build@ZPK@plot}[4]{%
  \foreach \feature/\values in {#4} {%
    \ifnum\pdfstrcmp{\feature}{z}=0
      \foreach \z in \values {%
        \ifnum\pdfstrcmp{#3}{linear}=0
          \add@feature{#2}{\PhZeroLin}{\z}%
          \add@feature{#1}{\MagZeroLin}{\z}%
        \else
          \ifnum\pdfstrcmp{#3}{asymptotic}=0
            \add@feature{#2}{\PhZeroAsymp}{\z}%
            \add@feature{#1}{\MagZeroAsymp}{\z}%
          \else
            \add@feature{#2}{\PhZero}{\z}%
            \add@feature{#1}{\MagZero}{\z}%
          \fi
        \fi
      }%
    \fi
    \ifnum\pdfstrcmp{\feature}{p}=0
      \foreach \p in \values {%
        \ifnum\pdfstrcmp{#3}{linear}=0
          \add@feature{#2}{\PhPoleLin}{\p}%
          \add@feature{#1}{\MagPoleLin}{\p}%
        \else
          \ifnum\pdfstrcmp{#3}{asymptotic}=0
            \add@feature{#2}{\PhPoleAsymp}{\p}%
            \add@feature{#1}{\MagPoleAsymp}{\p}%
          \else
            \add@feature{#2}{\PhPole}{\p}%
            \add@feature{#1}{\MagPole}{\p}%
          \fi
        \fi
      }%
    \fi
    \ifnum\pdfstrcmp{\feature}{k}=0
      \ifnum\pdfstrcmp{#3}{linear}=0
        \add@feature{#2}{\PhKLin}{\values}%
        \add@feature{#1}{\MagKLin}{\values}%
      \else
        \ifnum\pdfstrcmp{#3}{asymptotic}=0
          \add@feature{#2}{\PhKAsymp}{\values}%
          \add@feature{#1}{\MagKAsymp}{\values}%
        \else
          \add@feature{#2}{\PhK}{\values}%
          \add@feature{#1}{\MagK}{\values}%
        \fi
      \fi
    \fi
    \ifnum\pdfstrcmp{\feature}{d}=0
      \ifnum\pdfstrcmp{#3}{linear}=0
        \PackageError {bodeplot} {Linear approximation for pure delays is not 
        supported.} {Plot the true Bode plot using `true' instead of `linear'.}
      \else
        \ifnum\pdfstrcmp{#3}{asymptotic}=0
          \PackageError {bodeplot} {Asymptotic approximation for pure delays is not 
          supported.} {Plot the true Bode plot using `true' instead of `asymptotic'.}
        \else
          \ifdim\values pt < 0pt
            \PackageError {bodeplot} {Delay needs to be a positive number.}
          \fi
          \add@feature{#2}{\PhDel}{\values}%
          \add@feature{#1}{\MagDel}{\values}%
        \fi
      \fi
    \fi
  }%
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\build@TF@plot}
% This is an internal macro to build parametric Bode magnitude and phase functions by computing the magnitude and the phase given numerator and denominator coefficients and delay (input |#3|). The functions are assigned to user-supplied global magnitude and phase macros (inputs |#1| and |#2|). \changes{v1.0.8}{2022/07/05}{Included phase due to delay in wrapping.}
%    \begin{macrocode}
\newcommand{\build@TF@plot}[3]{%
  \gdef\num@real{0}%
  \gdef\num@im{0}%
  \gdef\den@real{0}%
  \gdef\den@im{0}%
  \gdef\loop@delay{0}%
  \foreach \feature/\values in {#3} {%
    \ifnum\pdfstrcmp{\feature}{num}=0
      \foreach \numcoeff [count=\numpow] in \values {%
        \xdef\num@degree{\numpow}%
      }%
      \foreach \numcoeff [count=\numpow] in \values {%
        \pgfmathtruncatemacro{\currentdegree}{\num@degree-\numpow}%
        \ifnum\currentdegree = 0
          \xdef\num@real{\num@real+\numcoeff}%
        \else
          \ifodd\currentdegree
            \xdef\num@im{\num@im+(\numcoeff*(\n@pow{-1}{(\currentdegree-1)/2})*%
              (\n@pow{t}{\currentdegree}))}%
          \else
            \xdef\num@real{\num@real+(\numcoeff*(\n@pow{-1}{(\currentdegree)/2})*%
              (\n@pow{t}{\currentdegree}))}%
          \fi
        \fi
      }%
    \fi
    \ifnum\pdfstrcmp{\feature}{den}=0
      \foreach \dencoeff [count=\denpow] in \values {%
        \xdef\den@degree{\denpow}%
      }%
      \foreach \dencoeff [count=\denpow] in \values {%
        \pgfmathtruncatemacro{\currentdegree}{\den@degree-\denpow}%
        \ifnum\currentdegree = 0
          \xdef\den@real{\den@real+\dencoeff}%
        \else
          \ifodd\currentdegree
            \xdef\den@im{\den@im+(\dencoeff*(\n@pow{-1}{(\currentdegree-1)/2})*%
              (\n@pow{t}{\currentdegree}))}%
          \else
            \xdef\den@real{\den@real+(\dencoeff*(\n@pow{-1}{(\currentdegree)/2})*%
              (\n@pow{t}{\currentdegree}))}%
          \fi
        \fi
      }%
    \fi
    \ifnum\pdfstrcmp{\feature}{d}=0
      \xdef\loop@delay{\values}%
    \fi
  }%
  \xdef#2{(atan2((\num@im),(\num@real))-atan2((\den@im),%
    (\den@real))-\loop@delay*t)}%
  \xdef#1{(20*log10(sqrt((\n@pow{\num@real}{2})+(\n@pow{\num@im}{2})))-%
    20*log10(sqrt((\n@pow{\den@real}{2})+(\n@pow{\den@im}{2}))))}%
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\parse@opt}
% Parses options supplied to the main Bode macros. A |for| loop over tuples of the form |\obj/\typ/\opt| with a long list of nested if-else statements does the job. If the input |\obj| is |plot|, |axes|, |group|, |approx|, or |tikz| the corresponding |\opt| are passed, unexpanded, to the |\addplot| macro, the |\nextgroupplot| macro, the |groupplot| environment, the |\build@ZPK@plot| macro, and the |tikzpicture| environment, respectively. If |\obj| is |commands|, the corresponding |\opt| are stored, unexpanded, in the macros |\optph@commands| and |\optmag@commands|, to be executed in appropriate |axis| environments. \changes{v1.0.3}{2021/11/03}{Added Tikz option} \changes{v1.0.5}{2021/11/15}{Fixed a bug}
%    \begin{macrocode}
\newcommand{\parse@opt}[1]{%
  \gdef\optmag@axes{}%
  \gdef\optph@axes{}%
  \gdef\optph@plot{}%
  \gdef\optmag@plot{}%
  \gdef\opt@group{}%
  \gdef\opt@approx{}%
  \gdef\optph@commands{}%
  \gdef\optmag@commands{}%
  \gdef\opt@tikz{}%
  \foreach \obj/\typ/\opt in {#1} {%
    \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{plot}=0
      \ifnum\pdfstrcmp{\unexpanded\expandafter{\typ}}{mag}=0
        \xdef\optmag@plot{\unexpanded\expandafter{\opt}}%
      \else
        \ifnum\pdfstrcmp{\unexpanded\expandafter{\typ}}{ph}=0
          \xdef\optph@plot{\unexpanded\expandafter{\opt}}%
        \else
          \xdef\optmag@plot{\unexpanded\expandafter{\opt}}%
          \xdef\optph@plot{\unexpanded\expandafter{\opt}}%
        \fi
      \fi
    \else
      \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{axes}=0
        \ifnum\pdfstrcmp{\unexpanded\expandafter{\typ}}{mag}=0
          \xdef\optmag@axes{\unexpanded\expandafter{\opt}}%
        \else
          \ifnum\pdfstrcmp{\unexpanded\expandafter{\typ}}{ph}=0
            \xdef\optph@axes{\unexpanded\expandafter{\opt}}%
          \else
            \xdef\optmag@axes{\unexpanded\expandafter{\opt}}%
            \xdef\optph@axes{\unexpanded\expandafter{\opt}}%
          \fi
        \fi
      \else
        \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{group}=0
          \xdef\opt@group{\unexpanded\expandafter{\opt}}%
        \else
          \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{approx}=0
            \xdef\opt@approx{\unexpanded\expandafter{\opt}}%
          \else
            \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{commands}=0
              \ifnum\pdfstrcmp{\unexpanded\expandafter{\typ}}{ph}=0
                \xdef\optph@commands{\unexpanded\expandafter{\opt}}%
              \else
                \xdef\optmag@commands{\unexpanded\expandafter{\opt}}%
              \fi
            \else
              \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{tikz}=0
                \xdef\opt@tikz{\unexpanded\expandafter{\opt}}%
              \else
                \xdef\optmag@plot{\unexpanded\expandafter{\optmag@plot},
                  \unexpanded\expandafter{\obj}}%
                \xdef\optph@plot{\unexpanded\expandafter{\optph@plot},
                  \unexpanded\expandafter{\obj}}%
              \fi
            \fi
          \fi
        \fi
      \fi
    \fi
  }%
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\parse@env@opt}
% Parses options supplied to the Bode, Nyquist, and Nichols environments. A |for| loop over tuples of the form |\obj/\opt|, processed using nested if-else statements does the job. The input |\obj| should either be |axes| or |tikz|, and the corresponding |\opt| are passed, unexpanded, to the |axis| environment and the |tikzpicture| environment, respectively. \changes{v1.0.3}{2021/11/03}{Added tikz option to environments}
%    \begin{macrocode}
\newcommand{\parse@env@opt}[1]{%
  \gdef\opt@axes{}%
  \gdef\opt@tikz{}%
  \foreach \obj/\opt in {#1} {%
    \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{axes}=0
      \xdef\opt@axes{\unexpanded\expandafter{\opt}}%
    \else
      \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{tikz}=0
        \xdef\opt@tikz{\unexpanded\expandafter{\opt}}%
      \else
        \xdef\opt@axes{\unexpanded\expandafter{\opt@axes},
          \unexpanded\expandafter{\obj}}%
      \fi
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% \subsection{Nyquist plots}
% \subsubsection{User macros}
% \begin{macro}{\NyquistZPK}
% Converts magnitude and phase parametric functions built using |\build@ZPK@plot| into real part and imaginary part parametric functions. A plot of these is the Nyquist plot. The parametric functions are then plotted in a |tikzpicture| environment using the |\addplot| macro. Unless the package is loaded with the option |pgf|, the parametric functions are evaluated using |gnuplot|. A large number of samples is typically needed to get a smooth plot because frequencies near 0 result in plot points that are very close to each other. Linear frequency sampling is unnecessarily fine near zero and very coarse for large $\omega$. Logarithmic sampling makes it worse, perhaps inverse logarithmic sampling will help, pull requests to fix that are welcome! \changes{v1.0.3}{2021/11/03}{Added commands and tikz options}
%    \begin{macrocode}
\newcommand{\NyquistZPK}[4][]{%
  \parse@N@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@ZPK@plot{\func@mag}{\func@ph}{}{#2}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      bode@style,
      domain=#3:#4,
      height=5cm,
      xlabel={$\Re$},
      ylabel={$\Im$},
      samples=500,
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
      \addplot [only marks,mark=+,thick,red] (-1 , 0);
      \edef\temp@cmd{\noexpand\addplot[variable=t, thick, \unexpanded\expandafter{\opt@plot}]}%
      \if@pgfarg
        \temp@cmd ( {\n@pow{10}{((\func@mag)/20)}*cos(\func@ph)},
          {\n@pow{10}{((\func@mag)/20)}*sin(\func@ph)} );
        \opt@commands
      \else
        \stepcounter{gnuplot@id}%
        \temp@cmd gnuplot[parametric,gnuplot@prefix] {%
          \n@pow{10}{((\func@mag)/20)}*cos(\func@ph),
          \n@pow{10}{((\func@mag)/20)}*sin(\func@ph)};
        \opt@commands
      \fi
    \end{axis}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NyquistTF}
% Implementation of this macro is very similar to the |\NyquistZPK| macro above. The only difference is a slightly different parsing of the mandatory arguments via |\build@TF@plot|. \changes{v1.0.3}{2021/11/03}{Added commands and tikz options}
%    \begin{macrocode}
\newcommand{\NyquistTF}[4][]{%
  \parse@N@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@TF@plot{\func@mag}{\func@ph}{#2}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      bode@style,
      domain=#3:#4,
      height=5cm,
      xlabel={$\Re$},
      ylabel={$\Im$},
      samples=500,
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
      \addplot [only marks,mark=+,thick,red] (-1 , 0);
      \edef\temp@cmd{\noexpand\addplot[variable=t, thick, \unexpanded\expandafter{\opt@plot}]}%
      \if@pgfarg
        \temp@cmd ( {\n@pow{10}{((\func@mag)/20)}*cos(\func@ph)},
          {\n@pow{10}{((\func@mag)/20)}*sin(\func@ph)} );
        \opt@commands
      \else
        \stepcounter{gnuplot@id}%
        \temp@cmd gnuplot[parametric,gnuplot@prefix]{%
          \n@pow{10}{((\func@mag)/20)}*cos(\func@ph),
          \n@pow{10}{((\func@mag)/20)}*sin(\func@ph)};
        \opt@commands
      \fi
    \end{axis}
  \end{tikzpicture}
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\addNyquistZPKPlot}
% Adds Nyquist plot of a transfer function in ZPK form. This macro is designed to pass two parametric function to an |\addplot| macro. The parametric functions for phase (|\func@ph|) and magnitude (|\func@mag|) are built using the |\build@ZPK@plot| macro, converted to real and imaginary parts and passed to |\addplot| commands.
%    \begin{macrocode}
\newcommand{\addNyquistZPKPlot}[2][]{%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@ZPK@plot{\func@mag}{\func@ph}{}{#2}%
  \if@pgfarg
    \addplot[variable=t,#1] ( {\n@pow{10}{((\func@mag)/20)}*cos(\func@ph)},
      {\n@pow{10}{((\func@mag)/20)}*sin(\func@ph)} );
  \else
    \stepcounter{gnuplot@id}%
    \addplot[variable=t,#1] gnuplot[parametric,gnuplot@prefix]{%
      \n@pow{10}{((\func@mag)/20)}*cos(\func@ph),
      \n@pow{10}{((\func@mag)/20)}*sin(\func@ph)};
  \fi
}
%    \end{macrocode}
%\end{macro}
% \begin{macro}{\addNyquistTFPlot}
% Adds Nyquist plot of a transfer function in TF form. This macro is designed to pass two parametric function to an |\addplot| macro. The parametric functions for phase (|\func@ph|) and magnitude (|\func@mag|) are built using the |\build@TF@plot| macro, converted to real and imaginary parts and passed to |\addplot| commands.
%    \begin{macrocode}
\newcommand{\addNyquistTFPlot}[2][]{%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@TF@plot{\func@mag}{\func@ph}{#2}%
  \if@pgfarg
    \addplot[variable=t,#1] ( {\n@pow{10}{((\func@mag)/20)}*cos(\func@ph)},
      {\n@pow{10}{((\func@mag)/20)}*sin(\func@ph)} );
  \else
    \stepcounter{gnuplot@id}%
    \addplot[variable=t,#1] gnuplot[parametric,gnuplot@prefix]{%
      \n@pow{10}{((\func@mag)/20)}*cos(\func@ph),
      \n@pow{10}{((\func@mag)/20)}*sin(\func@ph)};
  \fi
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{NyquistPlot}
% An environment to host |\addNyquist...| macros that pass parametric functions to |\addplot|. Uses the defaults specified in |bode@style| to create a shortcut that includes the |tikzpicture| and |axis| environments. \changes{v1.0.3}{2021/11/03}{Added tikz option to environments}
%    \begin{macrocode}
\newenvironment{NyquistPlot}[3][]{%
  \parse@env@opt{#1}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      bode@style,
      height=5cm,
      domain=#2:#3,
      xlabel={$\Re$},
      ylabel={$\Im$},
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
    \addplot [only marks,mark=+,thick,red] (-1 , 0);
}{%
    \end{axis}
  \end{tikzpicture}
}
%    \end{macrocode}
%\end{macro}
% \subsubsection{Internal commands\label{sec:NInternal}}
% \begin{macro}{\parse@N@opt}
% Parses options supplied to the main Nyquist and Nichols macros. A |for| loop over tuples of the form |\obj/\opt|, processed using nested if-else statements does the job. If the input |\obj| is |plot|, |axes|, or |tikz| then the corresponding |\opt| are passed, unexpanded, to the |\addplot| macro, the |axis| environment, and the |tikzpicture| environment, respectively. \changes{v1.0.3}{2021/11/03}{Added commands and tikz options}
%    \begin{macrocode}
\newcommand{\parse@N@opt}[1]{%
  \gdef\opt@axes{}%
  \gdef\opt@plot{}%
  \gdef\opt@commands{}%
  \gdef\opt@tikz{}
  \foreach \obj/\opt in {#1} {%
    \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{axes}=0
      \xdef\opt@axes{\unexpanded\expandafter{\opt}}%
    \else
      \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{plot}=0
        \xdef\opt@plot{\unexpanded\expandafter{\opt}}%
      \else
        \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{commands}=0
          \xdef\opt@commands{\unexpanded\expandafter{\opt}}%
        \else
          \ifnum\pdfstrcmp{\unexpanded\expandafter{\obj}}{tikz}=0
            \xdef\opt@tikz{\unexpanded\expandafter{\opt}}%
          \else
            \xdef\opt@plot{\unexpanded\expandafter{\opt@plot},
              \unexpanded\expandafter{\obj}}%
          \fi
        \fi
      \fi
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% \subsection{Nichols charts}
% \begin{macro}{\NicholsZPK}
% \changes{v1.0.3}{2021/11/03}{Added commands and tikz options}
% \begin{macro}{\NicholsTF}
% \changes{v1.0.3}{2021/11/03}{Added commands and tikz options}
% \begin{macro}{NicholsChart}
% \changes{v1.0.3}{2021/11/03}{Added tikz option to environments}
% \begin{macro}{\addNicholsZPKChart}
% \begin{macro}{\addNicholsTFChart}
% These macros and the |NicholsChart| environment generate Nichols charts, and they are implemented similar to their Nyquist counterparts.
%    \begin{macrocode}
\newcommand{\NicholsZPK}[4][]{%
  \parse@N@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@ZPK@plot{\func@mag}{\func@ph}{}{#2}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      ph@x@filter,
      bode@style,
      domain=#3:#4,
      height=5cm,
      ylabel={Gain (dB)},
      samples=500,
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
      \edef\temp@cmd{\noexpand\addplot[variable=t,thick,\opt@plot]}%
      \if@pgfarg
        \temp@cmd ( {\func@ph} , {\func@mag} );
        \opt@commands
      \else
        \stepcounter{gnuplot@id}%
        \temp@cmd gnuplot[parametric,gnuplot@prefix]
          { \func@ph , \func@mag };
        \opt@commands
      \fi
    \end{axis}
  \end{tikzpicture}
}
\newcommand{\NicholsTF}[4][]{%
  \parse@N@opt{#1}%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@TF@plot{\func@mag}{\func@ph}{#2}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      ph@x@filter,
      bode@style,
      domain=#3:#4,
      height=5cm,
      ylabel={Gain (dB)},
      samples=500,
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
      \edef\temp@cmd{\noexpand\addplot[variable=t,thick,\opt@plot]}%
      \if@pgfarg
        \temp@cmd ( {\n@mod{\func@ph}{2*pi}} , {\func@mag} );
        \opt@commands
      \else
        \stepcounter{gnuplot@id}%
        \temp@cmd gnuplot[parametric,gnuplot@prefix]
          { \n@mod{\func@ph}{2*pi} , \func@mag };
        \opt@commands
      \fi
    \end{axis}
  \end{tikzpicture}
}
\newenvironment{NicholsChart}[3][]{%
  \parse@env@opt{#1}%
  \edef\temp@cmd{\noexpand\begin{tikzpicture} [\unexpanded\expandafter{\opt@tikz}]%
    \noexpand\begin{axis}[%
      ph@x@filter,
      bode@style,
      domain=#2:#3,
      height=5cm,
      ylabel={Gain (dB)},
      \unexpanded\expandafter{\opt@axes}
    ]%
  }%
  \temp@cmd
}{
    \end{axis}
  \end{tikzpicture}
}
\newcommand{\addNicholsZPKChart}[2][]{%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@ZPK@plot{\func@mag}{\func@ph}{}{#2}%
  \if@pgfarg
    \addplot[variable=t,#1] ( {\func@ph} , {\func@mag} );
  \else
    \stepcounter{gnuplot@id}%
    \addplot[variable=t,#1] gnuplot[parametric,gnuplot@prefix]
      {\func@ph , \func@mag};
  \fi
}
\newcommand{\addNicholsTFChart}[2][]{%
  \gdef\func@mag{}%
  \gdef\func@ph{}%
  \build@TF@plot{\func@mag}{\func@ph}{#2}%
  \if@pgfarg
    \addplot[variable=t,#1] ( {\n@mod{\func@ph}{2*pi}} , {\func@mag} );
  \else
    \stepcounter{gnuplot@id}%
    \addplot[variable=t,#1] gnuplot[gnuplot@prefix]
      {\n@mod{\func@ph}{2*pi} , \func@mag};
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \Finale
\endinput