%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=-75:75;
h=sin((pi/3)*n)./(pi*n); h(76)=1/3;
h2=sin((pi/22)*n)./(pi*n); h2(76)=1/22;
h=(22/pi)*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,3*dl);
xlp(1:3:3*dl)=datar;
y=conv(xlp,h);
input('Utterance interpolated by a factor of 3 played back at 3*Fs=37.5 KHz'); 
soundsc(y,3*Fs)
%Nfft=16384; 
Nfft=32768;
Nfft=2*Nfft;
omega=linspace(-pi,pi,Nfft);
%yf1=abs(fftshift(fft(xlp,Nfft)));
yf1=abs(fftshift(fft(datar,Nfft)));
yf2=abs(fftshift(fft(y,Nfft)));
yf3=abs(fftshift(fft(xlp,Nfft)));
figure(1)
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)));
figure(2)
subplot(211)
domega=2*pi/512;
omegaf=-pi:domega:pi-domega;
plot(omegaf,hf,'Linewidth',4)
axis([-pi pi 0 max(hf)])
title('Frequency Response of LPF');
xlabel('omega (radians/s)');
subplot(212)
plot(omega,yf3,'Linewidth',3)
axis([-pi pi 0 max(yf3)])
title('DTFT of xlp, w/ 2 zeroes between each pair of sample values');
xlabel('omega (radians/s)');

