%Matlab file used for class demo conducted during Session 9
%illustrating upsampling by a factor of 2 using the
%efficient polyphase implementation of upsampling.
%A word utterance is sampled at 12.5 KHz, upsampled by
%a factor of 2 and played back at 25 KHz
clf
set(0,'defaultaxesfontsize',20);
% h=remez(50,[0 .333 .5 1],[1 1 0 0]);
% hd1=h(1:2:50); hd2=h(2:2:50);
n=-50:49;
h=sin((pi/2)*n)./(pi*n); h(51)=0.5;
h2=sin((pi/16)*n)./(pi*n); h2(51)=1/16;
h=32*h.*h2;
hd1=h(1:2:end); hd2=h(2:2:end);
data=getspeech('erf1s1t0.wav');
Fs=12500;
plot(data)
% end1=input('end1?');
% end2=input('end2?');
end1=1000;  end2=13500; ll=end2-end1+1;
datar=data(end1:end2);
%plot(datar)
input('Utterance played back at orignal'); 
input('sampling rate, Fs = 12.5 KHz'); 
soundsc(datar,Fs)
y1=conv(hd1,datar); y2=conv(hd2,datar);
y1=zeros(size(y2)); y1(1,26:26+ll-1)=datar;
[d,y1size]=size(y1); ly=2*y1size;
y=zeros(1,ly);
y(1:2:ly)=y1; y(2:2:ly)=y2;
input('Utterance interpolated by a factor');
input('played at twice sampling rate, 2*Fs = 25 KHz'); 
soundsc(y,2*Fs)
domega=2*pi/8192;
omega=-pi:domega:pi-domega;
yf1=abs(fftshift(fft(datar,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 utterance interpolated by a factor of 2');
xlabel('omega (radians/s)');
input('Original played back at twice');
input('orignal sampling rate, 2*Fs = 50 KHz'); 
soundsc(datar,2*Fs)
input('Interpolated data played back at'); 
input('orignal sampling rate, Fs = 12.5 KHz'); 
soundsc(y,Fs)