% 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}#%% Overlap and save or overlap and add#\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}$  load($\color{mdarkgrey}$'notch_filter_coeff'$\color{black}$); $\color{mgrey}$% loads filter coefficients$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 5}$  load($\color{mdarkgrey}$'ecg_recording.mat'$\color{black}$)  $\color{mgrey}$% loads ECG data$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 6}$  $\\
 \hspace*{-1.6em}{\scriptsize 7}$  $\color{mgrey}$% Definitions$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 8}$  Nfft = 256;    $\color{mgrey}$% FFT length$\color{black}$$\\
 \hspace*{-1.6em}{\scriptsize 9}$  M = length(hls) - 1; $\color{mgrey}$% M = 52$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 10}$  Loa = Nfft - M; $\color{mgrey}$% block length for overlap-add$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 11}$  Los = Nfft;     $\color{mgrey}$% block length for overlap-save$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 12}$  H = fft(hls, Nfft); $\color{mgrey}$% FFT of filter coefficients$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 13}$  $\\
 \hspace*{-2em}{\scriptsize 14}$  t = 0:T:(N-1)*T; $\color{mgrey}$% time vector$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 15}$  ecg_fir = filter(hls, 1, ecg_60Hz);$\\
 \hspace*{-2em}{\scriptsize 16}$  ecg_fir = circshift(ecg_fir, [0, -M/2]); $\color{mgrey}$% remove group delay$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 17}$  $\\
 \hspace*{-2em}{\scriptsize 18}$  $\color{mgrey}#%% Overlap-add#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 19}$  yoa = zeros(size(ecg_60Hz));$\\
 \hspace*{-2em}{\scriptsize 20}$  select = (1:Loa);$\\
 \hspace*{-2em}{\scriptsize 21}$  $#for#$ r = 0:floor(length(ecg_60Hz)/Loa)-1$\\
 \hspace*{-2em}{\scriptsize 22}$      xr = ecg_60Hz(select);           $\color{mgrey}$% non-overlapping segments of length Loa$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 23}$      yr = ifft(H.*fft(xr, Nfft));    $\color{mgrey}$% compute Nfft-point circular convolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 24}$      yoa(select(1):(select(1)+Nfft-1)) = yoa(select(1):(select(1)+Nfft-1)) + yr; $\color{mgrey}$% add$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 25}$      select = select + Loa;$\\
 \hspace*{-2em}{\scriptsize 26}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 27}$  $\\
 \hspace*{-2em}{\scriptsize 28}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 29}$  n = 0:length(ecg_60Hz)-1;$\\
 \hspace*{-2em}{\scriptsize 30}$  yoa = circshift(yoa, [0, -M/2]); $\color{mgrey}$% remove group delay$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 31}$  plot(t, ecg_clean, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'DisplayName'$\color{black}$, $\color{mdarkgrey}$'Clean ECG'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 32}$  plot(t, yoa, $\color{mdarkgrey}$'--'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayname'$\color{black}$, $\color{mdarkgrey}$'Overlap-add'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 33}$  plot(t, ecg_fir, $\color{mdarkgrey}$':'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayname'$\color{black}$, $\color{mdarkgrey}$'Direct form implementation'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 34}$  legend($\color{mdarkgrey}$'-dynamiclegend'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 35}$  xlabel($\color{mdarkgrey}$'Time (s)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 36}$  ylabel($\color{mdarkgrey}$'ECG'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 37}$  saveas(gca, $\color{mdarkgrey}$'../figs/fir_notch_overlap_add'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 38}$  $\\
 \hspace*{-2em}{\scriptsize 39}$  $\color{mgrey}#%% Overlap-save#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 40}$  yos = zeros(size(ecg_60Hz));$\\
 \hspace*{-2em}{\scriptsize 41}$  select = (1:Los);$\\
 \hspace*{-2em}{\scriptsize 42}$  $#for#$ r = 0:floor(length(ecg_60Hz)/Los)$\\
 \hspace*{-2em}{\scriptsize 43}$      xr = ecg_60Hz(select);           $\color{mgrey}$% non-overlapping segments of length Loa$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 44}$      yr = ifft(H.*fft(xr, Nfft));    $\color{mgrey}$% compute Nfft-point circular convolution$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 45}$      yoa(select(M+1:end)) = yr(M+1:end); $\color{mgrey}$% save$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 46}$      select = select + Los - M;$\\
 \hspace*{-2em}{\scriptsize 47}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 48}$  $\\
 \hspace*{-2em}{\scriptsize 49}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 50}$  n = 0:length(ecg_60Hz)-1;$\\
 \hspace*{-2em}{\scriptsize 51}$  yoa = circshift(yoa, [0, -M/2]); $\color{mgrey}$% remove group delay$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 52}$  plot(t, ecg_clean, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'DisplayName'$\color{black}$, $\color{mdarkgrey}$'Clean ECG'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 53}$  plot(t, yoa, $\color{mdarkgrey}$'--'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayname'$\color{black}$, $\color{mdarkgrey}$'Overlap-save'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 54}$  plot(t, ecg_fir, $\color{mdarkgrey}$':'$\color{black}$, $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'displayname'$\color{black}$, $\color{mdarkgrey}$'Direct form implementation'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 55}$  legend($\color{mdarkgrey}$'-dynamiclegend'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 56}$  xlabel($\color{mdarkgrey}$'Time (s)'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 57}$  ylabel($\color{mdarkgrey}$'ECG'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 58}$  saveas(gca, $\color{mdarkgrey}$'../figs/fir_notch_overlap_save'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 59}$  $\\
 \hspace*{-2em}{\scriptsize 60}$  $\color{mgrey}#%% Complexity calculation#\color{black}$$\\
 \hspace*{-2em}{\scriptsize 61}$  M = 50:50:150;$\\
 \hspace*{-2em}{\scriptsize 62}$  $\\
 \hspace*{-2em}{\scriptsize 63}$  C = @(N, M) (2*2*N.*(log2(N)-1) + N)./(N-M);$\\
 \hspace*{-2em}{\scriptsize 64}$  $\\
 \hspace*{-2em}{\scriptsize 65}$  figure, $\color{mdarkgrey}$hold on, box on$\color{black}$$\\
 \hspace*{-2em}{\scriptsize 66}$  $#for#$ k = 1:length(M)$\\
 \hspace*{-2em}{\scriptsize 67}$      N = linspace(M(k)*2, 5e3);$\\
 \hspace*{-2em}{\scriptsize 68}$      plot(N, C(N, M(k)), $\color{mdarkgrey}$'LineWidth'$\color{black}$, 2, $\color{mdarkgrey}$'DisplayName'$\color{black}$, sprintf($\color{mdarkgrey}$'M = %d'$\color{black}$, M(k)))$\\
 \hspace*{-2em}{\scriptsize 69}$  $#end#$$\\
 \hspace*{-2em}{\scriptsize 70}$  xlabel($\color{mdarkgrey}$'FFT length'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 71}$  ylabel($\color{mdarkgrey}$'Complexity'$\color{black}$, $\color{mdarkgrey}$'FontSize'$\color{black}$, 12)$\\
 \hspace*{-2em}{\scriptsize 72}$  legend($\color{mdarkgrey}$'-dynamiclegend'$\color{black}$)$\\
 \hspace*{-2em}{\scriptsize 73}$  saveas(gca, $\color{mdarkgrey}$'../figs/block_conv_complexity'$\color{black}$, $\color{mdarkgrey}$'epsc'$\color{black}$)$\\ 
  
\UndefineShortVerb{\$} 
\UndefineShortVerb{\#}