Calculating Convolution Sum With Matlab Code

Calculating Convolution Sum With Matlab Code

BASIC

Convolution is the process by which an input interacts with an LTI system to produce an output.


Convolution of an input signal x[n] with a system having impulse response h[n] is given by,

Convolution Sum

TABLE METHOD

Table method to find convolution sum has he steps below.

  • Step 1: List the index ‘k’ covering a sufficient range
  • Step 2: List the input x[k]
  • Step 3: Obtain the reversed sequence h[-k], and align the rightmost element of h[n-k] to the leftmost element of x[k]
  • Step 4: Cross-multiply and sum the nonzero overlap terms to produce y[n]
  • Step 5: Slide h[n-k] to the right by one position
  • Step 6: Repeat step 4; stop if all the output values are zero or if
  • required.

Example

Find the convolution sum of the given sequence, x[k]=[3, 1, 2], h[k]=[3, 2, 1] Where x and h both have there first value at 0’th position.

Solution

Start of k = –(length of h – 1) = –(3-1) = – 2

End of k = (length of h + length of x – 1) = (3+3–1) = 5

h(-k) = [1, 2, 3]

h(-k) is the flipped array of h. That is first value at last position and last value at first position.

Now, draw a table. At first row put all values of k from start of k to end of k.

k–2–1012345

At second row put all values of x[k]

x[k]00312000

Put the values of h(-k) in third row.

h[-k]12300000

Shift the non zero values of h(-k) to right by 1 bit and put them in fourth row.

h[1-k]01230000

Similarly for other rows. Shift the values of h(-k) as long as it is inside the boundary.

h[2-k]00123000
h[3-k]00012300
h[4-k]00001230
h[5-k]00000123

Full Table

k-2-1012345
x(k)00312000
h(-k)12300000
h(1-k)01230000
h(2-k)00123000
h(3-k)00012300
h(4-k)00001230
h(5-k)00000123

Now, cross multiply and sum the overlapped terms.

That is multiply each term of x(k) with respective term of h(-k) and sum them to find X(0).

Similarly multiply and sum h(1-k), h(2-k), h(3-k) with x(k) to get X(1), X(2), X(3) etc.

Here,

X(0)=3×3=9

X(1)=3×2+1×3=9

X(2)=3×1+1×2+2×3=11

X(3)=1×1+2×2=5

X(4)=2×1=2

X(5)=0 [No overlap]

Hence, X(m) = [9, 9, 11, 5, 2, 0]

MATLAB CODE

To calculate convolution sum first of all we need to know the value of x(n) and h(n). Moreover we need to know which one value of them is located at zero position. We will ask for this to the user.

x = input('Enter values of x(n): ');
zerox = input('Zero Position of x: ');
h = input('Enter values of h(n): ');
zeroh = input('Zero Position of h: ');

Then we need to know the starting value of k and end value of k. Carefully listen that we will say these values of k as actual index in later parts of the tutorial. We will find the start value of k and end value of k from the formula above.

k1 = -(length(h)-1);
k2 = length(x)+length(h)-1;

If we see here the values of k is started from negative. But MatLAB cannot take negative index. So we will take a positive k where the value of k will be started from 1 and will run till (k2-k1+1). That is the positive part, equal part of negative and 1 for 0 position. See the fraction.

kstart = 1;
kend = k2 - k1 + 1;

As we need to multiply value of x and values of h so they cannot be empty. Empty array or variable cause an error in matlab. So, we will declare xk and hk array with 0 from index kstart to kend (positive index).

xk = zeros(kstart,kend);
hk = zeros(kstart,kend);

Now we will put the values of x to xk with a loop as the zero positioned value of x is placed at (-k1+1) position. That is our current zero in positive index respective to actual index.

for i=1:length(x)
  xk(-k1+i-zerox+1) = x(i);
endfor

flip the value of h(k) to find h(-k).

h = flip(h);

Put the values of flipped h to the array hk as the zero positioned value is currently at (-k1+1) position(current zero).

for i=1:length(h)
  hk(i+zeroh-1) = h(i);
endfor

Now take a variable shift that will count the shifting of h(-k).

shift = 0;

Now run two loop where one is nested in another. One loop (external) will find the values of X(m). The loop inside will find the sum of h(k) and x(k).

The external loop will run from 1 to (k2-zeroh+1+1). Here k2 is the last limit of our actual index. zeroh is the position at what h denote index zero. zeroh is subtracted because when we flip h zeroh bit goes to right side of the array. And that cross the boundary quickly from x(k). One 1 is to cover zeroh and another is to cover k2.

We set X(shift+1) to 0 at each turn because it is added as previous value (summed). So, it cannot be empty.

The internal for loop runs from 1 to (length(h)+zeroh-1). Because we have no need to multiply and add values where h(k)=0. We assign the summed value in X(shift+1). The cross multiplication at each turn of this loop is hk(i)*xk(i+shift). That is for each turn we don’t shifting h(-k) to right rather we are shifting x(k) to left. And that is same.

You need to increase the value of shift as index and shifting size increase. That will be done after the internal loop.

for j=1:k2-zeroh+1+1
  X(shift+1) = 0;
  for i=1:length(h)+zeroh-1
    X(shift+1) = X(shift+1) + hk(i)*xk(i+shift);
  endfor
  shift = shift+1;
endfor

Now, Everything is done. Display the value with corresponding title.

disp('Value of x(k): ');
disp(xk);
disp('Value of h(-k): ');
disp(hk);
disp('Convolution Sum: ');
disp(X);

Plot the result at discrete format with respective title at (3,1) subplot.

subplot(3,1,1);
stem(1:length(xk),xk);
title('x(n)');
subplot(3,1,2);
stem(1:length(hk),hk);
title('h(n)');
subplot(3,1,3);
stem(1:length(X),X);
title('X(m)');

FULL CODE

x = input('Enter values of x(n): ');
zerox = input('Zero Position of x: ');
h = input('Enter values of h(n): ');
zeroh = input('Zero Position of h: ');
k1 = -(length(h)-1);
k2 = length(x)+length(h)-1;
kstart = 1;
kend = k2 - k1 + 1;
xk = zeros(kstart,kend);
hk = zeros(kstart,kend);
for i=1:length(x)
  xk(-k1+i-zerox+1) = x(i);
endfor
h = flip(h);
for i=1:length(h)
  hk(i+zeroh-1) = h(i);
endfor
shift = 0;
for j=1:k2-zeroh+1+1
  X(shift+1) = 0;
  for i=1:length(h)+zeroh-1
    X(shift+1) = X(shift+1) + hk(i)*xk(i+shift);
  endfor
  shift = shift+1;
endfor
disp('Value of x(k): ');
disp(xk);
disp('Value of h(-k): ');
disp(hk);
disp('Convolution Sum: ');
disp(X);
subplot(3,1,1);
stem(1:length(xk),xk);
title('x(n)');
subplot(3,1,2);
stem(1:length(hk),hk);
title('h(n)');
subplot(3,1,3);
stem(1:length(X),X);
title('X(m)');

I think your experience with tutorial is quite good. If you have to seek anything ask in comment or report by form.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *