% Simple script to demonstrate overlap-add algorithm
% Recoded 10/11/00 - Richard Stern
% Last revised 10/18/05 - rms

% DFT size 16
% P = 4

% Designing lowpass filter

% n = 4;
% f = [0 .2 .4 1];
% a = [1 1 0 0];
% b = remez(n,f,a);

b= [   0.1723    0.2726    0.2813    0.2726    0.1723];

load welcome16;
x = x16(101:228);
x = x(1:36);

y = conv(x,b);
n = 0:39;

subplot(3,1,1),stem(n,[x 0 0 0 0])
ylabel('x[n]','fontsize',14)
subplot(3,1,2),stem(n,[b zeros(1,35)])
ylabel('h[n]','fontsize',14)
subplot(3,1,3),stem(n,y)
xlabel('n','fontsize',14)
ylabel('y[n]','fontsize',14)

pause

% OLA demo using L = 12

% Show segmentation of x

Hk = fft(b,16);

subplot(4,1,1),stem(n,[x 0 0 0 0]);
ylabel('x[n]','fontsize',14)

subplot(4,1,2),stem([])
axis([0 40 -.01 .02])
subplot(4,1,3),stem([])
axis([0 40 -.01 .02])
subplot(4,1,4),stem([])
axis([0 40 -.01 .02])
xlabel('n','fontsize',14)

pause


x1 = x(1:12);
X1k = fft(x1,16);
Y1k = X1k.*Hk;
y1 = real(ifft(Y1k,16));

subplot(4,1,2),stem((0:11),x1);
axis([0 40 -.01 .02])
ylabel('x1[n]','fontsize',14)

pause

x2 = x(13:24);
X2k = fft([x2 0 0 0 0],16);
Y2k = X2k.*Hk;
y2 = real(ifft(Y2k,16));

subplot(4,1,3),stem((12:23),x2);
axis([0 40 -.01 .02])
ylabel('x2[n]','fontsize',14)

pause
x3 = x(25:36);
X3k = fft([x3 0 0 0 0],16);
Y3k = X3k.*Hk;
y3 = real(ifft(Y3k,16));

subplot(4,1,4),stem((24:35),x3);
axis([0 40 -.01 .02])
ylabel('x3[n]','fontsize',14)
xlabel('n','fontsize',14)

pause


% Now show segmentation of y

subplot(5,1,1),stem(n,y);
hold on
plot(n,zeros(size(n)))
xlabel('n','fontsize',14)
ylabel('y[n]','fontsize',14)

subplot(5,1,2),stem([])
axis([0 40 -.01 .02])
hold on
plot(n,zeros(size(n)))
subplot(5,1,3),stem([])
hold on
plot(n,zeros(size(n)))
axis([0 40 -.01 .02])
subplot(5,1,4),stem([])
hold on
plot(n,zeros(size(n)))
axis([0 40 -.01 .02])
subplot(5,1,5),stem([])
hold on
plot(n,zeros(size(n)))
axis([0 40 -.01 .02])

pause

subplot(5,1,2),stem((0:15),y1);
axis([0 40 -.01 .02])
ylabel('y1[n]','fontsize',14)
axis([0 40 -.01 .02])

subplot(5,1,5),stem((0:15),y1)
hold off
axis([0 40 -.01 .02])
ylabel('Sum[n]','fontsize',14)
xlabel('n','fontsize',14)

pause

subplot(5,1,3),stem((12:27),y2);
axis([0 40 -.01 .02])
hold on
plot(n,zeros(size(n)))
ylabel('y2[n]','fontsize',14)
axis([0 40 -.01 .02])

partial = [y1 zeros(1,12)] + [zeros(1,12) y2];
subplot(5,1,5)
hold off
stem((0:27),partial)
hold on
plot(n,zeros(size(n)))
axis([0 40 -.01 .02])
ylabel('Sum[n]','fontsize',14)
xlabel('n','fontsize',14)

pause

subplot(5,1,4),stem((24:39),y3);
axis([0 40 -.01 .02])
ylabel('y3[n]','fontsize',14)
axis([0 40 -.01 .02])

partial = [y1 zeros(1,24)] + [zeros(1,12) y2 zeros(1,12)] + [zeros(1,24) y3];
subplot(5,1,5)
hold off
stem((0:39),partial)
axis([0 40 -.01 .02])
hold on
plot(n,zeros(size(n)))
ylabel('Sum[n]','fontsize',14)
xlabel('n','fontsize',14)

pause
