Skip to content

Commit

Permalink
Update for spring 2020
Browse files Browse the repository at this point in the history
A light update, mostly to take care of minor-to-moderate errata. Renumbered Matlab lab exercises so that their numbering matches the chapter numbers in the textbook. Closes #8
  • Loading branch information
stiber committed Mar 29, 2020
1 parent 302a299 commit 7f94b9e
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 108 deletions.
Binary file modified Matlab Labs/Lab Book.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion Matlab Labs/Lab Book.tex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

\mbox{}\hfill
\parbox{1.5in}{\mbox{}}
\parbox{3in}{Fall 2016}\\[0.25in]
\parbox{3in}{Spring 2020}\\[0.25in]
\mbox{}\hfill
\parbox{3in}{\raggedright
Michael Stiber\\
Expand Down Expand Up @@ -148,6 +148,7 @@

\tableofcontents

\setcounter{section}{-1}
\include{lab1/lab1}
\include{lab2/lab2}
\include{lab3/lab3}
Expand Down
12 changes: 6 additions & 6 deletions Matlab Labs/lab1/lab1.tex
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ \subsection{Matlab in a (Very Small) Nutshell}
output.

Add three lines of code to your script, so that it will plot a cosine
on top of the sine in a different color. Use the \texttt{hold}
function to add a plot of
using the same axes as the sine (i.e., ``on top of the sine''). Use
the \texttt{hold} function to add a plot of
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
0.75*cos(2*pi*f*t)
\end{lstlisting}

to the plot. Save the plot using the MATLAB \texttt{print} command as
a PNG file named \texttt{step14.png} by typing:
to the plot. So, your final graph will have two functions
plotted. Save the plot using the MATLAB \texttt{print} command as a
PNG file named \texttt{step14.png} by typing:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
print -dpng step14
\end{lstlisting}
Expand Down Expand Up @@ -274,7 +274,7 @@ \subsection{Trigonometric Functions and Complex Mathematics in Matlab}


Include your code in your writeup. Additionally, include a plot of
\texttt{x = sumcos(20, [0 pi/4 pi/2 3*pi/2], 200, 0.25);} versus
\texttt{x = sumcos(20, [0 pi/4 pi/2 3*pi/2], [1 2 3 4], 200, 0.25);} versus
time.

Hint: the MATLAB \verb|length| function is useful in determining the
Expand Down
20 changes: 11 additions & 9 deletions Matlab Labs/lab2/lab2.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ \section{Let's Get Physical}
\subsection{Beating}

In this section, you will use the Matlab \texttt{AnalogSignal} class
to simulate an analog signal generator. The constructor takes the
following arguments:
(described previously in lab~1 and its
figure~\ref{fg:analogsignal-help}) to simulate an analog signal
generator. The constructor takes the following arguments:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
% AnalogSignal(type, amplitude, frequency, dur)
% Where:
Expand All @@ -32,7 +33,7 @@ \subsection{Beating}

\paragraph{Step 1.1} Verify that you can get a triangle wave. What is
the code to generate and plot a triangle wave ranging from -1 to 1
Volts with a frequency of 10Hz and a duration of 1sec?
Volts with a frequency of 10Hz and a duration of 1 second?

\paragraph{Step 1.2} Next, verify that you can generate a sine wave of
1 second duration at a frequency of 440Hz, ranging from -1 to +1. What
Expand All @@ -43,9 +44,10 @@ \subsection{Beating}
\itshape{Xmax}])} function call to set the X axis limits so that the
waveform is apparent (i.e., you're not just plotting a solid blob).

\paragraph{Step 1.3} Generate sine waves of identical range and
duration, but with frequencies of 442, 444, and 448 Hz. Now, generate
the sums of 440Hz and each of these new signals to generate beating
\paragraph{Step 1.3} Generate three sine waves of identical range and
duration, but with frequencies of 442, 444, and 448 Hz. Next, generate
the three sums of 440Hz and each of these new signals separately (so,
440Hz + 442Hz, 440Hz + 444Hz, and 440Hz + 448Hz) to generate beating
akin to tuning an instrument against the 440Hz standard. Play each sum
signal; can you hear the beating? Plot each sum signal for its full 1s
duration. The beating ``envelope'' should be obvious. What is the beat
Expand All @@ -57,8 +59,8 @@ \subsection{Fourier series representation of a physical signal}


\paragraph{Step 2.1} Recall that any periodic signal can be
represented as a sum of harmonic sinusoids. The amplitude of each
harmonic is known as the Fourier Series. It may at first seem like
represented as a sum of harmonic sinusoids. The amplitudes of these
harmonics is collectively known as the Fourier Series. It may at first seem like
sums of sinusoids would be poor approximations of real periodic
signals, but this is not the case. We can illustrate this using a
triangle wave. The formula for synthesis of a triangle wave with
Expand All @@ -75,7 +77,7 @@ \subsection{Fourier series representation of a physical signal}
in Hz, and so $\omega_0 = 2\pi f_0$. Notice that the Fourier Series
of the triangle wave only uses odd harmonics (i.e., the only
non-zero frequencies are $(2k+1)\omega_0=\omega_0, 3\omega_0,
5\omega_0 \cdots$). Also notice that resulting wave will have zero
5\omega_0 \cdots$). Also notice that the resulting wave will have zero
mean because there is no ``DC'' term (i.e., $2k+1 \neq 0$ for any
integer k).

