% 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}#%% Template for Homework 3: Problem 3 on quantization and quantization noise shaping#\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 2}$  clear, $\color{mdarkgrey}$clc, close all $\color{black}$$\color{mgrey}$% clear variables$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 3}$  $\\
 \hspace*{-1.6em}{\scriptsize 4}$  $\color{mgrey}$% Load speech signal$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  [x, Fs] = audioread($\color{mdarkgrey}$'speech_dft.wav'$\color{black}$);          $\color{mgrey}$% Fs is sampling frequency$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 6}$  T = 1/Fs;                                       $\color{mgrey}$% sampling period$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 7}$  $\\
 \hspace*{-1.6em}{\scriptsize 8}$  $\color{mgrey}#%% Important: #\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  $\color{mgrey}$% If your simulations take too long because you're using a slow computer,$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 10}$  $\color{mgrey}$% you can uncomment the following line in order to use just part of the $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 11}$  $\color{mgrey}$% speech signal. Specifically, you may use just the first 43000 samples, $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 12}$  $\color{mgrey}$% which correspond to the phrase "The discrete Fourier transform". $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  $\color{mgrey}$% In my computer, the simulations with the whole signal took < 1 s.$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 14}$  $\color{mgrey}$% x = x(1:43e3); % play just part of the speech$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  $\\
 \hspace*{-2em}{\scriptsize 16}$  $\color{mgrey}#%% Your code goes here#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 17}$  $\color{mgrey}$%sound(x, Fs) % Play speech$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 18}$  $\\
 \hspace*{-2em}{\scriptsize 19}$  $\color{mgrey}$% Define parameters$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 20}$  range_lims = [-1 1];           $\color{mgrey}$% range limits                  $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 21}$  B = 4; $\color{mgrey}$% quantizer resolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 22}$  $\\
 \hspace*{-2em}{\scriptsize 23}$  $\color{mgrey}#%% Part (b)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$  maxLag = 10;$\\
 \hspace*{-2em}{\scriptsize 25}$  [~, $\color{mdarkgrey}$e4bits] = quantizer(x, 4, range_lims); $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 26}$  [~, $\color{mdarkgrey}$e10bits] = quantizer(x, 10, range_lims); $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 27}$  $\\
 \hspace*{-2em}{\scriptsize 28}$  c4bits = xcorr(e4bits, e4bits, maxLag, $\color{mdarkgrey}$'unbiased'$\color{black}$);$\\
 \hspace*{-2em}{\scriptsize 29}$  c10bits = xcorr(e10bits, e10bits, maxLag, $\color{mdarkgrey}$'unbiased'$\color{black}$);$\\
 \hspace*{-2em}{\scriptsize 30}$  $\\
 \hspace*{-2em}{\scriptsize 31}$  e4bits_power = c4bits(maxLag+1); $\color{mgrey}$% autocorrelation at m = 0;$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 32}$  e10bits_power = c10bits(maxLag+1); $\color{mgrey}$% autocorrelation at m = 0;$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 33}$  $\\
 \hspace*{-2em}{\scriptsize 34}$  $\color{mgrey}$% Plot autocorrelation function$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 35}$  figure, $\color{mdarkgrey}$subplot(211), box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 36}$  stem(-maxLag:maxLag, $\color{mdarkgrey}$c4bits)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 37}$  legend($\color{mdarkgrey}$'B = 4 bits'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 38}$  xlabel($\color{mdarkgrey}$'m'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 39}$  ylabel($\color{mdarkgrey}$'Empir. autocorrelation'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 40}$  set(gca, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 41}$  subplot(212), $\color{mdarkgrey}$box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 42}$  stem(-maxLag:maxLag, $\color{mdarkgrey}$c10bits)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 43}$  legend($\color{mdarkgrey}$'B = 10 bits'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 44}$  xlabel($\color{mdarkgrey}$'m'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 45}$  ylabel($\color{mdarkgrey}$'Empir. autocorrelation'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 46}$  set(gca, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 47}$  saveas(gca, $\color{mdarkgrey}$'../figs/hw03_q4_empir_xcorr'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 48}$  $\\
 \hspace*{-2em}{\scriptsize 49}$  $\color{mgrey}#%% Part (c)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 50}$  B = 4; $\color{mgrey}$% quantizer resolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 51}$  b = [1 -1]; $\color{mgrey}$% shaping filter coefficients$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 52}$  $\\
 \hspace*{-2em}{\scriptsize 53}$  [xq, $\color{mdarkgrey}$e] = quantizer(x, B, range_lims); $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 54}$  $\\
 \hspace*{-2em}{\scriptsize 55}$  eshaped = filter(b, 1, e);$\\
 \hspace*{-2em}{\scriptsize 56}$  $\\
 \hspace*{-2em}{\scriptsize 57}$  xqshaped = x + eshaped;$\\
 \hspace*{-2em}{\scriptsize 58}$  $\color{mgrey}$%sound(xqshaped, Fs)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 59}$  $\\
 \hspace*{-2em}{\scriptsize 60}$  $\color{mgrey}#%% Part (d)#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 61}$  B = 4; $\color{mgrey}$% quantizer resolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 62}$  b = [1 -1]; $\color{mgrey}$% shaping filter coefficients$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 63}$  M = 3; $\color{mgrey}$% upsampling factor $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 64}$  [xq, $\color{mdarkgrey}$e] = quantizer(x, B, range_lims); $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 65}$  $\\
 \hspace*{-2em}{\scriptsize 66}$  eup = upsample(e, M);$\\
 \hspace*{-2em}{\scriptsize 67}$  eshaped = filter(b, 1, eup);$\\
 \hspace*{-2em}{\scriptsize 68}$  ef = M*decimate(eshaped, M); $\color{mgrey}$% Note multiplication by M in order to account $\color{black}$$\\
 \hspace*{-2em}{\scriptsize 69}$  $\color{mgrey}$% for the fact that in Fig. 6a the signal is also decimated$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 70}$  $\\
 \hspace*{-2em}{\scriptsize 71}$  xqshaped = x + ef;$\\
 \hspace*{-2em}{\scriptsize 72}$  sound(xqshaped, $\color{mdarkgrey}$Fs)$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 73}$  $\\
 \hspace*{-2em}{\scriptsize 74}$  ef_power = mean(abs(ef).^2); $\color{mgrey}$% can use var only if ef is zero mean$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 75}$  $\\
 \hspace*{-2em}{\scriptsize 76}$  SNRimprovement = 10*log10(e4bits_power/ef_power)$\\
 \hspace*{-2em}{\scriptsize 77}$  $\\
 \hspace*{-2em}{\scriptsize 78}$  Bimprovement = SNRimprovement/6.02$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}