\documentclass{article}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{graphicx}
\usepackage{listings}

\lstset{% Default options for all algorithm listings read
  language=matlab,
  %labelstep=5,
  %frame=trbl,
  %indent=15pt,
  %indent=12pt,
  showlines=false,
  stringstyle=\itshape%,
  %basicstyle=\ttfamily\footnotesize
}

\author{Nada Amin\\
\texttt{namin@mit.edu}
}

\title{6.003 Lab 1}

\begin{document}

\date{March 3, 2005}

\maketitle

\pagestyle{myheadings}
\markright{Nada Amin (6.003 Lab 1) (3/2/05)}

\section*{Basic Problems}
\subsection*{(a)}
The vector $he$ is $1$ at $n=0$, $\alpha = 0.5$ at $n=N=1000$ and null
in between.

\begin{figure}[htp]
   \centering
   \includegraphics[width=1\textwidth]{fig-a}
   \caption{Plot of $he[n]$}
   \label{fig:fig-a}
\end{figure}

\subsection*{(b)}
\subsubsection*{(b.1) Impulse Response of the Echo System}
\begin{align*}
y[n] = x[n] + \alpha x[n-N]
\end{align*}
I compute the impulse response $h_y$, assuming the condition of initial rest.
\begin{align*}
h_y[n] &&= 0 &\text{ if $n < 0$ (initial rest)}\\
h_y[0] &= \delta[0] + \alpha \delta[0-N] &= 1\\
h_y[n] &&= 0 & \text{ if $0 < n < N$}\\
h_y[N] &= \delta[N] + \alpha \delta[N-N] &= \alpha\\
h_y[n] &&= 0 &\text{ if $n> N$}
\end{align*}
Thus, $h_y[n] = \delta[n] + \alpha \delta[n-N]$.

\subsubsection*{(b.2) Impulse Response of the Echo Removal System}
\begin{align*}
z[n] + \alpha z[n-N] = y[n]\\
z[n] = y[n] - \alpha z[n-N]\\
\end{align*}
I compute the impulse response $h_z$, assuming the condition of initial rest.
\begin{align*}
h_z[n] &&&= 0 &\text{ if $n < 0$ (initial rest)}\\
h_z[0] &&= \delta[0] - \alpha h_z[0-N] &= 1\\
h_z[n] &&&= 0 &\text{ if $0 < n < N$}\\
h_z[N] &&= \delta[N] - \alpha h_z[N-N] &= -\alpha\\
h_z[n] &&&= 0 &\text{ if $N < n < 2N$}\\
h_z[2N] &&= \delta[2N] - \alpha h_z[2N-N] &= \alpha^2\\
h_z[n] &&&= 0 &\text{ if $n \neq kN$}\\
h_z[kN] &&= \delta[kN] - \alpha h_z[kN-N] &= (-\alpha)^k\\
%h_z[kN] &= \delta[kN] - \alpha h_z[kN-N] &= (-\alpha)^k
\end{align*}
Thus, $h_z[n] = \sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN]$.

\subsubsection*{(b.3) Verifying Echo Removal System $=$ Inverse of Echo System}
I now verify that the echo removal system is the inverse of the echo
system by showing that $h_y[n] * h_z[n] = \delta[n]$.
\begin{align*}
 & h_y[n] * h_z[n]\\
=& (\delta[n] + \alpha \delta[n-N]) * (\sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN])\\
=& (\delta[n] * \sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN]) + (\alpha \delta[n-N] * \sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN])\\
=& \sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN] - \sum_{k=0}^{\infty}(-\alpha)^{k+1}\delta[n-(k+1)N]\\
=& \delta[n] + \sum_{k=1}^{\infty}(-\alpha)^k\delta[n-kN] - \sum_{k=1}^{\infty}(-\alpha)^k\delta[n-kN]\\
=& \delta[n]
\end{align*}

\pagebreak
\section*{Intermediate Problems}
\subsection*{(c)}
\begin{figure}[htp]
   \centering
   \includegraphics[width=1\textwidth]{fig-c}
   \caption{Plot of $her[n]$}
   \label{fig:fig-c}
\end{figure}

\pagebreak
\subsection*{(d)}
\begin{figure}[htp]
   \centering
   \includegraphics[width=1\textwidth]{fig-d}
   \caption{Plot of $z[n]$, the filter output of $y$}
   \label{fig:fig-d}
\end{figure}

\pagebreak
\subsection*{(e)}
I convolve $her$ with $he$ and store the result in $hoa$. Though I
computed $her$ to be the inverse of $he$, the convolution of the two,
$hoa$, is not a unit impulse because the actual impulse response
of the inverse of $he$ is infinite and $her$ is only an approximation. 
More precisely, 
$h_z[n] = \sum_{k=0}^{\infty}(-\alpha)^k\delta[n-kN]$
but 
$her[n] = \sum_{k=0}^{4}(-\alpha)^k\delta[n-kN]$.
\begin{figure}[htp]
   \centering
   \includegraphics[width=1\textwidth]{fig-e}
   \caption{Plot of $hoa[n]$}
   \label{fig:fig-e}
