%Matlab file used for class demo conducted during Session 12
%illustrating upsampling by a factor of 2.
%A word utterance is sampled at 12.5 KHz, a zero is
%inserted between each pair of samples and the result
%played back at 25 KHz
clf
set(0,'defaultaxesfontsize',20);
%h=remez(50,[0 .333 .665 1],[1 1 0 0]);
EndL=99;
n=-EndL:EndL;
h=sin((pi/2)*n)./(pi*n); h(EndL+1)=0.5;
h2=sin((pi/32)*n)./(pi*n); h2(EndL+1)=1/32;
h=2*32*h.*h2;
data=getspeech('erf1s1t0.wav');
Fs=12500;
clf
%plot(data)
%end1=input('end1?');
%end2=input('end2?');
end1=500;
end2=13500;
datar=data(end1:end2);
dl=end2-end1+1;
input('Utterance played back at orignal sampling rate, Fs = 12.5 KHz'); 
soundsc(datar,Fs)
xlp=zeros(1,2*dl);
xlp(1:2:2*dl)=datar;
y=conv(xlp,h);
input('Utterance interpolated by a factor of 2 played back at 2*Fs=25 KHz'); 
soundsc(y,2*Fs)
FFTL=2^16;
omega=linspace(-pi,pi,FFTL);
%yf1=abs(fftshift(fft(xlp,FFTL)));
yf1=abs(fftshift(fft(datar,FFTL)));
yf2=abs(fftshift(fft(y,FFTL)));
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 orignal sampling rate, 2*Fs = 25 KHz'); 
soundsc(datar,2*Fs)
input('Interpolated data played back at orignal sampling rate, Fs = 12.5 KHz'); 
soundsc(y,Fs)
hf=abs(fftshift(fft(h,512)));
input('Plot of Frequency Response of LPF')
clf
domega=2*pi/512;
omega=-pi:domega:pi-domega;
plot(omega,hf,'Linewidth',4)
axis([-pi pi 0 max(hf)])
title('Frequency Response of LPF');
xlabel('omega (radians/s)');
