% 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}#%% Part 1: Modeling nonlinear plant with linear adaptive filter#\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 2}$  Niterations = 2000;             $\color{mgrey}$% Number of iterations$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 3}$  Nruns = 100;                    $\color{mgrey}$% Number of runs to average MSE$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 4}$  L = 9;                          $\color{mgrey}$% Memory length$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  var_r = 4;                      $\color{mgrey}$% variance of input signal$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 6}$  xtest = 2*cos(pi/10*(0:50));    $\color{mgrey}$% test signal$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 7}$  ytest = nonlinear_plant(xtest); $\color{mgrey}$% nonlinear plant output for test signal$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 8}$  Nweights = L + 2;               $\color{mgrey}$% number of weights: FIR taps + bias weight$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  $\\
 \hspace*{-2em}{\scriptsize 10}$  $\color{mgrey}$% Calculate mu$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 11}$  mu_max = 1/(var_r*(L+1) + 1);   $\color{mgrey}$% mu_max = 1/tr(R)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 12}$  mu = 0.01*mu_max;               $\color{mgrey}$% adaptation constant$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  $\\
 \hspace*{-2em}{\scriptsize 14}$  $\color{mgrey}$% Adaptation using LMS$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  MSE = zeros(Niterations, Nruns);$\\
 \hspace*{-2em}{\scriptsize 16}$  $#for#$ run = 1:Nruns$\\
 \hspace*{-2em}{\scriptsize 17}$      $\color{mgrey}$% Input signal: white and uniformly distributed zero-mean noise with$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 18}$      $\color{mgrey}$% var_r variance$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 19}$      r = sqrt(var_r*12)*(rand(Niterations, 1)-0.5);$\\
 \hspace*{-2em}{\scriptsize 20}$  $\\
 \hspace*{-2em}{\scriptsize 21}$      d = nonlinear_plant(r); $\color{mgrey}$% calculate desired response$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 22}$      $\\
 \hspace*{-2em}{\scriptsize 23}$      $\color{mgrey}$% Reset weights, input, and error before running LMS$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$      W = zeros(Nweights, 1); $\color{mgrey}$% weight vector$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$      X = zeros(L+1, 1); $\color{mgrey}$% shift register$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 26}$      error = zeros(Niterations, 1);$\\
 \hspace*{-2em}{\scriptsize 27}$      $#for#$ k = 1:Niterations$\\
 \hspace*{-2em}{\scriptsize 28}$          X = [r(k); X(1:end-1)];     $\color{mgrey}$% tap delay line$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$          Xin = [1; X];               $\color{mgrey}$% append input for bias weigth W(1)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 30}$          y = W.'*Xin;                $\color{mgrey}$% calculate output$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$          error(k) = d(k) - y;        $\color{mgrey}$% calculate error$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$          W = W + 2*mu*error(k)*Xin;  $\color{mgrey}$% weight update$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 33}$      $#end#$$\\
 \hspace*{-2em}{\scriptsize 34}$      MSE(:, $\color{mdarkgrey}$run) = error.^2;$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 35}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 36}$  $\\
 \hspace*{-2em}{\scriptsize 37}$  MSE = mean(MSE, 2); $\color{mgrey}$% average MSE over the number of runs$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 38}$  MMSE = mean(MSE(end-199:end)); $\color{mgrey}$% use last 200 samples to estimate MMSE$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 39}$  $\\
 \hspace*{-2em}{\scriptsize 40}$  $\color{mgrey}$% Ouput for test signal using linear model. W(1) is the bias weight$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 41}$  ymodel = filter(W(2:end), 1, xtest) + W(1);$\\
 \hspace*{-2em}{\scriptsize 42}$  $\\
 \hspace*{-2em}{\scriptsize 43}$  $\color{mgrey}#%% Plots#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 44}$  $\color{mgrey}$% Learning curve$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 45}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 46}$  plot(MSE(L+1:end))$\\
 \hspace*{-2em}{\scriptsize 47}$  plot([1 Niterations], MMSE*[1 1], $\color{mdarkgrey}$'--r'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 48}$  xlabel($\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 49}$  ylabel($\color{mdarkgrey}$'MSE'$\color{black}$, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 50}$  set(gca, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 51}$  legend($\color{mdarkgrey}$'Experimental learning curve'$\color{black}$, sprintf($\color{mdarkgrey}$'Estimated MMSE = %G'$\color{black}$, MMSE))$\\
 \hspace*{-2em}{\scriptsize 52}$  saveas(gca, $\color{mdarkgrey}$'figs/part1_learning_curve'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 53}$  $\\
 \hspace*{-2em}{\scriptsize 54}$  $\color{mgrey}$% Weights$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 55}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 56}$  stem(0:length(W)-1, W, $\color{mdarkgrey}$'fill'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 57}$  xlabel($\color{mdarkgrey}$'index'$\color{black}$, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 58}$  ylabel($\color{mdarkgrey}$'Weight'$\color{black}$, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 59}$  set(gca, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 60}$  saveas(gca, $\color{mdarkgrey}$'figs/part1_weights'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 61}$  $\\
 \hspace*{-2em}{\scriptsize 62}$  $\color{mgrey}$% Test signal$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 63}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 64}$  t = 0:50;$\\
 \hspace*{-2em}{\scriptsize 65}$  plot(t, xtest, $\color{mdarkgrey}$':k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 66}$  plot(t, ytest, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 67}$  plot(t, ymodel, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 68}$  xlabel($\color{mdarkgrey}$'Time'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 69}$  ylabel($\color{mdarkgrey}$'Amplitude'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 70}$  set(gca, $\color{mdarkgrey}$'Fontsize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 71}$  legend($\color{mdarkgrey}$'Nonlinear plant input'$\color{black}$, $\color{mdarkgrey}$'Nonlinear plant output'$\color{black}$,...$\\
 \hspace*{-2em}{\scriptsize 72}$      $\color{mdarkgrey}$'Adaptive filter output'$\color{black}$, $\color{mdarkgrey}$'Location'$\color{black}$, $\color{mdarkgrey}$'SouthEast'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 73}$  saveas(gca, $\color{mdarkgrey}$'figs/part1_test'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}