% 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}#%% Hearing aid problem #\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}$  T = 1/22.05e3;     $\color{mgrey}$% sampling time in seconds$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  f = 1e3*[0.1 0.5 1 2 3 4 5 6 7 8 9 10]; $\color{mgrey}$% in Hz$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 6}$  A = [0 0 0 -10 -12 -17 -22 -29 -34 -39 -47 -50]; $\color{mgrey}$% in dB$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 7}$  $\\
 \hspace*{-1.6em}{\scriptsize 8}$  $\color{mgrey}#%% a)#\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  w_spec = 2*pi*f*T;$\\
 \hspace*{-2em}{\scriptsize 10}$  $\\
 \hspace*{-2em}{\scriptsize 11}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 12}$  figure, $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  plot(w_spec/pi, -A, $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 14}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 15}$  ylabel($\color{mdarkgrey}$'20log_{10}(|H_d(e^{j\omega}|)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 16}$  axis([0 $\color{mdarkgrey}$1 0 60])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 17}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_spec'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 18}$      $\\
 \hspace*{-2em}{\scriptsize 19}$  $\color{mgrey}#%% b)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 20}$  Hd = 1./(10.^(A/20).*sinc(w_spec/(2*pi)).^2);$\\
 \hspace*{-2em}{\scriptsize 21}$  figure, $\color{mdarkgrey}$plot(w_spec/pi, 20*log10(abs(Hd)))$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 22}$  $\\
 \hspace*{-2em}{\scriptsize 23}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$  figure, $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$  plot(w_spec/pi, 20*log10(abs(Hd)), $\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}$'20log_{10}(|H_d(e^{j\omega}|)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 28}$  axis([0 $\color{mdarkgrey}$1 0 60])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_corrected_spec'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 30}$  $\\
 \hspace*{-2em}{\scriptsize 31}$  $\color{mgrey}#%% c)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$  $#for#$ M = 2:100$\\
 \hspace*{-2em}{\scriptsize 33}$      hls = firls(M, w_spec/pi, Hd); $\color{mgrey}$% design filter of order M$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 34}$      error = 20*log10(abs(freqz(hls, 1, w_spec))./abs(Hd));$\\
 \hspace*{-2em}{\scriptsize 35}$      $#if#$ all(abs(error) < 1) $\color{mgrey}$% error is smaller than 1 dB at all frequencies$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 36}$          $#break#$$\\
 \hspace*{-2em}{\scriptsize 37}$      $#end#$$\\
 \hspace*{-2em}{\scriptsize 38}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 39}$  $\\
 \hspace*{-2em}{\scriptsize 40}$  M $\color{mgrey}$% filter order that meets specs$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 41}$  [Hls, $\color{mdarkgrey}$w] = freqz(hls, 1);$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 42}$  $\\
 \hspace*{-2em}{\scriptsize 43}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 44}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 45}$  stem(w_spec/pi, 20*log10(abs(freqz(hls, 1, w_spec))./abs(Hd)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 46}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 47}$  ylabel($\color{mdarkgrey}$'Error (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 48}$  axis([0 $\color{mdarkgrey}$1 -1 1])$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 49}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_filter_error'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 50}$  $\\
 \hspace*{-2em}{\scriptsize 51}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 52}$  plot(w_spec/pi, 20*log10(abs(Hd)), $\color{mdarkgrey}$'r'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 53}$  plot(w/pi, 20*log10(abs(Hls)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 54}$  legend($\color{mdarkgrey}$'Specification'$\color{black}$, sprintf($\color{mdarkgrey}$'Least-squares FIR M = %d'$\color{black}$, M),...$\\
 \hspace*{-2em}{\scriptsize 55}$      $\color{mdarkgrey}$'Location'$\color{black}$, $\color{mdarkgrey}$'SouthEast'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 56}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 57}$  ylabel($\color{mdarkgrey}$'Magnitude (dB)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 58}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_filter_mag'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 59}$  $\\
 \hspace*{-2em}{\scriptsize 60}$  figure, $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 61}$  plot(w/pi, unwrap(angle(Hls)), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 62}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 63}$  ylabel($\color{mdarkgrey}$'Phase (rad)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 64}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_filter_phase'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 65}$  $\\
 \hspace*{-2em}{\scriptsize 66}$  $\color{mgrey}#%% d)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 67}$  delay_samples = M/2 $\color{mgrey}$% delay of linear phase filter$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 68}$  delay_time = delay_samples*T$\\
 \hspace*{-2em}{\scriptsize 69}$  $\\
 \hspace*{-2em}{\scriptsize 70}$  $\color{mgrey}#%% e)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 71}$  $\color{mgrey}$% Quantization noise power$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 72}$  DeltaX = 2;$\\
 \hspace*{-2em}{\scriptsize 73}$  B = 10:20;$\\
 \hspace*{-2em}{\scriptsize 74}$  Delta = DeltaX./2.^B;$\\
 \hspace*{-2em}{\scriptsize 75}$  varQ = Delta.^2/12;$\\
 \hspace*{-2em}{\scriptsize 76}$  $\\
 \hspace*{-2em}{\scriptsize 77}$  varQuant = varQ*sum(abs(hls).^2);$\\
 \hspace*{-2em}{\scriptsize 78}$  $\\
 \hspace*{-2em}{\scriptsize 79}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 80}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 81}$  stem(B, 10*log10(varQuant/1e-3), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 82}$  plot([B(1) B(end)], [-30 -30], $\color{mdarkgrey}$'r'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 83}$  xlabel($\color{mdarkgrey}$'Resolution (bits)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 84}$  ylabel($\color{mdarkgrey}$'Output quantization noise average power (dBm)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 85}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_quant_noise_var'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 86}$  $\\
 \hspace*{-2em}{\scriptsize 87}$  $\color{mgrey}#%% f)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 88}$  $\color{mgrey}$% Roundoff noise$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 89}$  Nmult = floor(M/2+1);$\\
 \hspace*{-2em}{\scriptsize 90}$  $\\
 \hspace*{-2em}{\scriptsize 91}$  $\color{mgrey}$% Rounoff noise is not enhanced by filter$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 92}$  varRoundOff = Nmult*(1./(2.^(B-1))).^2/12;$\\
 \hspace*{-2em}{\scriptsize 93}$  $\\
 \hspace*{-2em}{\scriptsize 94}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 95}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 96}$  stem(B, 10*log10((varRoundOff + varQuant)/1e-3), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2em}{\scriptsize 97}$  plot([B(1) B(end)], [-30 -30], $\color{mdarkgrey}$'r'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 98}$  xlabel($\color{mdarkgrey}$'Resolution (bits)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 99}$  ylabel($\color{mdarkgrey}$'Output noise variance (dBm)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2.4em}{\scriptsize 100}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_total_noise_var'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2.4em}{\scriptsize 101}$  $\\
 \hspace*{-2.4em}{\scriptsize 102}$  $\color{mgrey}#%% g)#\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 103}$  B = 18;$\\
 \hspace*{-2.4em}{\scriptsize 104}$  Delta = DeltaX/2^B;$\\
 \hspace*{-2.4em}{\scriptsize 105}$  varQ = Delta^2/12;$\\
 \hspace*{-2.4em}{\scriptsize 106}$  varRoundOff = Nmult*(1/(2^(B-1)))^2/12;$\\
 \hspace*{-2.4em}{\scriptsize 107}$  $\\
 \hspace*{-2.4em}{\scriptsize 108}$  $\color{mgrey}$% $\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 109}$  q = sqrt(varQ)/sqrt(1/12)*(rand(1, 10e4)-0.5); $\color{mgrey}$% quantization noise$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 110}$  roundoff = sqrt(varRoundOff)/sqrt(1/12)*(rand(1, 10e4)-0.5); $\color{mgrey}$% roundoff noise$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 111}$  $\\
 \hspace*{-2.4em}{\scriptsize 112}$  $\color{mgrey}$% Quantization noise is shaped by the filter, but roundoff noise is not $\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 113}$  noise = filter(hls, 1, q) + roundoff;$\\
 \hspace*{-2.4em}{\scriptsize 114}$  $\\
 \hspace*{-2.4em}{\scriptsize 115}$  $\color{mgrey}$% PSD estimation$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 116}$  M = 1024;$\\
 \hspace*{-2.4em}{\scriptsize 117}$  L = 2*M-1;$\\
 \hspace*{-2.4em}{\scriptsize 118}$  window = bartlett(L).';$\\
 \hspace*{-2.4em}{\scriptsize 119}$  dw = 2*pi/L;$\\
 \hspace*{-2.4em}{\scriptsize 120}$  w = -pi:dw:pi-dw; $\color{mgrey}$% frequency vector$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 121}$  $\\
 \hspace*{-2.4em}{\scriptsize 122}$  Pw = blackman_tukey_psd(noise, window, M);$\\
 \hspace*{-2.4em}{\scriptsize 123}$  $\\
 \hspace*{-2.4em}{\scriptsize 124}$  $\color{mgrey}$% Average power estimation$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 125}$  estimatedNoisePower = trapz(w, Pw)/(2*pi);$\\
 \hspace*{-2.4em}{\scriptsize 126}$  estimatedNoisePowerdBm = 10*log10(estimatedNoisePower/1e-3)$\\
 \hspace*{-2.4em}{\scriptsize 127}$  $\\
 \hspace*{-2.4em}{\scriptsize 128}$  $\color{mgrey}$% Theoretical PSD$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 129}$  Hls = freqz(hls, 1, w);$\\
 \hspace*{-2.4em}{\scriptsize 130}$  Ptheory = varQ*abs(Hls).^2 + varRoundOff;$\\
 \hspace*{-2.4em}{\scriptsize 131}$  $\\
 \hspace*{-2.4em}{\scriptsize 132}$  theoreticalNoisePower = trapz(w, Pw)/(2*pi);$\\
 \hspace*{-2.4em}{\scriptsize 133}$  theoreticalNoisePowerdBm = 10*log10(theoreticalNoisePower/1e-3)$\\
 \hspace*{-2.4em}{\scriptsize 134}$  $\\
 \hspace*{-2.4em}{\scriptsize 135}$  $\color{mgrey}$% Plot results$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 136}$  figure, $\color{mdarkgrey}$box on, hold on$\color{black}$$\\
 \hspace*{-2.4em}{\scriptsize 137}$  plot(w/pi, 10*log10(Pw/1e-3), $\color{mdarkgrey}$'r'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2.4em}{\scriptsize 138}$  plot(w/pi, 10*log10(Ptheory/1e-3), $\color{mdarkgrey}$'k'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2)$\\
 \hspace*{-2.4em}{\scriptsize 139}$  legend($\color{mdarkgrey}$'Theory'$\color{black}$, $\color{mdarkgrey}$'Estimated PSD'$\color{black}$, $\color{mdarkgrey}$'Location'$\color{black}$, $\color{mdarkgrey}$'SouthEast'$\color{black}$)$\\
 \hspace*{-2.4em}{\scriptsize 140}$  xlabel($\color{mdarkgrey}$'\omega/\pi'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2.4em}{\scriptsize 141}$  ylabel($\color{mdarkgrey}$'Magnitude (dBm)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2.4em}{\scriptsize 142}$  saveas(gca, $\color{mdarkgrey}$'../figs/hearing_aid_psd'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}