
% Demo of discrete-time resampling
% Last revised 9/14/99 - rms

clear all
close all

load mtlb;
m = mtlb; 
fs = 7000;	% sampling freq for the Matlab utterance
x = (mtlb(3000:3127));
w = -pi:2*pi/257:pi;
f = w*fs/(2*pi);
n = 0:127;
[X] = dtft(x',n,w);

subplot(2,1,1),stem(n,x,'linewidth',1.5)
title('Original waveform','color','r','fontsize',14,'linewidth',1.5)
subplot(2,1,2),plot(f,abs(X),'linewidth',1.5)
title('Its DTFT','color','r','fontsize',14,'linewidth',1.5)
p = audioplayer(mtlb,fs);
playblocking(p)
pause

% Upsampling by a factor of 2

m21 = resample(m,2,1);
fs = 14000;
n21 = 0:255;
x21 = (m21(6000:((2*3128)-1)));
f21 = w*fs/(2*pi);
[X21] = dtft(x21',n21,w);

subplot(2,1,1),stem(n,x,'linewidth',1.5)
title('Upsampling by 2','color','r','fontsize',14,'linewidth',1.5)
axis([0 150 -2 2])
subplot(2,1,2),stem(n21,x21,'linewidth',1.5)
axis([0 300 -2 2])
p = audioplayer(mtlb,7000);
playblocking(p)
p = audioplayer(m21, 14000);
playblocking(p)
pause

subplot(2,1,1),plot(w/pi,abs(X)) 
title('Original DTFT','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
subplot(2,1,2),plot(w/pi,abs(X21),'linewidth',1.5)
title('DTFT for Upsampling by 2','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
p = audioplayer(mtlb, 7000);
playblocking(p)
p = audioplayer(m21, 14000);
playblocking(p)
pause

% Downsampling by a factor of 2

h = [1 zeros(1,20)];
m12 = resample(m,1,2,h);
fs = 3500;

n12 = 0:63;
x12 = (m12(1500:((3128/2)-1)));
f12 = w*fs/(2*pi);
[X12] = dtft(x12',n12,w);

subplot(2,1,1),stem(n,x,'linewidth',1.5)
title('Downsampling by 2','color','r','fontsize',14)
subplot(2,1,2),stem(n12,x12,'linewidth',1.5)
p = audioplayer(mtlb, 7000);
playblocking(p)
p = audioplayer(m12, 3500);
playblocking(p)

pause

subplot(2,1,1),plot(w/pi,abs(X),'linewidth',1.5)
title('Original DTFT','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
subplot(2,1,2),plot(w/pi,abs(X12),'linewidth',1.5)
title('DTFT for Downsampling by 2','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
p = audioplayer(mtlb, 7000);
playblocking(p)
p = audioplayer(m12, 3500);
playblocking(p)
pause



% Downsampling by a factor of 3

m13 = resample(m,1,3,h);
fs = 7000/3;
n13 = 0:floor(128/3);
x13 = (m13(1000:1042,1));
f13 = w*fs/(2*pi);
[X13] = dtft(x13',n13,w);

subplot(2,1,1),stem(n,x,'linewidth',1.5)
title('Downsampling by 3','color','r','fontsize',14)
subplot(2,1,2),stem(n13,x13,'linewidth',1.5)
p = audioplayer(mtlb, 7000);
playblocking(p)
p = audioplayer(m13, 2333);
playblocking(p)
pause

subplot(2,1,1),plot(w/pi,abs(X),'linewidth',1.5)
title('Original DTFT','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
subplot(2,1,2),plot(w/pi,abs(X13),'linewidth',1.5)
title('DTFT for Downsampling by 3','color','r','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
p = audioplayer(mtlb, 7000);
playblocking(p)
p = audioplayer(m13, 2333);
playblocking(p)
pause


