% 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}$  [y, Fs] = audioread($\color{mdarkgrey}$'saw_noise_front_mic.wav'$\color{black}$); $\color{mgrey}$% Fs is sampling frequency$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  [x, Fs] = audioread($\color{mdarkgrey}$'saw_noise_rear_mic.wav'$\color{black}$);   $\\
 \hspace*{-1.6em}{\scriptsize 6}$  $\\
 \hspace*{-1.6em}{\scriptsize 7}$  N = floor(length(y)/2);$\\
 \hspace*{-1.6em}{\scriptsize 8}$  $\\
 \hspace*{-1.6em}{\scriptsize 9}$  y_train = y(1:N);$\\
 \hspace*{-2em}{\scriptsize 10}$  y_val = y(N+1:end);$\\
 \hspace*{-2em}{\scriptsize 11}$  x_train = x(1:N);$\\
 \hspace*{-2em}{\scriptsize 12}$  x_val = x(N+1:end);$\\
 \hspace*{-2em}{\scriptsize 13}$  $\\
 \hspace*{-2em}{\scriptsize 14}$  $\color{mgrey}#%% c) Estimate x[n] PSD#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  M = 512;$\\
 \hspace*{-2em}{\scriptsize 16}$  L = 2*M-1;$\\
 \hspace*{-2em}{\scriptsize 17}$  window = bartlett(L);$\\
 \hspace*{-2em}{\scriptsize 18}$  dw = 2*pi/L;        $\color{mgrey}$% Nfft = L;$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 19}$  w = -pi:dw:pi-dw;$\\
 \hspace*{-2em}{\scriptsize 20}$  $\\
 \hspace*{-2em}{\scriptsize 21}$  Pxx = blackman_tukey_psd(x_train, window, M);$\\
 \hspace*{-2em}{\scriptsize 22}$  $\\
 \hspace*{-2em}{\scriptsize 23}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$  plot(w/pi, 10*log10(Pxx), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 26}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 27}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 28}$  saveas(gca, $\color{mdarkgrey}$'../figs/noise_cancel_x_psd'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 29}$  $\\
 \hspace*{-2em}{\scriptsize 30}$  $\color{mgrey}#%% d) Estimate cross-correlation #\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$  $\color{mgrey}$% unbiased autocorrelation estimator$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$  phi_yx_hat = xcorr(y_train, x_train, M-1, $\color{mdarkgrey}$'unbiased'$\color{black}$);  $\\
 \hspace*{-2em}{\scriptsize 33}$  s = phi_yx_hat.*window; $\color{mgrey}$% windowing$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 34}$  Phi_yx_hat = fftshift(fft(ifftshift(s))); $\\
 \hspace*{-2em}{\scriptsize 35}$  $\color{mgrey}$% Note: ifftshift ensures that Phi_yx_hat is not phase shifted due to FFT $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 36}$  $\color{mgrey}$% indexing. Using abs() or real() would result in incorrect estimates$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 37}$  $\\
 \hspace*{-2em}{\scriptsize 38}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 39}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 40}$  plot(w/pi, 10*log10(abs(Phi_yx_hat)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 41}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 42}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 43}$  saveas(gca, $\color{mdarkgrey}$'../figs/noise_cancel_cross_psd'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 44}$  $\\
 \hspace*{-2em}{\scriptsize 45}$  $\color{mgrey}#%% e)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 46}$  Hest = Phi_yx_hat./Pxx;$\\
 \hspace*{-2em}{\scriptsize 47}$  $\\
 \hspace*{-2em}{\scriptsize 48}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 49}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 50}$  plot(w/pi, 20*log10(abs(Hest)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 51}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 52}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 53}$  saveas(gca, $\color{mdarkgrey}$'../figs/noise_cancel_Hest'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 54}$  $\\
 \hspace*{-2em}{\scriptsize 55}$  $\color{mgrey}#%% f) Filter design#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 56}$  M = 49;$\\
 \hspace*{-2em}{\scriptsize 57}$  $\\
 \hspace*{-2em}{\scriptsize 58}$  $\color{mgrey}$% Calculate matrix Q$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 59}$  $\color{mgrey}$% This assumes even symmetry$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 60}$  Q = zeros(length(w), M+1); $\color{mgrey}$% initialized$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 61}$  n = 0:M; $\color{mgrey}$% time vector$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 62}$  $#for#$ i = 1:length(w) $\color{mgrey}$% for every frequency wi$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 63}$      Q(i, $\color{mdarkgrey}$:) = exp(-1j*w(i)*n);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 64}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 65}$  $\\
 \hspace*{-2em}{\scriptsize 66}$  $\color{mgrey}$% Least-squares algorithm$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 67}$  d = Hest;$\\
 \hspace*{-2em}{\scriptsize 68}$  hls = real(pinv(Q)*d); $\color{mgrey}$% real() removes imaginary part due to numerical errors$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 69}$  Hls = freqz(hls, 1, w);$\\
 \hspace*{-2em}{\scriptsize 70}$  $\\
 \hspace*{-2em}{\scriptsize 71}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 72}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 73}$  plot(w/pi, 20*log10(abs(Hest)), $\color{mdarkgrey}$'r'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 74}$  plot(w/pi, 20*log10(abs(Hls)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 75}$  legend($\color{mdarkgrey}$'Desired response'$\color{black}$, $\color{mdarkgrey}$'FIR filter of order 50'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 76}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 77}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 78}$  saveas(gca, $\color{mdarkgrey}$'../figs/noise_cancel_Hfir'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 79}$  $\\
 \hspace*{-2em}{\scriptsize 80}$  $\color{mgrey}#%% g)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 81}$  y_clean = y_val - filter(hls, 1, x_val);$\\
 \hspace*{-2em}{\scriptsize 82}$  sound(y_clean, $\color{mdarkgrey}$Fs)$\color{black}$$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}