%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]);
n=-50:50;
h=sin((pi/2)*n)./(pi*n); h(51)=0.5;
h2=sin((pi/16)*n)./(pi*n); h2(51)=1/16;
h=(16/pi)*h.*h2;
data=audioread('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)
domega=2*pi/8192;
omega=-pi:domega:pi-domega;
%yf1=abs(fftshift(fft(xlp,8192)));
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 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)');