%Matlab file used for class demo 
%illustrating efficacy of M-Channel PR Filter Bank
%based on 4-channel hadamard filters.  The 4 channel PR Bank is
%applied to a word utterance sampled at 12.5 KHz.
clf
clear all
set(0,'defaultaxesfontsize',20);
%Set up M-channel DFT filter bank:
M=4;
%
H4=dftmtx(4)';
%
H(1,:)=H4(1,:);  G(1,:)=conj(H(1,end:-1:1));
H(2,:)=H4(2,:);  G(2,:)=conj(H(2,end:-1:1));
H(3,:)=H4(3,:);  G(3,:)=conj(H(3,end:-1:1));
H(4,:)=H4(4,:);  G(4,:)=conj(H(4,end:-1:1));
%
rss=zeros(1,7); r11=rss; r22=rss; r33=rss; 
for in=1:4,
   rss=rss+conv(H(in,:),G(in,:));
   r11=r11+conv(exp(j*(2*pi/4)*[0:3]).*H(in,:),G(in,:));
   r22=r22+conv(exp(j*(4*pi/4)*[0:3]).*H(in,:),G(in,:));
   r33=r33+conv(exp(j*(6*pi/4)*[0:3]).*H(in,:),G(in,:));
end
rss
r11
r22
r33
data=getspeech('erf1s1t0.wav');
Fs=12500;
%plot(data)
%end1=input('end1?');
%end2=input('end2?');
end1=500;
end2=12500;
x=data(end1:end2);
%dl=end2-end1+1;
input('Utterance played back at 12.5 KHz sampling rate'); 
soundsc(x,Fs)
%create x_0 [n] through x_M-1 [n]
for m=1:M;
W(m,:)=conv(x,H(m,:));
X(m,:)=W(m,1:M:length(W(m,:)));
end
%create y_0[n] and y_M-1 [n]
for m=1:M;
Z(m,:)=zeros(1,M*length(X(m,:)));
Z(m,1:M:length(Z(m,:)))=X(m,:);
Y(m,:)=conv(Z(m,:),G(m,:));
end
y=zeros(1,length(Y(1,:)));
for m=1:M;
y=y+Y(m,:);
end
y=real(y);
input('QMF Output played back at Fs=12.5 KHz'); 
soundsc(y,Fs)
%plot and compare DTFT's of x[n] and y[n]
domega=2*pi/8192;
omega=-pi:domega:pi-domega;
yf1=abs(fftshift(fft(x,8192)));
yf2=abs(fftshift(fft(y,8192)));
subplot(211)
plot(omega,yf1,'Linewidth',3)
axis([-pi pi 0 max(yf1)])
title('DTFT of original utterance');
xlabel('omega (radians/s)');
subplot(212)
plot(omega,yf2,'Linewidth',3)
axis([-pi pi 0 max(yf2)])
title('DTFT of output of QMF');
xlabel('omega (radians/s)');
%plot filter responses h[n]
h0f=abs(fftshift(fft(H(1,:),512)));
h1f=abs(fftshift(fft(H(2,:),512)));
h2f=abs(fftshift(fft(H(3,:),512)));
h3f=abs(fftshift(fft(H(4,:),512)));
input('Plot DTFT of h0[n] thru h4[n]')
clf
domega=2*pi/512;
omega=-pi:domega:pi-domega;
plot(omega,h0f,'b','Linewidth',4)
axis([-pi pi 0 max(h0f)])
hold on
plot(omega,h1f,'r','Linewidth',4)
plot(omega,h2f,'g','Linewidth',4)
plot(omega,h3f,'c','Linewidth',4)
%plot(omega,h4f,'m','Linewidth',4)
legend('H0(w)','H1(w)','H2(w)','H3(w)');
title('Frequency Response of h0[n] thru h4[n]');
xlabel('omega (radians/s)');
hold off
