% 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}#%% Comparison of famous filters#\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}#%% a) Design filters#\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  N = 8;                      $\color{mgrey}$% filter order$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 6}$  wc = pi/2;                  $\color{mgrey}$% cutoff frequency wc = 2pi*4/16 = pi/2$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 7}$  Rp = 1;                     $\color{mgrey}$% pass-band ripple$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 8}$  ws = 1.1*wc;                $\color{mgrey}$% stopband edge frequency to achieve wc = 4 kHz$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  Rs = 30;                    $\color{mgrey}$% stopband attenuation$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 10}$  T = 1/16e3;                 $\color{mgrey}$% sampling period$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 11}$  $\\
 \hspace*{-2em}{\scriptsize 12}$  $\color{mgrey}$% Design filters$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  [num_butter, $\color{mdarkgrey}$den_butter] = butter(N, wc/pi);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 14}$  [num_cheby1, $\color{mdarkgrey}$den_cheby1] = cheby1(N, Rp, wc/pi);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  [num_cheby2, $\color{mdarkgrey}$den_cheby2] = cheby2(N, Rs, ws/pi);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 16}$  [num_ellip, $\color{mdarkgrey}$den_ellip] = ellip(N, Rp, Rs, wc/pi);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 17}$  $\\
 \hspace*{-2em}{\scriptsize 18}$  $\color{mgrey}$% Frequency response$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 19}$  [H_butter, $\color{mdarkgrey}$w] = freqz(num_butter, den_butter);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 20}$  H_cheby1 = freqz(num_cheby1, den_cheby1, w);$\\
 \hspace*{-2em}{\scriptsize 21}$  H_cheby2 = freqz(num_cheby2, den_cheby2, w);$\\
 \hspace*{-2em}{\scriptsize 22}$  H_ellip = freqz(num_ellip, den_ellip, w);$\\
 \hspace*{-2em}{\scriptsize 23}$  $\\
 \hspace*{-2em}{\scriptsize 24}$  $\color{mgrey}$% Pole-zero$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$  figure, $\color{mdarkgrey}$zplane(num_butter, den_butter)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 26}$  figure, $\color{mdarkgrey}$zplane(num_cheby1, den_cheby1)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 27}$  figure, $\color{mdarkgrey}$zplane(num_cheby2, den_cheby2)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 28}$  figure, $\color{mdarkgrey}$zplane(num_ellip, den_ellip)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$  $\\
 \hspace*{-2em}{\scriptsize 30}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$20*log10(abs(H_butter)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 33}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Butterworth'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 34}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$20*log10(abs(H_cheby1)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 35}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Chebyshev I'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 36}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$20*log10(abs(H_cheby2)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 37}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Chebyshev II'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 38}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$20*log10(abs(H_ellip)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 39}$  $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Elliptic'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 40}$  plot([0 1e-3/T], [-3 -3], $\color{mdarkgrey}$'--k'$\color{black}$, $\color{mdarkgrey}$'DisplayName'$\color{black}$, $\color{mdarkgrey}$'-3 dB'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 41}$  xlabel($\color{mdarkgrey}$'Frequency (kHz)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 42}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 43}$  axis([0 $\color{mdarkgrey}$0.5e-3/T -40 0])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 44}$  legend($\color{mdarkgrey}$'-dynamiclegend'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 45}$  saveas(gca, $\color{mdarkgrey}$'../figs/classic_filters_mag'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 46}$  $\\
 \hspace*{-2em}{\scriptsize 47}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 48}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$unwrap(angle(H_butter)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 49}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Butterworth'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 50}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$unwrap(angle(H_cheby1)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 51}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Chebyshev I'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 52}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$unwrap(angle(H_cheby2)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 53}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Chebyshev II'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 54}$  plot(w/(2*pi*T)*1e-3, $\color{mdarkgrey}$unwrap(angle(H_ellip)),...$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 55}$      $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayName'$\color{black}$, $\color{mdarkgrey}$'Elliptic'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 56}$  legend($\color{mdarkgrey}$'-dynamiclegend'$\color{black}$, $\color{mdarkgrey}$'Location'$\color{black}$, $\color{mdarkgrey}$'SouthWest'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 57}$  xlabel($\color{mdarkgrey}$'Frequency (kHz)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 58}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 59}$  axis([0 $\color{mdarkgrey}$0.5e-3/T -12 0])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 60}$  saveas(gca, $\color{mdarkgrey}$'../figs/classic_filters_phase'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 61}$  $\\
 \hspace*{-2em}{\scriptsize 62}$  $\color{mgrey}#%% b) Filter speech#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 63}$  $\color{mgrey}$% Load speech signal$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 64}$  [x_orig, Fs] = audioread($\color{mdarkgrey}$'speech_dft.wav'$\color{black}$);          $\color{mgrey}$% Fs is sampling frequency$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 65}$  T = 1/Fs;                                            $\color{mgrey}$% sampling period$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 66}$  $\\
 \hspace*{-2em}{\scriptsize 67}$  $\color{mgrey}$% Express 16/22.05 as fraction L/M$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 68}$  [L, $\color{mdarkgrey}$M] = rat(16/22.05); $\color{black}$$\color{mgrey}$% L is the umpsampling factor and M is the downsampling factor$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 69}$         $\\
 \hspace*{-2em}{\scriptsize 70}$  $\color{mgrey}#%% Upsampling followed by decimation.#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 71}$  $\color{mgrey}$% Note that the lowpass filter between the upsampling and downsampling $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 72}$  $\color{mgrey}$% stages must have gain L. However, the lowpass filter prior to $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 73}$  $\color{mgrey}$% downsampling in decimation (and in the decimate function) has gain 1.$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 74}$  $\color{mgrey}$% Therefore, we must scale the result by L$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 75}$  $\color{mgrey}$% For more details, refer to slide 30 of lecture notes 4.$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 76}$  x = L*decimate(upsample(x_orig, L), M);$\\
 \hspace*{-2em}{\scriptsize 77}$  Fs = 16e3;  $\color{mgrey}$% Sampling rate after upsampling/decimation$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 78}$  $\\
 \hspace*{-2em}{\scriptsize 79}$  y_butter = filter(num_butter, den_butter, x);$\\
 \hspace*{-2em}{\scriptsize 80}$  y_cheby1 = filter(num_cheby1, den_cheby1, x);$\\
 \hspace*{-2em}{\scriptsize 81}$  y_cheby2 = filter(num_cheby2, den_cheby2, x);$\\
 \hspace*{-2em}{\scriptsize 82}$  y_ellip = filter(num_ellip, den_ellip, x);$\\
 \hspace*{-2em}{\scriptsize 83}$  $\\
 \hspace*{-2em}{\scriptsize 84}$  sound(y_ellip, $\color{mdarkgrey}$Fs) $\color{black}$$\color{mgrey}$% play sound after filtering$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 85}$  $\\
 \hspace*{-2em}{\scriptsize 86}$  $\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}