Page MenuHomec4science

3_structures.tex
No OneTemporary

File Metadata

Created
Sun, May 4, 12:05

3_structures.tex

\documentclass[aspectratio=169]{beamer}
\def\stylepath{../styles}
\usepackage{\stylepath/com303}
\begin{document}
\intertitle{Block diagrams}
\begin{frame}
\frametitle{Overview:}
\begin{itemize}
\item Algorithms for CCDE's
\item Block diagram
\item Real-time processing
\end{itemize}
\end{frame}
\begin{frame}[fragile] \frametitle{An old friend}
\begin{verbatim}
class Leaky:
def __init__(self, lmb):
self.lmb=lmb
self.y=0
def compute(self, x):
self.y = self.lmb * self.y + (1 - self.lmb) * x
return self.y
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Testing the code}
\begin{verbatim}
>>> from leaky import Leaky
>>> li = Leaky(0.95)
>>> for v in [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]:
>>> print(li.compute(v), end=' ')
0.0, 0.0, 0.0, 0.0, 0.0500000000000000, 0.047500000000000,
0.045125000000000, 0.042868750000000, 0.0407253125000000,
0.038689046875000, 0.0367545945312500
>>>
\end{verbatim}
\end{frame}
\begin{frame} \frametitle{Key points}
\begin{itemize}
\item we need a ``memory cell'' to store previous output
\item we need to initialize the storage before first use
\item we need 2 multiplications and one addition per output sample
\end{itemize}
\end{frame}
\begin{frame} \frametitle{Another old friend}
\[
y[n] = \frac{1}{M}\sum_{k=0}^{M-1}x[n-k]
\]
\end{frame}
\begin{frame}[fragile] \frametitle{Another old friend}
\begin{verbatim}
class MA:
def __init__(self, M):
self.M = M
self.buf = [0.0] * M
self.ix = 0
def compute(self, x):
self.buf[self.ix] = x
self.ix = (self.ix + 1) % self.M
res = 0.0
for v in self.buf:
res += v
return res / float(self.M)
\end{verbatim}
\end{frame}
\def\cell#1{\psframebox[framesep=.3]{\makebox[1.5em]{$\vphantom{\int}$#1}}}
\def\here{$ix\,\uparrow$}
\begin{frame}
\frametitle{The circular buffer}
\begin{figure}
\begin{dspBlocks}{0}{0.2}
~~~~~~ & 0 & 1 & 2 & 3 & 4 \\%
\only<1>{ & \cell{$0$} & \cell{$0$} & \cell{$0$} & \cell{$0$} & \cell{$0$} \\%
& \here & & & & \\}%
\only<2>{ & \cell{$x_0$} & \cell{$0$} & \cell{$0$} & \cell{$0$} & \cell{$0$} \\%
& & \here & & & \\}%
\only<3>{ & \cell{$x_0$} & \cell{$x_1$} & \cell{$0$} & \cell{$0$} & \cell{$0$} \\%
& & & \here & & \\}%
\only<4>{ & \cell{$x_0$} & \cell{$x_1$} & \cell{$x_2$} & \cell{$0$} & \cell{$0$} \\%
& & & & \here & \\}%
\only<5>{ & \cell{$x_0$} & \cell{$x_1$} & \cell{$x_2$} & \cell{$x_3$} & \cell{$0$} \\%
& & & & & \here \\}%
\only<6>{ & \cell{$x_0$} & \cell{$x_1$} & \cell{$x_2$} & \cell{$x_3$} & \cell{$x_4$} \\%
& \here & & & & \\}%
\only<7>{ & \cell{$x_5$} & \cell{$x_1$} & \cell{$x_2$} & \cell{$x_3$} & \cell{$x_4$} \\%
& & \here & & & \\}%
\only<8>{ & \cell{$x_5$} & \cell{$x_6$} & \cell{$x_2$} & \cell{$x_3$} & \cell{$x_4$} \\%
& & & \here & & \\}%
\only<9>{ & \cell{$x_5$} & \cell{$x_6$} & \cell{$x_7$} & \cell{$x_3$} & \cell{$x_4$} \\%
& & & & \here & \\}%
\only<6->{\ncloop[angleA=0,angleB=180,loopsize=-4em,arm=2em,linearc=.2,linecolor=darkred]{->}{2,6}{2,2}}
\end{dspBlocks}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Key points}
\begin{itemize}
\item we now need $M$ memory cells to store previous input values
\item we need to initialize the storage before first use
\item we need 1 division and $M$ additions per output sample
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{We can abstract from the implementation}
\begin{center}
\begin{figure}
\begin{dspBlocks}{2}{0.1}
$x[n]~~$ & & \\
& \BDadd & $~~x[n] + y[n]$ \\
$y[n]~~$ & & \\
\ncline{->}{1,1}{2,2}
\ncline{->}{3,1}{2,2}
\ncline{->}{2,2}{2,3}
\end{dspBlocks}
\vspace{1.5em}
\begin{dspBlocks}{2}{0.1}
$x[n]~~$ & & $~~\alpha x[n]$ \\
\ncline{->}{1,1}{1,3}\taput{$\alpha$}
\end{dspBlocks}
\vspace{1.5em}
\begin{dspBlocks}{2}{0.1}
$x[n]~~$ & \BDdelayN{N} & $~~x[n-N]$ \\
\ncline{->}{1,1}{1,2}\ncline{->}{1,2}{1,3}
\end{dspBlocks}
\end{figure}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Leaky Integrator}
\begin{center}
$y[n] = \lambda y[n-1] + (1-\lambda)x[n]$
\vspace{2em}
\begin{figure}
\begin{dspBlocks}{1.5}{1}
% 1 2 3 4 5
$x[n]~~$ & \BDadd & & \BDsplit & $~~y[n]$ \\
& & \BDdelay & &
\ncline{->}{1,1}{1,2}\taput{$1-\lambda$}\ncline{->}{1,2}{1,5}
\ncline{->}{2,2}{1,2}\ncline{2,2}{2,3}\taput{$\lambda$}\ncline{2,3}{2,4}
\ncline{2,4}{1,4}
\end{dspBlocks}
\end{figure}
\end{center}
\vspace{1em}
\end{frame}
\begin{frame}
\frametitle{Moving Average}
\begin{center}
$y[n] = \frac{1}{M}\sum_{k=0}^{M-1}x[n-k]$
\vspace{2em}
\begin{figure}
\begin{dspBlocks}{.15}{1}
$x[n]$~~ & & & \BDsplit & \BDdelay & \BDsplit & \BDdelay & \BDsplit & \BDdelay & \BDsplit & \hspace{3em} & & \BDdelay & \\
& & & & & \BDadd & & \BDadd & & \BDadd & \hspace{3em} & & & \BDadd & & & & & & & ~~$y[n]$
%\psset{linewidth=1.5pt}
\ncline{1,1}{1,3}\ncline{1,3}{1,5}
\ncline{1,5}{1,7}\ncline{1,7}{1,9}
\ncline{1,9}{1,10}
\ncline[linestyle=dotted]{1,10}{1,12}\ncline{1,12}{1,13}
\ncline{1,13}{1,14}
\ncline{1,4}{2,4}
\psset{arrows=->}
\ncline{2,4}{2,6}\ncline{1,6}{2,6}
\ncline{2,6}{2,8}\ncline{1,8}{2,8}
\ncline{2,8}{2,10}\ncline{1,10}{2,10}
\ncline{-}{2,10}{2,11}
\ncline[linestyle=dotted]{-}{2,10}{2,13}
\ncline{2,13}{2,14}\ncline{1,14}{2,14}
\ncline{2,14}{2,21}\taput{$1/M$}
\end{dspBlocks}
\end{figure}
\end{center}
\vspace{1em}
\end{frame}
\begin{frame}
\frametitle{The second-order section}
\[
y[n] + a_1 y[n-1] + a_2 y[n-2] = b_0 x[n] + b_1 x[n-1] + b_2 x[n-2]
\]
\pause
\vspace{2em}
\[
H(z)=\frac{b_0 + b_1 z^{-1} + b_2 z^{-2}}{1+a_1z^{-1}+a_2z^{-2}} = \frac{B(z)}{A(z)}
\]
\end{frame}
\begin{frame}
\frametitle{Second-order section, direct form I}
\begin{center}
\begin{figure}
\begin{dspBlocks}{1.2}{0.4}
$x[n]~~$ & \BDsplit & \BDadd & \BDadd & \BDsplit & ~~$y[n]$ \\%
& \BDdelay & & & \BDdelay \\%
& \BDsplit & \BDadd & \BDadd & \BDsplit \\%
& \BDdelay & & & \BDdelay \\%
& & & & \\%
& $B(z)$ & & & $1/A(z)$%
\psset{arrows=->,linewidth=1.5pt}
\ncline{-}{1,1}{1,2} \ncline{1,2}{1,3} \taput{$b_0$}
\ncline{1,3}{1,4} \ncline{-}{1,4}{1,5} \ncline{1,5}{1,6}
\ncline{3,2}{3,3} \taput{$b_1$} \ncline{3,5}{3,4}\taput{$-a_1$}
\ncline{-}{5,2}{5,3} \taput{$b_2$} \ncline{-}{5,5}{5,4}\taput{$-a_2$}
\ncline{1,2}{2,2} \ncline{2,2}{4,2} \ncline{-}{4,2}{5,2}
\ncline{5,3}{3,3} \ncline{3,3}{1,3}
\ncline{1,5}{2,5} \ncline{2,5}{4,5} \ncline{-}{4,5}{5,5}
\ncline{5,4}{3,4} \ncline{3,4}{1,4}
\end{dspBlocks}
\end{figure}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Second-order section, direct form I, inverted order}
\begin{center}
\begin{figure}
\begin{dspBlocks}{1.2}{0.4}
$x[n]$ & \BDadd & \BDsplit & \BDsplit & \BDadd & $y[n]$ \\%
& & \BDdelay & \BDdelay \\%
& \BDadd & \BDsplit & \BDsplit & \BDadd \\%
& & \BDdelay & \BDdelay \\%
& & & & \\%
&$1/A(z)$& & & $B(z)$%
\psset{arrows=->,linewidth=1.5pt}
\ncline{1,1}{1,2} \ncline{-}{1,2}{1,3} \ncline{-}{1,3}{1,4}
\ncline{1,4}{1,5} \taput{$b_0$}
\ncline{1,5}{1,6}
\ncline{3,3}{3,2} \taput{$-a_1$} \ncline{3,4}{3,5}\taput{$b_1$}
\ncline{-}{5,3}{5,2} \taput{$-a_2$} \ncline{-}{5,4}{5,5}\taput{$b_2$}
\ncline{1,3}{2,3} \ncline{2,3}{4,3} \ncline{-}{4,3}{5,3}
\ncline{5,2}{3,2} \ncline{3,2}{1,2}
\ncline{1,4}{2,4} \ncline{2,4}{4,4} \ncline{-}{4,4}{5,4}
\ncline{5,5}{3,5} \ncline{3,5}{1,5}
\end{dspBlocks}
\end{figure}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Second-order section, direct form II}
\begin{center}
\begin{figure}
\begin{dspBlocks}{1.2}{0.4}
$x[n]$ & \BDadd & \BDsplit & \BDadd & $y[n]$ \\%
& & \BDdelay & & \\%
& \BDadd & \BDsplit & \BDadd & \\%
& & \BDdelay & & \\%
& & \BDsplit & &
\psset{arrows=->,linewidth=1.5pt}
\ncline{1,1}{1,2} \ncline{-}{1,2}{1,3}
\ncline{1,3}{1,4} \taput{$b_0$} \ncline{1,4}{1,5}
\ncline{3,3}{3,2} \taput{$-a_1$} \ncline{3,3}{3,4}\taput{$b_1$}
\ncline{-}{5,3}{5,2} \taput{$-a_2$} \ncline{-}{5,3}{5,4}\taput{$b_2$}
\ncline{1,3}{2,3} \ncline{2,3}{4,3} \ncline{-}{4,3}{5,3}
\ncline{5,2}{3,2} \ncline{5,4}{3,4}
\ncline{3,2}{1,2} \ncline{3,4}{1,4}
\end{dspBlocks}
\end{figure}
\end{center}
\end{frame}
\begin{comment}
\begin{frame}
\frametitle{Real-time processing - speed}
If input samples arrive every $T$ seconds,
\begin{itemize}
\item each output sample must be computed in at most $T$ seconds
\item number of operations becomes important (IIR vs FIR)
\item some common tricks:
\begin{itemize}
\item circular buffers are size $2^K$ (mod operation faster)
\item exploit the parallelism some processor operations (fetch and compute)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Real-time processing - accuracy}
Processing unit have finite precision arithmetic:
\begin{itemize}
\item overflow or underflow are a real problem
\item more complex structures are more resilient (but less efficient)
\item some common tricks:
\begin{itemize}
\item use double precision in the accumulator
\item split the filter in low-order sections
\item use floating point
\end{itemize}
\end{itemize}
\end{frame}
\end{comment}
\end{document}

Event Timeline