\documentclass[12pt]{report}
\setlength{\textwidth}{6.5 in} \setlength{\evensidemargin}{0
in} \setlength{\oddsidemargin}{0 in}
\setlength{\textheight}{9.40 in } \setlength{\topmargin}{-0.5 in}
\usepackage[pdftex]{graphicx} \usepackage{eso-pic}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{graphicx}
    \usepackage{ifthen}
    \usepackage{pdfpages}
\DeclareGraphicsExtensions{.pdf,.eps,.png}

\DeclareMathOperator{\E}{\mathbb{E}} % expectation

\makeatletter
\newcommand\xleftrightarrow[2][]{%
	\ext@arrow 9999{\longleftrightarrowfill@}{#1}{#2}}
\newcommand\longleftrightarrowfill@{%
	\arrowfill@\leftarrow\relbar\rightarrow}
\makeatother

\begin{document}
\thispagestyle{empty}
\begin{centering}
{\large Stanford University}\\
{\large EE 264: Digital Signal Processing}\\
{\large Summer, 2018} \\
\mbox{}\\
{\large Homework \#01}\\
\mbox{}\\
\end{centering}
\noindent Date assigned:  June 29, 2018 \hfill
Date due: July 6, 2018\\
\noindent \rule{6.5 in}{0.5pt}
%\mbox{}\\
\noindent {\bf Reading:}  This assignment covers lectures notes 1 and 2, which correspond to the chapters 2 and 3 of the text book {\bf DTSP 3e}. \\

\noindent {\bf Homework submission:}  Please submit your solutions on Gradescope. Create a single .pdf file containing all your analytical derivations, plots, and Matlab code. \\
\noindent
\rule{6.5 in}{0.5pt}
\mbox{}\\
\noindent
{\bf Problem 1: Difference equations, $z$-transform, and the DTFT (20 points)} \\
Consider the LTI systems described by the following difference equations:

\begin{description}
	\item[(i)] $y[n] = x[n] - 0.5x[n-1] + 0.5x[n-2]$
	\item[(ii)] $y[n] - 0.5y[n-1] + 0.2y[n-2] + 0.3y[n-3] = x[n] + 0.5x[n-1] + 0.1x[n-2] + 0.5x[n-3]$
\end{description}
\vspace{2.5mm}
\noindent
For each of these systems, do the following:
\begin{description}
	\item[(a)] Calculate the system $z$-transform $H(z) = \frac{Y(z)}{X(z)}$.
	\item[(b)] Sketch the pole-zero diagram of $H(z)$. Clearly label the location of the poles, zeros, and the ROC. Is this system causal? Is it stable? Please justify.\\
	\textit{Hint:} If you need to calculate the roots of a polynomial, you may use the Matlab function \texttt{roots}.
	\item[(c)] Plot the magnitude ($20\log_{10}(|H(e^{j\omega})|)$) and phase ($\arg(H(e^{j\omega}))$) responses of each system. \\
	\textit{Hint:} For this part you can use the Matlab function \texttt{freqz}.
	\item [(d)] Suppose that the input to both systems is the signal $x[n] = \cos(0.4\pi n)$. Which system would produce the output with highest amplitude? Justify your answer, you do not need to plot their output.
\end{description}

\vspace{5mm}
\noindent {\bf Problem 2: Echo cancellation (30 points)}

Echo cancellation is an application of signal processing that arises frequently in practice. The basic idea is that a signal of interest is corrupted by one or more echoes, which are delayed and scaled copies of the original signal. The goal of echo cancellation is to process the corrupted signal so as to recover the original signal.
\vspace{1.5mm}
There are two common models for echo generation. In the first echo-generation model, there are only a few echoes. An example is propagation of radio signals outdoors, where there are a few different transmission paths between the transmitting antenna and receiving antenna, each with a different path gain and path delay. This is called \textit{multipath} propagation. In the simplest instance, there is a primary path and just one echo. In discrete time, the system is described by the difference equation
\vspace{1.5mm}
\begin{equation}
y[n] = x[n] + \alpha x[n-N],
\end{equation}
where $N$ is the delay between the primary path and the echo, and $\alpha$ is the echo amplitude. Note that this system is \textit{nonrecursive} and has a \textit{finite impulse response (FIR)}.
\vspace{1.5mm}
In the second echo-generation model, there is an infinite sequence of echoes with decreasing amplitudes. This may occur in a concert hall, where sound is reflected off multiple surfaces. In discrete time, such a system is described by an $N$th-order difference equation:

\begin{equation}
y[n] = x[n] + \alpha y[n-N].
\end{equation}                                                           
Note that the second echo-generation system is \textit{autoregressive} or \textit{recursive} and has an \textit{infinite impulse response (IIR)}.

\vspace{2.5mm}
In this problem, you'll use each of those systems to generate an echoed signal, and then you'll design echo cancellation filters to recover the original signal. 

\vspace{3.5mm}

\noindent
\textbf{Generating an echoed signal:}

On Canvas, download the Matlab files \texttt{echo\_cancellation\_template.m} and \texttt{pulse.m} in \texttt{Files/Matlab/}. The \texttt{echo\_cancellation\_template.m} will use the function \texttt{pulse.m} to generate a sinusoidal pulse of frequency 400 Hz and duration 0.2 seconds. Feel free to play with these values. The sinusoidal pulse is sampled at a sampling rate of 8 kHz. When you run \texttt{echo\_cancellation\_template.m} you should see a plot of the signal waveform and hear the short sinusoidal pulse of frequency 400 Hz.

To generate the echoed signal, suppose that the delay between the primary path and the echo is of only $0.1$ seconds, and that the echo amplitude is $\alpha = 0.5$. Note that the sampling frequency is 8 kHz, hence samples are spaced by $1/(8\times 10^3) = 0.125$ ms. You can use the same $N$ and $\alpha$ for both echo generation systems.

\begin{description}
	\item[(a)] Write an equation for the $z$-transform of each of the ech generation systems.
	\item[(b)] For each system, generate the echoed signal by using the Matlab function \texttt{filter(b, a, x)}. Plot the echoed time-domain waveforms. Use the Matlab function \texttt{sound(x, fs)} to play the echoed signal and comment on what you hear.
	\item[(c)] Now the echo cancellation part, we will achieve perfect echo cancellation if we can design a system $G(z)$ such that $H(z)G(z) = 1$, where $H(z)$ is the echo generating system. Hence, we must have $G(z) = H^{-1}(z)$. Write an equation for $G(z)$ for each of the echo generating systems. For each case, is $G(z)$ IIR or FIR? Is it stable? Justify.
	\item [(d)] Implement the systems you found in part (c) to recover the original signal. Once again you can use the Matlab function \texttt{filter}. Plot the recovered waveforms and describe whether or not perfect echo cancellation was achieved. You can also hear the recovered signals by using the function \texttt{sound}.
\end{description}

\newpage
\noindent
{\bf Problem 3 (10 points)} \\
Consider the following model for how fixed-point arithmetic affects the implementation of an LTI system:
\setlength{\unitlength}{1in}
\begin{center}
        \begin{picture}(5.4,1)(0,0.2)
        \put(0,.8){\vector(1,0){1}}
                \put(1,.4){\framebox(1.2,.8){\shortstack{LTI
\\System
\#1\\ $h_1[n]$}}}
        \put(2.2,.8){\vector(1,0){.4}}
        \put(2.6,.8){\vector(1,0){.6}}
                \put(3.2,.4){\framebox(1.2,.8){\shortstack{LTI\\
System \#2 \\ $h_2[n]$}}}
                \put(4.4,.8){\vector(1,0){1}}
                \put(2.7,.8){\circle{.2}}
                \put(2.7,.7){\line(0,1){.2}}
                \put(2.7,.5){\vector(0,1){.2}}
                \put(0,.9){{$x[n]$}}
               \put(2.75,.45){{$e[n]$}}
               \put(2.8,.9){{$w[n]$}}
               \put(2.25,.9){{$v[n]$}}
                \put(5,.9){{$y[n]$}}
    \end{picture}
\end{center}

\vspace*{-.3in} \noindent where $w[n]=v[n]+e[n]$. The first system has impulse response $h_{1}[n] = \delta[n]+\delta[n-1]$ and the second system has frequency response \[
H_{2}(e^{j\omega}) = \frac{1}{1-0.9e^{-j\omega}}
\]


The input $x[n]$ is a stationary random signal with zero mean and power spectrum $\Phi_{xx}(e^{j\omega}) = 20$ for all $\omega$, while the internal noise input $e[n]$ is a white noise sequence with zero mean and autocorrelation $\phi_{ee}[m]=10\delta[m]$.  The signals $x[n]$ and $e[n]$ are assumed to be independent.
\begin{description}
\item{(a)} Use superposition to determine an equation for $\Phi_{yy}(e^{j\omega})$, the power
    spectrum of the output of the second filter, and either sketch it or plot it for $-\pi
    <\omega <\pi$.
    
\item{(b)} Use any convenient means to determine the total average power $\E\{y^2[n]\}$ of the output
    of the second filter.
\end{description}

\vspace{5mm}

\noindent
{\bf Problem 4 (25 points)} \\
Consider the following cascade system: \setlength{\unitlength}{1in}
\begin{center}
        \begin{picture}(5.4,1)(0,0.2)
        \put(0,.8){\vector(1,0){1}}
                \put(1,.4){\framebox(1.2,.8){\shortstack{LTI
\\System
\#1\\ $h_1[n]$}}}
        \put(2.2,.8){\vector(1,0){1}}
                \put(3.2,.4){\framebox(1.2,.8){\shortstack{LTI\\
System \#2 \\ $h_2[n]$}}}
                \put(4.4,.8){\vector(1,0){1}}
                \put(0,.9){{$x[n]$}}
                \put(2.5,.9){{$w[n]$}}
                \put(5,.9){{$y[n]$}}
    \end{picture}
\end{center}

\if(0)
\vspace*{-.3in} The first system is an {\em ideal highpass filter} with
\[
h_1[n]=\delta[n]-\frac{\sin \omega_c n}{\pi n} \Longleftrightarrow
H_1(e^{j\omega}) = \left\{\begin{array}{ll}
0 & \quad |\omega|<\omega_c \\
1 & \quad \omega_c< |\omega|<\pi
\end{array}
\right.
\]
\fi
\vspace*{-.3in} The first system is an {\em ideal bandpass filter} with
\[
h_1[n]=\frac{\sin \omega_b n}{\pi n}-\frac{\sin \omega_a n}{\pi n} \Longleftrightarrow
H_1(e^{j\omega}) = \left\{\begin{array}{ll}
0 & \quad |\omega|<\omega_a \\
1 & \quad \omega_a< |\omega|<\omega_b \\
0 & \quad \omega_{b}< |\omega| \leq \pi
\end{array}
\right.
\] where $0 < \omega_{a}< \omega_{b}<\pi$.
The second system is a time delay system with gain 5 defined by $y[n]= 5w[n-10]$. 

The input
$x[n]$ is a random signal with zero mean and autocorrelation
$\phi_{xx}[m]=20\delta[m]$.
\begin{description}
\item{(a)} Determine an equation for $\Phi_{ww}(e^{j\omega})$, the power
    spectrum of the output of the first filter, and sketch it for $-\pi
    <\omega <\pi$.
\item{(b)} From the power spectrum determined in part (a), determine an
    equation for the autocorrelation function $\phi_{ww}[m]$ of $w[n]$.
\item{(c)} Determine the average power $\E\{w^2[n]\}$ of the output
    of the first filter.
\item{(d)} Determine the average power $\E\{y^2[n]\}$, the
    autocorrelation $\phi_{yy}[m]$, and the power density spectrum
    $\Phi_{yy}(e^{j\omega})$ of the overall output $y[n]$.
\item{(e)}
Determine the cross-correlation function $\phi_{wy}[m]=\E\{w[n+m]y^{*}[n]\}$ between the input and output of the second system.
\end{description}

\vspace{5mm}

\noindent
{\bf Problem 5 (15 points)} \\
Consider an $L$-point moving average system defined by the difference equation
\begin{equation}
\label{eq:1}
y[n]=\frac{1}{L} \sum_{k=0}^{L-1} x[n-k].
\end{equation}
Further, suppose that the input is a Bernoulli random signal whose
values are either +1 or -1 with equal probability, and whose
autocorrelation function is $\phi_{xx}[m]=\delta[m]$; i.e., the
input is white noise with mean zero and average power equal to
unity.
\begin{description}
\item{(a)}
Sketch the first-order probability density function of the
input signal, $p_{\bf x}(x)$; the autocorrelation function of
the input signal, $\phi_{xx}[m]=\delta[m]$; and
$\Phi_{xx}(e^{j\omega})$, the power spectrum of the input signal.
\item{(b)}
 Determine a general expression for $\phi_{yy}[m]$, the autocorrelation
function of the output of the system. Sketch $\phi_{yy}[m]$ for $L=5$.
\item{(c)} Determine expressions for the mean and average power of
the output sequence $y[n]$.
\end{description}
\newpage
\end{document}
