% 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}#%% Noise canceling#\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}$  [s, Fs] = audioread($\color{mdarkgrey}$'guitartune.wav'$\color{black}$); $\color{mgrey}$% Fs is sampling frequency$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  $\\
 \hspace*{-1.6em}{\scriptsize 6}$  Nruns = 20; $\color{mgrey}$% number of independent runs to average learning curve$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 7}$  var_r = 2e-3; $\color{mgrey}$% variance of white Gaussian noise$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 8}$  $\\
 \hspace*{-1.6em}{\scriptsize 9}$  L = 49; $\color{mgrey}$% adaptive filter order. L+1 coefficients$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 10}$  $\\
 \hspace*{-2em}{\scriptsize 11}$  $\color{mgrey}$% H(z) filter coefficients $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 12}$  $\color{mgrey}$% Although H(z) is "unknown", for developing your answers you can use this$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  $\color{mgrey}$% filter. It is a eighth-order IIR Chebyshev type II filter with bandwidth$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 14}$  $\color{mgrey}$% 0.55*Fs. Chebyshev type II filters have constant gain at the passband, $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  $\color{mgrey}$% but they exhibit ripples in the stopband. $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 16}$  [hb, $\color{mdarkgrey}$ha] = cheby2(6, 30, 0.55);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 17}$  hb = [zeros(1, 20) hb]; $\color{mgrey}$% Add a delay of 20 samples to H(z)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 18}$  $\color{mgrey}$% Hint: for filtering, use the function filter, e.g., output = filter(hb, ha, input)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 19}$  $\\
 \hspace*{-2em}{\scriptsize 20}$  $\color{mgrey}#%% Your solutions go here#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 21}$  $\color{mgrey}$% Matlab hints: $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 22}$  $\color{mgrey}$% - The function randn() generates zero-mean, unit-variance,$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 23}$  $\color{mgrey}$% Gaussian-distributed numbers$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$  $\color{mgrey}$% - Fixed filters can be easily implement using the function filter()$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$  $\color{mgrey}$% - Adaptive filters are can be implemented in a for loop$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 26}$  $\color{mgrey}$% - To plot the coefficients of an FIR filter, use the function impz$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 27}$  $\color{mgrey}$% (impulse response)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 28}$  $\color{mgrey}$% - To plot the magnitude and phase response, use the function freqz()$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$  $\color{mgrey}$% - To play a signal use the function sound(x, Fs). Don't forget to include$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 30}$  $\color{mgrey}$% the sampling frequency when calling sound, otherwise Matlab will assume $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$  $\color{mgrey}$% Fs = 8 kHz, which is not the correct value.$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$  sound(s, $\color{mdarkgrey}$Fs) $\color{black}$$\color{mgrey}$% example$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 33}$  $\\
 \hspace*{-2em}{\scriptsize 34}$  $\\
 \hspace*{-2em}{\scriptsize 35}$  $\color{mgrey}#%% Your solutions go here#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 36}$  $\color{mgrey}$% (C) adaptation constant$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 37}$  lamb = var_r; $\color{mgrey}$% all eigenvalues are equal to var_r since R = var_r*I$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 38}$  mu = 0.001/((L+1)*var_r)$\\
 \hspace*{-2em}{\scriptsize 39}$  $\\
 \hspace*{-2em}{\scriptsize 40}$  $\color{mgrey}$% (D) convergence time$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 41}$  tau = 1/(4*mu*lamb); $\color{mgrey}$% learning curve time constants$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 42}$  T_conv = 4*tau/Fs $\color{mgrey}$% convergence time$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 43}$  $\\
 \hspace*{-2em}{\scriptsize 44}$  $\color{mgrey}$% (F) Implementation$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 45}$  $\color{mgrey}$% Generate input white noise$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 46}$  r = sqrt(var_r)*randn(size(s));$\\
 \hspace*{-2em}{\scriptsize 47}$  rf = filter(hb, ha, r);$\\
 \hspace*{-2em}{\scriptsize 48}$  $\\
 \hspace*{-2em}{\scriptsize 49}$  $\color{mgrey}$% Generate signal x$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 50}$  x = s + rf; $\color{mgrey}$% from diagram in Fig. 2$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 51}$  $\\
 \hspace*{-2em}{\scriptsize 52}$  avg_noise = zeros(size(s));$\\
 \hspace*{-2em}{\scriptsize 53}$  $#for#$ q = 1:Nruns$\\
 \hspace*{-2em}{\scriptsize 54}$      y = zeros(size(s)); $\color{mgrey}$% output of adaptive filter$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 55}$      e = zeros(size(s)); $\color{mgrey}$% error signal$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 56}$      W = zeros(1, L+1); $\color{mgrey}$% initial weight vector$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 57}$      $#for#$ k = L+1:length(s)$\\
 \hspace*{-2em}{\scriptsize 58}$          y(k) = W*r(k:-1:k-L);$\\
 \hspace*{-2em}{\scriptsize 59}$          e(k) = x(k) - y(k);$\\
 \hspace*{-2em}{\scriptsize 60}$          W = W + 2*mu*e(k)*r(k:-1:k-L).';$\\
 \hspace*{-2em}{\scriptsize 61}$      $#end#$$\\
 \hspace*{-2em}{\scriptsize 62}$      $\\
 \hspace*{-2em}{\scriptsize 63}$      avg_noise = avg_noise + (s-e).^2;$\\
 \hspace*{-2em}{\scriptsize 64}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 65}$  $\\
 \hspace*{-2em}{\scriptsize 66}$  avg_noise = avg_noise/Nruns;$\\
 \hspace*{-2em}{\scriptsize 67}$  $\\
 \hspace*{-2em}{\scriptsize 68}$  figure, $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 69}$  plot((0:length(s)-1)/Fs, $\color{mdarkgrey}$avg_noise)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 70}$  xlabel($\color{mdarkgrey}$'Time (seconds)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 71}$  ylabel($\color{mdarkgrey}$'Average noise'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 72}$  saveas(gca, $\color{mdarkgrey}$'../figs/part1_average_noise'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 73}$  $\\
 \hspace*{-2em}{\scriptsize 74}$  figure, $\color{mdarkgrey}$impz(W)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 75}$  saveas(gca, $\color{mdarkgrey}$'../figs/part1_coeff'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 76}$  $\\
 \hspace*{-2em}{\scriptsize 77}$  [H1, $\color{mdarkgrey}$w] = freqz(hb, ha);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 78}$  H2 = freqz(W, 1, w);$\\
 \hspace*{-2em}{\scriptsize 79}$  $\\
 \hspace*{-2em}{\scriptsize 80}$  figure$\\
 \hspace*{-2em}{\scriptsize 81}$  subplot(211), $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 82}$  plot(w/pi, $\color{mdarkgrey}$20*log10(abs(H1)))$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 83}$  plot(w/pi, $\color{mdarkgrey}$20*log10(abs(H2)))$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 84}$  xlabel($\color{mdarkgrey}$'Normalized frequency (\times\pi rad/samples)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 85}$  ylabel($\color{mdarkgrey}$'Magnitude response (dB)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 86}$  legend($\color{mdarkgrey}$'H(z)'$\color{black}$, $\color{mdarkgrey}$'Adaptive filter'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 87}$  $\\
 \hspace*{-2em}{\scriptsize 88}$  subplot(212), $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 89}$  plot(w/pi, $\color{mdarkgrey}$unwrap(angle(H1)))$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 90}$  plot(w/pi, $\color{mdarkgrey}$unwrap(angle(H2)))$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 91}$  xlabel($\color{mdarkgrey}$'Normalized frequency (\times\pi rad/samples)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 92}$  ylabel($\color{mdarkgrey}$'Phase response (rad)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 93}$  legend($\color{mdarkgrey}$'H(z)'$\color{black}$, $\color{mdarkgrey}$'Adaptive filter'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 94}$  saveas(gca, $\color{mdarkgrey}$'../figs/part1_freqz'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}