diff options
| author | Philipp Le <philipp-le-prviat@freenet.de> | 2020-07-06 02:51:38 +0200 |
|---|---|---|
| committer | Philipp Le <philipp-le-prviat@freenet.de> | 2021-03-04 22:44:39 +0100 |
| commit | 711fb9f60698bba1d4f7570d34e8cf3fa9a87207 (patch) | |
| tree | bc61022f29f2b5dc60469e2a04cea032369d8f65 /exercise06 | |
| parent | e013061148e3b081d73442d1b6bc11212f60debc (diff) | |
| download | dcs-lecture-notes-711fb9f60698bba1d4f7570d34e8cf3fa9a87207.zip dcs-lecture-notes-711fb9f60698bba1d4f7570d34e8cf3fa9a87207.tar.gz dcs-lecture-notes-711fb9f60698bba1d4f7570d34e8cf3fa9a87207.tar.bz2 | |
Solution of Exercise 6
Diffstat (limited to 'exercise06')
| -rw-r--r-- | exercise06/exercise06.tex | 359 | ||||
| -rw-r--r-- | exercise06/helpers/ex_6_1.py | 69 | ||||
| -rw-r--r-- | exercise06/helpers/ex_6_2.py | 80 | ||||
| -rw-r--r-- | exercise06/helpers/ex_6_4.py | 75 |
4 files changed, 582 insertions, 1 deletions
diff --git a/exercise06/exercise06.tex b/exercise06/exercise06.tex index a19d818..5045453 100644 --- a/exercise06/exercise06.tex +++ b/exercise06/exercise06.tex @@ -51,6 +51,117 @@ \begin{solution} \begin{tasks} + \task + See hand-written solution + + \task + See hand-written solution + + \task + See hand-written solution + + \task + See hand-written solution + + \task + \begin{table}[H] + \centering + \begin{tabular}{|r|r|r|} + \hline + $\phi$ in \si{rad} & $\left|\underline{H}\left(e^{j\phi}\right)\right|$ & $\arg\left(\underline{H}\left(e^{j\phi}\right)\right)$ in \si{rad} \\ + \hline + \hline + $0$ & $2$ & $0$ \\ + \hline + $0.39$ & $2$ & $0$ \\ + \hline + $0.79$ & $2$ & $0$ \\ + \hline + $1.18$ & $2$ & $0$ \\ + \hline + $1.57$ & $2$ & $0$ \\ + \hline + $1.96$ & $2$ & $0$ \\ + \hline + $2.36$ & $2$ & $0$ \\ + \hline + $2.75$ & $2$ & $0$ \\ + \hline + \end{tabular} + \end{table} + + \begin{figure}[H] + \centering + \begin{tikzpicture} + \begin{axis}[ + height={0.10\textheight}, + width=0.7\linewidth, + scale only axis, + xlabel={$\phi$ in \si{rad}}, + ylabel={$\left|\underline{H}\left(e^{j\phi}\right)\right|$}, + %grid style={line width=.6pt, color=lightgray}, + %grid=both, + grid=none, + legend pos=north east, + axis y line=middle, + axis x line=middle, + every axis x label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=north, + }, + every axis y label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=east, + }, + xmin=0, + xmax=3.5, + ymin=0, + ymax=2.2, + xtick={0, 1.5708, 3.14159}, + xticklabels={0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, +% ytick={0}, + ] + \addplot[red] coordinates {(0, 2) (0.39, 2) (0.79, 2) (1.18, 2) (1.57, 2) (1.96, 2) (2.36, 2) (2.75, 2)}; + \end{axis} + \end{tikzpicture} + \end{figure} + + \begin{figure}[H] + \centering + \begin{tikzpicture} + \begin{axis}[ + height={0.10\textheight}, + width=0.7\linewidth, + scale only axis, + xlabel={$\phi$ in \si{rad}}, + ylabel={$\arg\left(\underline{H}\left(e^{j\phi}\right)\right)$ in \si{rad}}, + %grid style={line width=.6pt, color=lightgray}, + %grid=both, + grid=none, + legend pos=north east, + axis y line=middle, + axis x line=middle, + every axis x label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=north, + }, + every axis y label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=east, + }, + xmin=0, + xmax=3.5, + ymin=-3.5, + ymax=3.5, + xtick={0, 1.5708, 3.14159}, + xticklabels={0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, + ytick={-3.14159, -1.5708, 0, 1.5708, 3.14159}, + yticklabels={$\pi\hspace{0.30cm}-$, $-\frac{\pi}{2}$, 0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, + ] + \addplot[blue] coordinates {(0, 0) (0.39, 0) (0.79, 0) (1.18, 0) (1.57, 0) (1.96, 0) (2.36, 0) (2.75, 0)}; + \end{axis} + \end{tikzpicture} + \end{figure} \end{tasks} \end{solution} @@ -84,6 +195,138 @@ \begin{solution} \begin{tasks} + \task + See hand-written solution + + \task + See hand-written solution + + \task + See hand-written solution + + \task + See hand-written solution + + \task + \begin{table}[H] + \centering + \begin{tabular}{|r|r|r|} + \hline + $f$ in \si{kHz} & $\left|\underline{H}\left(e^{j\phi}\right)\right|$ & $\arg\left(\underline{H}\left(e^{j\phi}\right)\right)$ in \si{rad} \\ + \hline + \hline + $-875$ & $2.66222$ & $-0.940067$ \\
+ \hline + $-750$ & $2.35077$ & $-1.59632$ \\
+ \hline + $-625$ & $2.02726$ & $-2.42601$ \\
+ \hline + $-500$ & $2.06155$ & $2.89661$ \\
+ \hline + $-375$ & $2.53576$ & $2.0415$ \\
+ \hline + $-250$ & $3.12827$ & $1.36149$ \\
+ \hline + $-125$ & $3.54979$ & $0.793366$ \\
+ \hline + $0$ & $3.64005$ & $0.2783$ \\
+ \hline + $125$ & $3.33017$ & $-0.20564$ \\
+ \hline + $250$ & $2.63934$ & $-0.675333$ \\
+ \hline + $375$ & $1.64624$ & $-1.1316$ \\
+ \hline + $500$ & $0.5$ & $-1.5708$ \\
+ \hline + $625$ & $0.653682$ & $1.05924$ \\
+ \hline + $750$ & $1.64502$ & $0.608236$ \\
+ \hline + $875$ & $2.34923$ & $0.128051$ \\
+ \hline + $1000$ & $2.69258$ & $-0.380506$ \\ + \hline + \end{tabular} + \end{table} + + Amplitude and phase plots will not be symmetric, because one filter coefficient is complex-valued. + + \begin{figure}[H] + \centering + \begin{tikzpicture} + \begin{axis}[ + height={0.10\textheight}, + width=0.7\linewidth, + scale only axis, + xlabel={$f$ in \si{Hz}}, + ylabel={$\left|\underline{H}\left(e^{j\phi}\right)\right|$}, + %grid style={line width=.6pt, color=lightgray}, + %grid=both, + grid=none, + legend pos=north east, + axis y line=middle, + axis x line=middle, + every axis x label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=north, + }, + every axis y label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=east, + }, +% xmin=-3.5, +% xmax=3.5, + ymin=0, + ymax=3.7, +% xtick={0, 1.5708, 3.14159}, +% xticklabels={0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, +% ytick={0}, + ] + \addplot[red] coordinates {(-875000, 2.66222) (-750000, 2.35077) (-625000, 2.02726) (-500000, 2.06155) (-375000, 2.53576) (-250000, 3.12827) (-125000, 3.54979) (0, 3.64005) (125000, 3.33017) (250000, 2.63934) (375000, 1.64624) (500000, 0.5) (625000, 0.653682) (750000, 1.64502) (875000, 2.34923) (1e+06, 2.69258)}; + \end{axis} + \end{tikzpicture} + \end{figure} + + \begin{figure}[H] + \centering + \begin{tikzpicture} + \begin{axis}[ + height={0.10\textheight}, + width=0.7\linewidth, + scale only axis, + xlabel={$f$ in \si{Hz}}, + ylabel={$\arg\left(\underline{H}\left(e^{j\phi}\right)\right)$ in \si{rad}}, + %grid style={line width=.6pt, color=lightgray}, + %grid=both, + grid=none, + legend pos=north east, + axis y line=middle, + axis x line=middle, + every axis x label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=north, + }, + every axis y label/.style={ + at={(ticklabel* cs:1.05)}, + anchor=east, + }, +% xmin=-3.5, +% xmax=3.5, + ymin=-3.5, + ymax=3.5, +% xtick={0, 1.5708, 3.14159}, +% xticklabels={0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, + ytick={-3.14159, -1.5708, 0, 1.5708, 3.14159}, + yticklabels={$\pi\hspace{0.30cm}-$, $-\frac{\pi}{2}$, 0, $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$}, + ] + \addplot[blue] coordinates {(-875000, -0.940067) (-750000, -1.59632) (-625000, -2.42601) (-500000, 2.89661) (-375000, 2.0415) (-250000, 1.36149) (-125000, 0.793366) (0, 0.2783) (125000, -0.20564) (250000, -0.675333) (375000, -1.1316) (500000, -1.5708) (625000, 1.05924) (750000, 0.608236) (875000, 0.128051) (1e+06, -0.380506)}; + \end{axis} + \end{tikzpicture} + \end{figure} + + \task + See hand-written solution \end{tasks} \end{solution} @@ -146,8 +389,122 @@ \end{question} \begin{solution} - %The signal is periodic with $N = 4$. + The signal is periodic with $N = 4$. So, it can be re-written as: + \begin{equation*} + x[n] = \left[\underline{-0.5}, 1, -2, 2\right] + \end{equation*} + \begin{tasks} + \task + The formula of the DFT is: + \begin{equation} + \underline{X}[k] = \sum\limits_{n = 0}^{N - 1} \underline{x}[n] \cdot e^{- j \frac{2 \pi}{N} k n} + \end{equation} + where: + \begin{itemize} + \item $N = 4$ + \item $\underline{x}[0] = -0.5$ + \item $\underline{x}[1] = 1$ + \item $\underline{x}[2] = -2$ + \item $\underline{x}[3] = \underline{x}[-1] = 2$ + \end{itemize} + + \begin{equation*} + \begin{split} + \underline{X}[0] &= \underline{x}[0] \cdot e^{-j \frac{\pi}{2} 0 \cdot 0} + \underline{x}[1] \cdot e^{-j \frac{\pi}{2} 0 \cdot 1} + \underline{x}[2] \cdot e^{-j \frac{\pi}{2} 0 \cdot 2} + \underline{x}[3] \cdot e^{-j \frac{\pi}{2} 0 \cdot 3} = 0.5 \\ + \underline{X}[1] &= \underline{x}[0] \cdot e^{-j \frac{\pi}{2} 1 \cdot 0} + \underline{x}[1] \cdot e^{-j \frac{\pi}{2} 1 \cdot 1} + \underline{x}[2] \cdot e^{-j \frac{\pi}{2} 1 \cdot 2} + \underline{x}[3] \cdot e^{-j \frac{\pi}{2} 1 \cdot 3} = 1.5+1j \\ + \underline{X}[2] &= \underline{x}[0] \cdot e^{-j \frac{\pi}{2} 2 \cdot 0} + \underline{x}[1] \cdot e^{-j \frac{\pi}{2} 2 \cdot 1} + \underline{x}[2] \cdot e^{-j \frac{\pi}{2} 2 \cdot 2} + \underline{x}[3] \cdot e^{-j \frac{\pi}{2} 2 \cdot 3} = -5.5 \\ + \underline{X}[3] &= \underline{x}[0] \cdot e^{-j \frac{\pi}{2} 3 \cdot 0} + \underline{x}[1] \cdot e^{-j \frac{\pi}{2} 3 \cdot 1} + \underline{x}[2] \cdot e^{-j \frac{\pi}{2} 3 \cdot 2} + \underline{x}[3] \cdot e^{-j \frac{\pi}{2} 3 \cdot 3} = 1.5-1j + \end{split} + \end{equation*} + + \task + \begin{figure}[H] + \centering + \begin{circuitikz}[ + x=0.4cm, + y=0.4cm, + littleamp/.style={amp, blocks/scale=0.2} + ] + \pgfmathsetmacro{\Xscale}{5} + \pgfmathsetmacro{\Cmargin}{0.15} + \foreach \n/\f/\g in {0/1/3,1/0/2}{ + \draw (0,{((2*\n+1)*\Xscale)}) node[left,align=right]{$\underline{x}[\f]$} -- ({10-\Cmargin},{((2*\n+1)*\Xscale)}) node[inputarrow,rotate=0]{}; + \node[adder,scale=0.2] at(10,{(\n*2*\Xscale)+(\Xscale)}) {}; + \draw (0,{(\n*2*\Xscale)}) node[left,align=right]{$\underline{x}[\g]$} -- (7.5,{(\n*2*\Xscale)}) to[littleamp,l=$\underline{w}_2^{1}$] ({10-\Cmargin},{(\n*2*\Xscale)}) node[inputarrow,rotate=0]{}; + \node[adder,scale=0.2] at(10,{(\n*2*\Xscale)}) {}; + \draw (5,{(\n*2*\Xscale)+\Xscale}) to[short,*-] ({10-\Cmargin},{(\n*2*\Xscale)+\Cmargin}) node[inputarrow,rotate=-45]{}; + \draw (5,{(\n*2*\Xscale)}) to[short,*-] (7.5,{(\n*2*\Xscale)+(\Xscale/2)}) to[littleamp,l=$\underline{w}_2^{0}$] ({10-\Cmargin},{(\n*2*\Xscale)+\Xscale-\Cmargin}) node[inputarrow,rotate=45]{}; + } + \foreach \n/\g in {0/3,1/2}{ + \draw ({10+\Cmargin},{(\n*\Xscale)+(2*\Xscale)}) -- ({20-\Cmargin},{(\n*\Xscale)+(2*\Xscale)}) node[inputarrow,rotate=0]{}; + \node[adder,scale=0.2] at(20,{(\n*\Xscale)+(2*\Xscale)}) {}; + \draw ({10+\Cmargin},{(\n*\Xscale)}) -- (17.5,{(\n*\Xscale)}) to[littleamp,l=$\underline{w}_4^{\g}$] ({20-\Cmargin},{(\n*\Xscale)}) node[inputarrow,rotate=0]{}; + \node[adder,scale=0.2] at(20,{(\n*\Xscale)}) {}; + } + \foreach \n/\g in {0/1,1/0}{ + \draw (15,{(2*\Xscale)+(\n*\Xscale)}) node[above,align=center,red]{$\underline{E}[\g]$} to[short,*-] ({20-\Cmargin},{(\n*\Xscale)+\Cmargin}) node[inputarrow,rotate=-60]{}; + \draw (15,{(\n*\Xscale)}) node[below,align=center,red]{$\underline{O}[\g]$} to[short,*-] (17.5,{(\Xscale)+(\n*\Xscale)}) to[littleamp,l=$\underline{w}_4^{\g}$] ({20-\Cmargin},{(2*\Xscale)+(\n*\Xscale)-\Cmargin}) node[inputarrow,rotate=60]{}; + } + \foreach \f in {0,1,2,3}{ + \draw ({20+\Cmargin},{(3-\f)*\Xscale}) -- (25,{(3-\f)*\Xscale}) node[inputarrow,rotate=0]{} node[right,align=left]{$\underline{X}[\f]$}; + } + \end{circuitikz} + \end{figure} + + The 4-point DFT can divided into two 2-point DFTs and a 4-point combination network (butterfly graph). The 2-point sub-DFTs are itself two 1-point DFTs and a 2-point combination network (butterfly graph). Ordering by even and odd indices must be considered. + + \task + The unit circle is equally divided by the primitive roots of unity. + \begin{equation} + \underline{w}_N = e^{- j \frac{2 \pi}{N}} + \end{equation} + + $N$ is the number of points of the FFT. $N$ must be a power of $2$, i.e., $1$, $2$, $4$, $8$, $16$, $32$, ..., $512$, $1024$, ... + + \begin{itemize} + \item The primitive root of unity of the 2-point DFT ($N = 2$) + \begin{equation*} + \underline{w}_2 = e^{- j \pi} + \end{equation*} + \item The primitive root of unity of the 4-point DFT ($N = 4$) + \begin{equation*} + \underline{w}_4 = e^{- j \frac{\pi}{2}} + \end{equation*} + \end{itemize} + + \task + At first, calculate the two 2-point sub-FFTs, called \emph{even} part ($\underline{E}[k]$) and \emph{odd} part ($\underline{O}[k]$). + \begin{equation*} + \begin{split} + \underline{E}[0] &= \underline{x}[0] + \underline{w}_2^{0} \cdot \underline{x}[2] = -2.5 \\ + \underline{E}[1] &= \underline{x}[0] + \underline{w}_2^{1} \cdot \underline{x}[2] = 1.5 \\ + \underline{O}[0] &= \underline{x}[1] + \underline{w}_2^{0} \cdot \underline{x}[3] = 3.0 \\ + \underline{O}[1] &= \underline{x}[1] + \underline{w}_2^{1} \cdot \underline{x}[3] = -1.0 + \end{split} + \end{equation*} + Note that both $\underline{E}[k]$ and $\underline{O}[k]$ are 2-periodic, i.e., $\underline{E}[2] = \underline{E}[0]$, ... + + Now combine the two parts, to the 4-point FFT. + \begin{equation*} + \begin{split} + \underline{X}[0] &= \underline{E}[0] + \underline{w}_2^{0} \cdot \underline{O}[0] = 0.5 \\ + \underline{X}[1] &= \underline{E}[1] + \underline{w}_2^{1} \cdot \underline{O}[1] = 1.5+1j \\ + \underline{X}[2] &= \underline{E}[0] + \underline{w}_2^{2} \cdot \underline{O}[0] = -5.5 \\ + \underline{X}[3] &= \underline{E}[1] + \underline{w}_2^{3} \cdot \underline{O}[1] = (1.5-1j) + \end{split} + \end{equation*} + + The result is the same like in a). + + \task + \begin{itemize} + \item a) and d) gave the same results. They both calculate the DFT. + \item a) required 16 multiplications. + \item d) required only 8 multiplications. + \item Multiplications require many computational resources. d) will perform better, because it requires fewer multiplications. + \end{itemize} + \end{tasks} \end{solution} diff --git a/exercise06/helpers/ex_6_1.py b/exercise06/helpers/ex_6_1.py new file mode 100644 index 0000000..2ba7096 --- /dev/null +++ b/exercise06/helpers/ex_6_1.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright 2020 Philipp Le +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. + +import numpy +import scipy.signal +import matplotlib.pyplot as plt + +b = [2, -1] +a = [1, -0.5] + +w, h = scipy.signal.freqz(b, a, 8) + +print(scipy.signal.tf2zpk(b,a)) + +w = numpy.round(w*100)/100 +h = numpy.round(h*100)/100 + +print("$\\phi$ in \\si{rad} & $\\left|\\underline{H}\\left(e^{j\\phi}\\right)\\right|$ & $\\arg\\left(\\underline{H}\\left(e^{j\\phi}\\right)\\right)$ in \\si{rad} \\\\") +for n in range(len(w)): + print("$%g$ & $%g$ & $%g$ \\\\" % (w[n], abs(h[n]), (numpy.angle(h[n])))) + +print() + +elems = [] +for n in range(len(w)): + elems.append("(%g, %g)" % (w[n], abs(h[n]))) +print("coordinates {%s}" % " ".join(elems)) + +elems = [] +for n in range(len(w)): + elems.append("(%g, %g)" % (w[n], (numpy.angle(h[n])))) +print("coordinates {%s}" % " ".join(elems)) + +fig = plt.figure() +ax1 = fig.add_subplot(1, 1, 1) +ax1.plot(w, abs(h), 'b') +ax2 = ax1.twinx() +ax2.plot(w, (numpy.angle(h)), 'g') +plt.show() diff --git a/exercise06/helpers/ex_6_2.py b/exercise06/helpers/ex_6_2.py new file mode 100644 index 0000000..dcbfc7a --- /dev/null +++ b/exercise06/helpers/ex_6_2.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright 2020 Philipp Le +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. + +import numpy +import scipy.signal +import matplotlib.pyplot as plt + +b = [1, 0.5+1j, 2] +a = [1] + +w, h = scipy.signal.freqz(b, a, 16, whole=True) + +print(scipy.signal.tf2zpk(b,a)) + +w = 1e6*w/numpy.pi + +w = numpy.round(w*100)/100 +h = numpy.round(h*100)/100 + +print("$f$ in \\si{kHz} & $\\left|\\underline{H}\\left(e^{j 2\\pi f T_S}\\right)\\right|$ & $\\arg\\left(\\underline{H}\\left(e^{j 2\\pi f T_S}\\right)\\right)$ in \\si{rad} \\\\") +for n in range(len(w)): + if w[n] <= 1e6: + print("$%g$ & $%g$ & $%g$ \\\\" % (w[n]/1000, abs(h[n]), (numpy.angle(h[n])))) + else: + print("$%g$ & $%g$ & $%g$ \\\\" % (w[n]/1000-2e3, abs(h[n]), (numpy.angle(h[n])))) + +print() + +elems = [] +for n in range(len(w)): + if w[n] <= 1e6: + elems.append("(%g, %g)" % (w[n], abs(h[n]))) + else: + elems.append("(%g, %g)" % (w[n]-2e6, abs(h[n]))) +print("coordinates {%s}" % " ".join(elems)) + +elems = [] +for n in range(len(w)): + if w[n] <= 1e6: + elems.append("(%g, %g)" % (w[n], (numpy.angle(h[n])))) + else: + elems.append("(%g, %g)" % (w[n]-2e6, (numpy.angle(h[n])))) +print("coordinates {%s}" % " ".join(elems)) + +fig = plt.figure() +ax1 = fig.add_subplot(1, 1, 1) +ax1.plot(w, abs(h), 'b') +ax2 = ax1.twinx() +ax2.plot(w, (numpy.angle(h)), 'g') +plt.show() diff --git a/exercise06/helpers/ex_6_4.py b/exercise06/helpers/ex_6_4.py new file mode 100644 index 0000000..0e1bb9e --- /dev/null +++ b/exercise06/helpers/ex_6_4.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright 2020 Philipp Le +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. + +import numpy + +x = numpy.array([-0.5, 1, -2, 2]) + +print("TEST ================================") + +print(numpy.fft.fft(x)) + +print("a) ==================================") + +for k in range(4): + eq_str = "\\underline{X}[%d] = " % (k) + elems = [] + res = 0 + for n in range(4): + #elems.append("\\underline{x}[%d] \\cdot e^{-j \\frac{2\\pi}{N} %d \\cdot %d}" % (n, k, n)) + elems.append("\\underline{x}[%d] \\cdot e^{-j \\frac{\\pi}{2} %d \\cdot %d}" % (n, k, n)) + res += x[n] * numpy.exp(-1j*(numpy.pi/2)*n*k) + eq_str += " + ".join(elems) + eq_str += " = " + str(res) + print (eq_str) + +print("d) ==================================") + +E = numpy.zeros(2) +O = numpy.zeros(2) +for k in range(2): + eq_str = "\\underline{E}[%d] = \\underline{x}[0] + \\underline{w}_2^{%d} \\cdot \\underline{x}[2]" % (k, k) + E[k] = x[0] + numpy.exp(-1j*(numpy.pi)*k) * x[2] + eq_str += " = " + str(E[k]) + print (eq_str) + + eq_str = "\\underline{O}[%d] = \\underline{x}[1] + \\underline{w}_2^{%d} \\cdot \\underline{x}[3]" % (k, k) + O[k] = x[1] + numpy.exp(-1j*(numpy.pi)*k) * x[3] + eq_str += " = " + str(O[k]) + print (eq_str) + +for k in range(4): + eq_str = "\\underline{X}[%d] = \\underline{E}[%d] + \\underline{w}_2^{%d} \\cdot \\underline{O}[%d]" % (k, (k%2), k, (k%2)) + res = E[k%2] + numpy.exp(-1j*(numpy.pi/2)*k) * O[k%2] + eq_str += " = " + str(res) + print (eq_str) |