\end{figure}

\pagebreak

\section*{Advanced Problems}
\subsection*{(f)}
The autocorrelation function $R_{ww}[n]$ of a signal $w[n]$ is defined
as follows:
\begin{align*}
R_{ww}[n] = w[n] * w[-n]
\end{align*}

I now write $R_{yy}[n]$ in terms of $R_{xx}[n]$:
\begin{align*}
R_{yy}[n] &= y[n] * y[-n]\\
&= (x[n] * h[n]) * (x[-n] * h[-n])\\
&= (x[n] * x[-n]) * (h[n] * h[-n])\\
&= R_{xx}[n] * R_{hh}[n]
\end{align*}
where $R_{hh}[n]$ is:
\begin{align*}
R_{hh}[n] &= h[n] * h[-n]\\
&= (\delta[n] + \alpha\delta[n-N]) * (\delta[-n] + \alpha\delta[-n-N])\\
&= (\delta[n] + \alpha\delta[n-N]) * (\delta[n] + \alpha\delta[n+N])\\
&= (1+\alpha²)\delta[n] + \alpha (\delta[n-N] + \delta[n+N])
\end{align*}

\begin{align*}
R_{yy}[n] = (1+\alpha²)R_{xx}[n] + \alpha (R_{xx}[n-N] + R_{xx}[n+N])
\end{align*}

Since I know that $R_{xx}[0]$ is a global maximum, i infer that
$R_{yy}[n]$ has a big peak at $n=0$ and two smaller peaks at $n=N$ and
$n=-N$. By detecting these smaller peaks, I can estimate $N$. I use
the difference equation $y[n] = x[n]-x[n+1]+x[n]-x[n+1]$ to detect
those peaks.

Once I found $N$, $\alpha$ is trivial to find. I can use any value
$n\geq N$ in the original difference equation for the system.
\begin{align*}
y[n] &= x[n] + \alpha x[n-N] &\\
\alpha &= \frac{y[n]-x[n]}{x[n-N]} & \text{for $n \geq N$}
\end{align*}

For $y3$, I needed to be a bit more ingenious because it contains two
echoes, not just one. I find $N_1$, the delay of the echo with the
largest $\alpha$, by looking at how far the largest uncentered peak is
from the centered peak in the auto-correlation signal $R_{y3y3}[n]$. I
then estimate $\alpha_1$ to be the approximated ratio of the found
peak with the center peak of the auto-correlation signal
$R_{xx}[n]$. Having found $N_1$ and $\alpha_1$, I filter the largest
echo from the output signal. I auto-correlate this filtered signal,
and again look for the largest peak away from the center, which gives
me $N_2$. I then use the original difference equation for the double
echo system to find $\alpha_2$.
\begin{align*}
y[n] &= x[n] + \alpha_1 x[n-N_1] +\alpha_2 x[n-N_2] &\\
\alpha_2 &= \frac{y[n]-x[n]-\alpha_1x[n-N_1]}{x[n-N_2]} & \text{for $n \geq \max(N_1,N_2)$}
\end{align*}

For $y$, I find that $N=1000$ and $\alpha = 0.5$, for $y2$, $N=250$
and $\alpha = 0.7$, and for $y3$, $N_1=1500$, $\alpha_1 = 0.6$,
$N_2=2500$, $\alpha_2 = 0.3$.

\begin{figure}[htp]
   \centering
   \includegraphics[height=0.4\textheight]{fig-rhh}
   \caption{Plot of $R_{hh}[n]$}
   \label{fig:fig-rhh}
\end{figure}

\begin{figure}[htp]
   \centering
   \includegraphics[height=0.4\textheight]{fig-ryy}
   \caption{Plot of $R_{yy}[n]$}
   \label{fig:fig-ryy}
\end{figure}


\begin{figure}[htp]
   \centering
   \includegraphics[height=0.4\textheight]{fig-ry2y2}
   \caption{Plot of $R_{y2y2}[n]$}
   \label{fig:fig-ry2y2}
\end{figure}

\begin{figure}[htp]
   \centering
   \includegraphics[height=0.4\textheight]{fig-ry3y3}
   \caption{Plot of $R_{y3y3}[n]$}
   \label{fig:fig-ry3y3}
\end{figure}

\pagebreak

\section*{MATLAB Code}
\subsection*{\texttt{lab1.m}}
\lstinputlisting{lab1.m}

\subsection*{\texttt{detect1.m}}
\lstinputlisting{detect1.m}

\subsection*{\texttt{alpha1.m}}
\lstinputlisting{alpha1.m}

\end{document}

