% polyphase.m
%
% quick script to demo polyphase filtering
% written 9/16/20 - rms

h = (7/8).^(0:8);
    
load mtlb;
m = mtlb; 
fs = 7000;	% sampling freq for the Matlab utterance
x = (mtlb(3000:3127));

x = x(6:26);
n = -5:15;

% show x and h

subplot(2,1,1),stem(n,x,'linewidth',2)
title('x[n]','color','r','fontsize',14)
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');
hplot = [zeros(1,5),h,zeros(1,7)];
subplot(2,1,2),stem(n,hplot,'linewidth',2)
title('h[n]','color','r','fontsize',14)
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');

pause

% show interpolation by L=3

L = 3;
xe = zeros(1,3*length(x));
for r = -2:5
    xe( 7+(L*r))  =x(6+r);
end
%%
subplot(3,1,1),stem(n,x,'linewidth',2)
title('x[n]','color','r','fontsize',14)
axis([-6,16,min(x),max(x)])

set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');
subplot(3,1,2),stem(n-1,xe(1:length(n)),'linewidth',2);
title('x_e[n]','color','r','fontsize',14)
axis([-6,16,min(xe),max(xe)])
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');

xi = conv(xe,h);
subplot(3,1,3),stem(n-1,xi(1:length(n)),'linewidth',2);
title('x_i[n]','color','r','fontsize',14)
axis([-6,16,min(xi),max(xi)])
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');


pause
%%
subplot(2,1,1),stem(n,hplot,'linewidth',2)
ylabel('h')
xlabel('n')
title('h[n]','color','r','fontsize',14)
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');

subplot(2,1,2)
[Hf,w] = freqz(h,1);
plot(w/pi,20*log10(abs(Hf)/max(abs(Hf))),'linewidth',2);
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(e^{j\omega})|, dB')
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');

pause

%%

subplot(2,1,1),stem(n-1,xe(1:length(n)),'linewidth',2);
title('x_e[m]','color','r','fontsize',14)
xlabel('m')
axis([-6,16,min(xe),max(xe)])
set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');


hf = fliplr(hplot);
for m = 0:10
    subplot(2,1,1),stem(n-1,xe(1:length(n)),'linewidth',2);
    title('x_e[m]','color','r','FontSize',14,'FontWeight','bold')
    xlabel('m')
    text(8,1,['n = [',num2str((-2:0)+floor(m/3)),']',],'FontSize',14,'FontWeight','bold')
    axis([-6,16,min(xe),max(xe)])
    set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');

    
    
    subplot(2,1,2),
    stem((n+m-10),hf,'linewidth',2)
    axis([-6,16,0,1])
    title('h[m-r]','color','r','FontSize',14,'FontWeight','bold')
    xlabel('r')
    text(8,.7,['m = [',num2str((mod(m,3))+(0:3:6)),']'],'FontSize',14,'FontWeight','bold')
    set(gca,'FontName','Helvetica','FontSize',14,'FontWeight','Bold','FontAngle','Italic');
    pause
end





