% This file was automatically created from the m-file 
% "m2tex.m" written by USL. 
% The fontencoding in this file is UTF-8. 
%  
% You will need to include the following two packages in 
% your LaTeX-Main-File. 
%  
% \usepackage{color} 
% \usepackage{fancyvrb} 
%  
% It is advised to use the following option for Inputenc 
% \usepackage[utf8]{inputenc} 
%  
  
% definition of matlab colors: 
\definecolor{mblue}{rgb}{0,0,1} 
\definecolor{mgreen}{rgb}{0.13333,0.5451,0.13333} 
\definecolor{mred}{rgb}{0.62745,0.12549,0.94118} 
\definecolor{mgrey}{rgb}{0.5,0.5,0.5} 
\definecolor{mdarkgrey}{rgb}{0.25,0.25,0.25} 
  
\DefineShortVerb[fontfamily=courier,fontseries=m]{\$} 
\DefineShortVerb[fontfamily=courier,fontseries=b]{\#} 
  
\noindent                                                             
 \hspace*{-1.6em}{\scriptsize 1}$  $\color{mgrey}#%% Plant identification by noise injection#\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 2}$  clear, $\color{mdarkgrey}$clc, close all$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 3}$  $\\
 \hspace*{-1.6em}{\scriptsize 4}$  $\color{mgrey}$% Coefficients for H(z)$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  b = 0.05634*conv([1 0.9], [1 -1.0166 0.8]);$\\
 \hspace*{-1.6em}{\scriptsize 6}$  a = conv([1 -0.683], [1 -1.4461 0.7957]);$\\
 \hspace*{-1.6em}{\scriptsize 7}$  $\\
 \hspace*{-1.6em}{\scriptsize 8}$  figure, $\color{mdarkgrey}$zplane(b, a)$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  $\\
 \hspace*{-2em}{\scriptsize 10}$  sigma_x = 1; $\color{mgrey}$% average power of input noise$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 11}$  $\\
 \hspace*{-2em}{\scriptsize 12}$  $\color{mgrey}#%% Part (a)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  maxLag = 256;$\\
 \hspace*{-2em}{\scriptsize 14}$  N = 50e3;$\\
 \hspace*{-2em}{\scriptsize 15}$  x = sigma_x*randn(1, N);$\\
 \hspace*{-2em}{\scriptsize 16}$  y = filter(b, a, x);$\\
 \hspace*{-2em}{\scriptsize 17}$  c = xcorr(y, y, maxLag, $\color{mdarkgrey}$'unbiased'$\color{black}$);$\\
 \hspace*{-2em}{\scriptsize 18}$  $\\
 \hspace*{-2em}{\scriptsize 19}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 20}$  figure, $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 21}$  stem(-maxLag:maxLag, $\color{mdarkgrey}$c)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 22}$  xlabel($\color{mdarkgrey}$'m'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 23}$  ylabel($\color{mdarkgrey}$'Empir. autocorrelation function of y[n]'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 24}$  set(gca, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 25}$  axis([-$\color{mdarkgrey}$maxLag maxLag -0.02 max(c)])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 26}$  saveas(gca, $\color{mdarkgrey}$'../figs/hw04_q3a_emp_xcorr'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 27}$  $\\
 \hspace*{-2em}{\scriptsize 28}$  $\color{mgrey}#%% Part (b)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$  [C, $\color{mdarkgrey}$w] = calc_psd(c);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 30}$  [H, $\color{mdarkgrey}$w] = freqz(b, a, w);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$  $\\
 \hspace*{-2em}{\scriptsize 32}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 33}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 34}$  plot(w/pi, C, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 35}$  plot(w/pi, abs(H).^2, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 36}$  legend($\color{mdarkgrey}$'Empirical PSD'$\color{black}$, $\color{mdarkgrey}$'Theoretical PSD'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 37}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 38}$  ylabel($\color{mdarkgrey}$'PSD'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 39}$  set(gca, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 40}$  saveas(gca, $\color{mdarkgrey}$'../figs/hw04_q3b_emp_psd'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 41}$  $\\
 \hspace*{-2em}{\scriptsize 42}$  $\color{mgrey}#%% Part (d)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 43}$  logMag = log(sqrt(C))/sigma_x^2; $\color{mgrey}$% Log magnitude ln(|H|)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 44}$  $\\
 \hspace*{-2em}{\scriptsize 45}$  $\color{mgrey}$% Convolution function. $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 46}$  hHT = sin(w)./(1-cos(w));$\\
 \hspace*{-2em}{\scriptsize 47}$  hHT(w == 0) = 0; $\color{mgrey}$% enforces hHT = 0, when w = 0$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 48}$  $\\
 \hspace*{-2em}{\scriptsize 49}$  $\color{mgrey}$% Convolution integral$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 50}$  dw = abs(w(1))-abs(w(2)); $\color{mgrey}$% frequency resolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 51}$  P = -1/(2*pi)*conv(logMag, hHT, $\color{mdarkgrey}$'same'$\color{black}$)*dw;$\\
 \hspace*{-2em}{\scriptsize 52}$  $\\
 \hspace*{-2em}{\scriptsize 53}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 54}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 55}$  plot(w/pi, P, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 56}$  plot(w/pi, unwrap(angle(H)), $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 57}$  legend($\color{mdarkgrey}$'Estimated phase'$\color{black}$, $\color{mdarkgrey}$'Theoretical phase'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 58}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 59}$  ylabel($\color{mdarkgrey}$'Phase (rad)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 60}$  set(gca, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 61}$  saveas(gca, $\color{mdarkgrey}$'../figs/hw04_q3d_phase'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}