Expand Down
29 changes: 19 additions & 10 deletions Matlab Labs/lab3/lab3.tex
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ \subsection{Sampling}
sampled versions of this signal at 300Hz, 500Hz, 1000Hz, and
2000Hz. Use the Matlab \texttt{subplot} command, and the
\texttt{discreteplot} function provided with this class's Matlab code,
to plot the original and all four sampled signals together. Clearly,
the results are not the same, and none look identical to the original
sine wave. What are the two essential pieces of information about a
sine wave that need to be preserved when sampling it? Does it appear
that all sampled versions are equally useful in achieving this? Why or
why not (in other words, your answer to this question should not be
just ``yes'' or ``no'')?
to plot the original and all four sampled signals together. One of the
things that you will note from these plots is that the X axes of these
plots do not have units of continuous time; their units are \emph{not}
seconds. Instead, the X axis values are sample count, starting with
sample 1. You will want these plots to show their signals over the
same time duration, and so you'll need to choose that duration,
then figure out, for each sampled signal, how many samples correspond
to that duration, and then set the X axis limits for each figure so
that the plots show equal durations.

Clearly, the results are not the same, and none look identical to the
original sine wave. What are the two essential pieces of information
about a sine wave that need to be preserved when sampling it? Does it
appear that all sampled versions are equally useful in achieving this?
Why or why not (in other words, your answer to this question should
not be just ``yes'' or ``no'')?

\paragraph{Step 1.3} Let's look at aliasing in a little more detail
and with a lot more numerical precision. You'll recall from the text
Expand Down Expand Up @@ -121,13 +130,13 @@ \subsection{Analog to Digital Conversion}
2, 4, 8, 12, and 16 bits quantization, and plot SNR on the Y-axis
versus number of quantization bits on the X-axis.

\paragraph{Step 2.3} Repeat Step 3.2 using a square waveform with
\paragraph{Step 2.3} Repeat Step 2.2 using a square waveform with
the same parameters.

\paragraph{Step 2.4} Repeat Step 3.2 using a triangle waveform with
\paragraph{Step 2.4} Repeat Step 2.2 using a triangle waveform with
the same parameters.

\paragraph{Step 2.5} As you double the number of bits used in
quantization, how does the SNR change? How does this compare to what
your learned from the textbook? Refer to specific features of your
plots from Steps~3.2--3.4 to justify your answer.
plots from Steps~2.2--2.4 to justify your answer.
11 changes: 7 additions & 4 deletions Matlab Labs/lab4/lab4.tex
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ \subsection{Overview of Filtering and Matlab}

You can visualize the zero locations with the following code:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
plot(r, 'o')
plot(complex(r), 'o')
rectangle('Position', [-1 -1 2 2], 'Curvature', [1 1])
line([-1 1], [0 0], 'Color', [0 0 0])
line([0 0], [-1 1], 'Color', [0 0 0])
axis equal
\end{lstlisting}
Here, we use the unfortunately named \verb|rectangle| function
to draw the unit circle and the \verb|line| function to draw the real
and imaginary axes.
Here, we use the unfortunately named \verb|rectangle| function to draw
the unit circle and the \verb|line| function to draw the real and
imaginary axes. Note also that we have to make sure that the value of
$r$ that we pass to the \verb|plot()| function is complex (since we
want to plot it on the complex plane) using the \verb|complex()|
function, because the roots of a polynomial can be real.

Finally, you can compute and plot the magnitude of the filter's
frequency response pretty directly in Matlab:
Expand Down
2 changes: 1 addition & 1 deletion Matlab Labs/lab6/lab6.tex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ \subsection{Feedback Filters as Recurrence Relations}
\paragraph{Step 1.4} Let's do something similar with the recurrence
relation for computing the series $y[n] = 1/3^n$ in the text (as
always, remember that Matlab indices start at 1). Set the
coefficients for a feedback filter to implement equation~(5-42) in the
coefficients for a feedback filter to implement equation~(5-43) in the
text, $y[n] = 1/3 y[n-1] + x[n]$. What are the filter coefficients?


Expand Down
2 changes: 1 addition & 1 deletion Matlab Labs/lab7/lab7.tex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ \subsection{Using the DFT}

\paragraph{Step 2.4} Replace the sum of two sinusoids with your
\verb|DTMFCoder| function from lab 6. Input a selection of button
values. Does the FFT block show the separate frequencies for each
values. Do the FFT function and plot show the separate frequencies for each
button?


Expand Down
33 changes: 18 additions & 15 deletions Matlab Labs/lab8/lab8.tex
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,22 @@ \subsection{Lossy image coding: JPEG}

\paragraph{Step 3.3} Let's quantize the image's spectral
content. First, find the number of zero elements in the FFT, using
something like \verb|origZero=length(find(abs(a)==0));|, where
\verb|a| is the FFT. \emph{Remember to exclude the DC value in the
something like \verb|origZero=length(find(abs(origX)==0));|, where
\verb|origX| is the FFT. \emph{Remember to exclude the DC value in the
\texttt{fft2} output in figuring out this range.} Then, zero out
additional frequency components by zeroing out all with magnitudes
below some threshold. You'll want to set the threshold somewhere
between the min and max magnitudes of \verb|a|, which you can get as
\verb|mn=min(min(abs(a)));| and \verb|mx=max(max(abs(a)));|. Let's
between the min and max magnitudes of \verb|origX|, which you can get as
\verb|mn=min(min(abs(origX)));| and \verb|mx=max(max(abs(origX)));|. Let's
make four tests, with thresholds 5\%, 10\%, 20\%, and 50\% of the way
between the min and max, i.e., \verb|th=0.05*(mx-mn)+mn;|. Zero out
all FFT values below the threshold using something like:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
b = a;
b(abs(a)<th) = 0; % Uses logical array indexing
compX = origX;
compX(abs(origX)<th) = 0; % Uses logical array indexing
\end{lstlisting}
You can count the number of elements thresholded by finding the number
of zero elements in \verb|b| at this point and subtracting the number
of zero elements in \verb|compX| at this point and subtracting the number
that were originally zero (i.e., \verb|origZero|). This is an estimate
of the amount the image could be compressed with an entropy
coder. Express the number of thresholded elements as a fraction of the
Expand All @@ -270,17 +270,20 @@ \subsection{Lossy image coding: JPEG}
you suggest an approach that will take this into account? How does the
JPEG algorithm deal with or avoid this problem?

\begin{sloppypar}
\paragraph{Step 3.4} Now we will see the effect of this thresholding
on image quality. Convert the thresholded FFT back to an image using
something like \verb|c = abs(ifft2(b));|. For each type of image and
each threshold value, plot the original image and the final processed
image. Compute the mean squared error (MSE) between the original and
reconstructed image (mean squared error for matrices can be computed
as \verb|mean(mean((a-c).^2))|). What can you say about the effects
on the image and MSE? Collect your code together as a script to
automate the thresholding and reconstruction, so you can easily
compute MSE for a number of thresholds. Plot MSE vs. threshold
something like \verb|reconstructed = abs(ifft2(compX));|. For each
type of image and each threshold value, plot the original image and
the final processed image. Compute the mean squared error (MSE)
between the original and reconstructed image (mean squared error for
matrices can be computed as
\verb|mean(mean((original-reconstructed).^2))|). What can you say
about the effects on the image and MSE? Collect your code together as
a script to automate the thresholding and reconstruction, so you can
easily compute MSE for a number of thresholds. Plot MSE vs. threshold
percentage (just as you plotted fraction of pixels thresholded
vs. threshold in step 3.3).
\end{sloppypar}

% LocalWords: WebQ MATLAB DSP
1 change: 1 addition & 0 deletions Matlab/AnalogSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ function soundsc(sig)

function y = square(f, t)
% SQUARE Generate a square wave with values in range [0, 1]
% with 50% duty cycle
% Y = SQUARE(F, T)
% Where:
% F = number of positive/negative waves per second
Expand Down
Binary file modified Signal Computing.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions Signal Computing.tex
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
\mbox{}\hfill
\parbox{1.5in}{\mbox{}}
\parbox{3in}{\raggedright
Winter 2018\\[0.25in]
Spring 2020\\[0.25in]
Michael Stiber\\
Bilin Zhang Stiber\\
University of Washington Bothell\\
Expand All @@ -275,7 +275,7 @@
\newpage
\mbox{}\vspace{2in}
\begin{flushright}
Copyright \copyright\ 2002--2018 by Michael and Bilin Stiber and Eric
Copyright \copyright\ 2002--2020 by Michael and Bilin Stiber and Eric
C. Larson\\[1in]
This material is based upon work supported by the National Science
Foundation under Grant No. 0443118.\\[1in]
Expand Down
2 changes: 1 addition & 1 deletion ch-conv/convolution.tex
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ \subsection{Example: z-transform of exponential signal}
\end{equation}
or
\begin{equation}
u[n] = \{1, 1, 1, \ldots\}, \quad k \ge 0
u[n] = \{1, 1, 1, \ldots\}, \quad n \ge 0
\end{equation}

\index{unit step}
Expand Down
46 changes: 24 additions & 22 deletions ch-fft/fft.tex
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ \subsection{The Fast Fourier Transform Algorithm}
\ENDIF
\STATE Shuffle the $x[n]$ in bit-reversed order
\STATE $\mathit{Size} = 2$ \COMMENT{$\mathit{Size}=1$ DFTs already done}
\WHILE{$\mathit{Size} < N$}
\WHILE{$\mathit{Size} \leq N$}
\STATE Compute $N/\mathit{Size}$ DFTs from the existing ones as
$X_{\mathit{Size}/2}[k]^{\mathit{even}} +
e^{-jk2\pi/\mathit{Size}}X_{\mathit{Size}/2}[k]^{\mathit{odd}}$
Expand Down Expand Up @@ -714,36 +714,38 @@ \subsubsection{Example: 8-Point FFT}
1\}$, $n = \{0, 1, 2, 3, 4, 5, 6, 7\}$. The first thing we'll do is
write the signal out as a 1D array, with indices in binary:
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|} \hline
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 \\ \hline
000 & 001 & 010 & 011 & 100 & 101 & 110 & 111 \\ \hline
\begin{tabular}{r|c|c|c|c|c|c|c|c|} \hline
$x[n]$ & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 \\ \hline
$n$ & 000 & 001 & 010 & 011 & 100 & 101 & 110 & 111 \\ \hline
\end{tabular}
\end{center}

Next, we sort the elements according to their bit-reversed indices:
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|} \hline
0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
000 & 100 & 010 & 110 & 001 & 101 & 011 & 111 \\ \hline
\begin{tabular}{r|c|c|c|c|c|c|c|c|} \hline
$x[n]$ & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
$n$ 000 & 100 & 010 & 110 & 001 & 101 & 011 & 111 \\ \hline
\end{tabular}
\end{center}

Each of these elements is already a 1-point FFT; we now begin merging
them into 2-point ones ($X_2[0]$ and $X_2[1]$), as in
them into 2-point ones ($X_2[0]= x[\mathit{even}] + x[\mathit{odd}]$
and $X_2[1]= x[\mathit{even}] - x[\mathit{odd}]$), as in
equations~(\ref{eq:2fft-even}) and~(\ref{eq:2fft-odd}):
\begin{center}
\begin{tabular}{|c|c||c|c||c|c||c|c|} \hline
0 & 0 & 0 & 0 & 2 & 0 & 2 & 0 \\ \hline
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 \\ \hline
\begin{tabular}{r|c|c||c|c||c|c||c|c|} \hline
$X_2[k]$ & 0 & 0 & 0 & 0 & 2 & 0 & 2 & 0 \\ \hline
$k$ & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 \\ \hline
\end{tabular}
\end{center}

Now we have four 2-point FFTs; we now merge adjacent pairs of
points. So, we will be computing $X_4[k]$ for $k=\{0, 1, 2, 3\}$. To
compute elements 2 and 3, we need to remember
equations~(\ref{eq:fft-repeat-even}) and~(\ref{eq:fft-repeat-odd}): we
get $X_2[2] = X_2[0]$ and $X_2[3] = X_2[1]$. The resulting equations
are:
compute elements $k=2$ and $k=3$, we need to remember
equations~(\ref{eq:fft-repeat-even}) and~(\ref{eq:fft-repeat-odd}) ---
that, since the transform $X$ is periodic, we simply repeat the
2-point FFTs to get the ``extra'' two points. So, $X_2[2] = X_2[0]$
and $X_2[3] = X_2[1]$. The resulting equations are:
\begin{align*}
X_4[0] &= X_2[0]^{\mathit{even}} + e^{-j(0)2\pi/4}X_2[0]^{\mathit{odd}} \\
X_4[1] &= X_2[1]^{\mathit{even}} + e^{-j(1)2\pi/4}X_2[1]^{\mathit{odd}} \\
Expand All @@ -767,9 +769,9 @@ \subsubsection{Example: 8-Point FFT}
\end{align*}
yielding:
\begin{center}
\begin{tabular}{|c|c|c|c||c|c|c|c|} \hline
0 & 0 & 0 & 0 & 4 & 0 & 0 & 0 \\ \hline
0 & 1 & 2 & 3 & 0 & 1 & 2 & 3 \\ \hline
\begin{tabular}{r|c|c|c|c||c|c|c|c|} \hline
$X_4[k]$ & 0 & 0 & 0 & 0 & 4 & 0 & 0 & 0 \\ \hline
$k$ & 0 & 1 & 2 & 3 & 0 & 1 & 2 & 3 \\ \hline
\end{tabular}
\end{center}

Expand Down Expand Up @@ -798,9 +800,9 @@ \subsubsection{Example: 8-Point FFT}

The final result is:
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|} \hline
4 & 0 & 0 & 0 & -4 & 0 & 0 & 0 \\ \hline
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \hline
\begin{tabular}{r|c|c|c|c|c|c|c|c|} \hline
$X_8[k]$ & 4 & 0 & 0 & 0 & -4 & 0 & 0 & 0 \\ \hline
$k$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \hline
\end{tabular}
\end{center}

Expand Down Expand Up @@ -1269,7 +1271,7 @@ \subsubsection{Hamming Window}

\begin{figure}
\centerline{\includegraphics[width=4in]{ch-fft/ufft_hamming_w512}}
\caption{A 512-point Hann window in the time domain.\label{fig:ufft-hmw}}
\caption{A 512-point Hamming window in the time domain.\label{fig:ufft-hmw}}
\end{figure}

\index{FFT!Hamming window and}
Expand Down
Loading

0 comments on commit 7f94b9e

Please sign in to comment.