%Matlab file used for class demo conducted during Session 12
%to illustrate digital subbanding -- transmultiplexers.
%An utterance from a male speaker and one from a
%female speaker are transmultiplexed into different subbands.
%--polyphase implementation
clf
set(0,'defaultaxesfontsize',20);
ML=24;
hlp=remez(ML,[0 .333 .665 1],[1 1 0 0]);
hl1=hlp(1:2:ML); hl2=hlp(2:2:ML);
N=512;
hlpfft=fftshift(fft(hlp,N));
subplot(211)
hlpmag=20*log10(abs(hlpfft));
hlpmag=hlpmag-max(hlpmag);
hlpang=angle(hlpfft);
freqomega=linspace(-pi,pi,N);
plot(freqomega,hlpmag,'LineWidth',3);
axis([-pi pi min(hlpmag) 0]);
title('LowpassFilter Response'),...
xlabel('Omega'), ylabel('Magnitude'), grid
subplot(212)
plot(freqomega,hlpang,'LineWidth',3);
axis([-pi pi -pi pi]);
title('LowpassFilter Response'),...
xlabel('Omega'), ylabel('Phase'), grid
pause
hhp=remez(ML,[0 .333 .665 1],[0 0 1 1]);
hp1=hhp(1:2:ML); hp2=hhp(2:2:ML);
hhpfft=fftshift(fft(hhp,N));
subplot(211)
hhpmag=20*log10(abs(hhpfft));
hhpmag=hhpmag-max(hhpmag);
hhpang=angle(hhpfft);
freqomega=linspace(-pi,pi,N);
plot(freqomega,hhpmag,'LineWidth',3);
axis([-pi pi min(hhpmag) 0]);
title('Highpass Filter Response'),...
xlabel('Omega'), ylabel('Magnitude'), grid
subplot(212)
plot(freqomega,hhpang,'LineWidth',3);
axis([-pi pi -pi pi]);
title('Highpass Filter Response'),...
xlabel('Omega'), ylabel('Phase'), grid
pause
data = getspeech('enf1s1t0.wav');
Fs=12500;
clf
plot(data)
end1=input('end1?')
end2=input('end2?')
del=end2-end1;
datar=data(end1:end2);
y1=conv(hl1,datar); y2=conv(hl2,datar);
[d,y1size]=size(y1); ly=2*y1size;
y=zeros(1,ly);
y(1:2:ly)=y1; y(2:2:ly)=y2;
input('First word interpolated by');
input('a factor of 2 played back at 2*Fs'); 
soundsc(y,2*Fs)
data = getspeech('hpf1s1t0.wav');
plot(data)
end1=input('end1?')
end2=end1+del;
datar=data(end1:end2);
plot(datar)
%Here's where the second signal is
%upsampled by a factor of 2 and
%translated to the band pi/2 < |omega| < pi
y1=conv(hp1,datar); y2=conv(hp2,datar);
[d,y1size]=size(y1); ly=2*y1size;
z=zeros(1,ly);
z(1:2:ly)=y1; z(2:2:ly)=y2;
%Here's where the two signals are summed
w=y+z;
domega=2*pi/8192;
omega=-pi:domega:pi-domega;
sum2=abs(fftshift(fft(w,8192)));
clf
plot(omega,sum2,'Linewidth',3)
axis([-pi pi 0 max(sum2)])
xlabel('omega')
title('Two speech signals in different subbands')
pause
%soundsc(w,2*Fs)
%ylp=conv(w(1:2:ly),hl2)+conv(w(2:2:ly),hl1);
ylp=conv([w(1:2:ly) 0],hl1)+conv([0 w(2:2:ly)],hl2);
input('First word after polyphase');
input('implementation of sequential operations');
input('of LPF and decimation by a factor of 2,');
input('played back at Fs');
soundsc(ylp,Fs)
%zlp=conv(w(1:2:ly),hp2)+conv(w(2:2:ly),hp1);
zlp=conv([w(1:2:ly) 0],hp1)+conv([0 w(2:2:ly)],hp2);
input('Second word after transmultiplexer');
input('implementation of sequential');
input('operations of HPF & decimation');
input('by a factor of 2, played back at Fs');
soundsc(zlp,Fs)

