因此我设法得到了如下所示的幅度和相位图:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{float}
\begin{document}
\begin{figure}[H]\centering
\begin{tikzpicture}
\begin{axis}[
width = 5 in,
height = 1 in,
scale only axis,
at={(0in,3.5in)},
xtick={-pi, -(3/4)*pi, -(1/2)*pi, -(1/4)*pi, (1/4)*pi, (1/2)*pi, (3/4)*pi, pi},
xticklabels={$-\pi$, $-\frac{3\pi}{4}$, $-\frac{\pi}{2}$, $-\frac{\pi}{4}$,$\frac{\pi}{4}$, $\frac{\pi}{2}$, $\frac{3\pi}{4}$, $\pi$},
ytick={1},
xmin=-pi,xmax=pi,
ymin=0,ymax=1,
axis lines=center,
axis line style=->, xlabel = {$\omega$}, ylabel={$\left|H\left(e^{j\omega}\right)\right|$},
%every axis y label/.style={at={(0,1)},anchor=south},
%every axis x label/.style={at={(1,2/3)},anchor=west},
every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
domain=-pi:pi,samples=101]
\addplot [blue,thick, domain = -pi:pi] {abs(sin(2*deg(x)))};
\end{axis}
\begin{axis}[ %Phase plot
width = 5 in,
height = 2.5 in,
scale only axis,
at={(0in,0in)},
xtick={-pi, -(3/4)*pi, -(1/2)*pi, -(1/4)*pi, (1/4)*pi, (1/2)*pi, (3/4)*pi, pi},
xticklabels={$-\pi$, $-\frac{3\pi}{4}$, $-\frac{\pi}{2}$, $-\frac{\pi}{4}$,$\frac{\pi}{4}$, $\frac{\pi}{2}$, $\frac{3\pi}{4}$, $\pi$},
ytick={-pi, -(1/2)*pi, (1/2)*pi, pi},
yticklabels={$-\pi$, $-\frac{\pi}{2}$, $\frac{\pi}{2}$, $\pi$},
xmin=-pi,xmax=pi,
ymin=-pi,ymax=pi,
axis lines=center,
axis line style=->, xlabel = {$\omega$}, ylabel={$\angle H\left(e^{j\omega}\right)$},
%every axis y label/.style={at={(0,1)},anchor=south},
%every axis x label/.style={at={(1,2/3)},anchor=west},
every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
domain=-pi:pi,samples=101]
\addplot [blue,thick, domain = -pi: -pi/2] { pi/2 - 2*(x + pi)};
\addplot [blue, thick] coordinates{(-pi/2, -pi/2) (-pi/2, pi/2)};
\addplot [blue,thick, domain = -pi/2: 0] { pi/2 - 2*(x + pi/2)};
\addplot [blue, thick] coordinates{(0, -pi/2) (0, pi/2)};
\addplot [blue,thick, domain = 0:pi/2] { pi/2 - 2*x};
\addplot [blue, thick] coordinates{(pi/2, -pi/2) (pi/2, pi/2)};
\addplot [blue,thick, domain = pi/2:pi] { pi/2 - 2*(x - pi/2)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
输出:
我想知道是否有更简洁(甚至更好)的方法来绘制如图所示的相位图。我试过了\pgfplotsinvokeforeach
,但一直出错。
答案1
您要求提供情节。以下是情节:
\addplot[blue,thick,samples at={-2,-1,-1,0,0,1,1,2}]
({x*pi/2},{int(\coordindex/2)==\coordindex/2?pi/2:-pi/2});
;-)
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{float}
\begin{document}
\begin{figure}[H]\centering
\begin{tikzpicture}
\begin{axis}[
width = 5 in,
height = 1 in,
scale only axis,
at={(0in,3.5in)},
xtick={-pi, -(3/4)*pi, -(1/2)*pi, -(1/4)*pi, (1/4)*pi, (1/2)*pi, (3/4)*pi, pi},
xticklabels={$-\pi$, $-\frac{3\pi}{4}$, $-\frac{\pi}{2}$, $-\frac{\pi}{4}$,$\frac{\pi}{4}$, $\frac{\pi}{2}$, $\frac{3\pi}{4}$, $\pi$},
ytick={1},
xmin=-pi,xmax=pi,
ymin=0,ymax=1,
axis lines=center,
axis line style=->, xlabel = {$\omega$}, ylabel={$\left|H\left(e^{j\omega}\right)\right|$},
%every axis y label/.style={at={(0,1)},anchor=south},
%every axis x label/.style={at={(1,2/3)},anchor=west},
every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
domain=-pi:pi,samples=101]
\addplot [blue,thick, domain = -pi:pi] {abs(sin(2*deg(x)))};
\end{axis}
\begin{axis}[ %Phase plot
width = 5 in,
height = 2.5 in,
scale only axis,
at={(0in,0in)},
xtick={-pi, -(3/4)*pi, -(1/2)*pi, -(1/4)*pi, (1/4)*pi, (1/2)*pi, (3/4)*pi, pi},
xticklabels={$-\pi$, $-\frac{3\pi}{4}$, $-\frac{\pi}{2}$, $-\frac{\pi}{4}$,$\frac{\pi}{4}$, $\frac{\pi}{2}$, $\frac{3\pi}{4}$, $\pi$},
ytick={-pi, -(1/2)*pi, (1/2)*pi, pi},
yticklabels={$-\pi$, $-\frac{\pi}{2}$, $\frac{\pi}{2}$, $\pi$},
xmin=-pi,xmax=pi,
ymin=-pi,ymax=pi,
axis lines=center,
axis line style=->, xlabel = {$\omega$}, ylabel={$\angle H\left(e^{j\omega}\right)$},
%every axis y label/.style={at={(0,1)},anchor=south},
%every axis x label/.style={at={(1,2/3)},anchor=west},
every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,}]
\addplot[blue,thick,samples at={-2,-1,-1,0,0,1,1,2}]
({x*pi/2},{int(\coordindex/2)==\coordindex/2?pi/2:-pi/2});
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
如果你不想输入所有样本,请使用
\addplot[blue,thick,samples at={-2,-1.5,...,2}]
({x*pi/2+(int(\coordindex/2)==\coordindex/2?0:pi/4)},{int(\coordindex/2)==\coordindex/2?pi/2:-pi/2});