Pages

Tuesday 26 February 2013

Block Convolution Using Overlap Add Method (Matlab 2012a)

Program:

clc;
clear all;
x = input('Enter the sequence x(n) = ');
h = input('Enter the sequence h(n) = ');
n1 = length(x);
n2 = length(h);
N = n1+n2-1;
y = zeros(1,N);
h1 = [h zeros(1,n2-1)];
n3 = length(h1);
y = zeros(1,N+n3-n2);
H = fft(h1);
for i = 1:n2:n1
if i<=(n1+n2-1)
x1 = [x(i:i+n3-n2) zeros(1,n3-n2)];
else
x1 = [x(i:n1) zeros(1,n3-n2)];
end
x2 = fft(x1);
x3 = x2.*H;
x4 = round(ifft(x3));
if (i==1)
    y(1:n3) = x4(1:n3);
else
y(i:i+n3-1) = y(i:i+n3-1)+x4(1:n3);
end
end
subplot(3,1,1);
stem(x(1:n1));
grid on;
title('Input Sequence x(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,2);
stem(h(1:n2));
grid on;
title('Input Sequence h(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,3);
disp('Block Convolution Using Overlap Add Method = ');
disp(y(1:N));
stem(y(1:N));
grid on;
title('Block Convolution Using Overlap Add Method');
xlabel('Time --->');
ylabel('Amplitude --->');


Output:

Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1]
Enter the sequence h(n) = [1 2 3 -1]
Block Convolution Using Overlap Add Method =
     1     4     6     5     2    11     0   -16    -8     3     8     5     3    -5     1


Waveform:

 

4 comments:

  1. Enter the sequence x(n) = [3 -1 0 1 3 2 0 1 2 1]
    Enter the sequence h(n) = [1 1 1]
    Index exceeds matrix dimensions.

    Error in ovrlapaadd (line 15)
    x1 = [x(i:i+n3-n2) zeros(1,n3-n2)];

    AN ERROR IS SHOWING UP LIKE THIS WHY?

    ReplyDelete
  2. What is the logic didn't understand

    ReplyDelete
  3. Line 9 should be: h1 = [h zeros(1,n1-1)];

    ReplyDelete