You are on page 1of 12

Lab :4 Discrete Convolution

OBJECTIVES:

 Figure out deficiency of conv() command


 Make a function to perform proper convolution sum along with index calculation.

INTRODUCTION

Convolution is a mathematical operation on two functions to generate another function. In the


signals domain convolution is the operation on two signals to generate a new signal. Consider
the following system:

x[n] y[n]

h[n]

Fig.4-1

The output y[n] will be the convolution of input x[n] and impulse response of the system will be
h[n].

TYPES OF CONVOLUTION

1. CONTINUOUS TIME

In continuous time convolution we take input, impulse response and the output in continuous
time. The output then is represented by the convolution integral given by:

∞ ∞ (4-1)
y(t) = x(t) ∗ h(t) = ∫−∞ x(τ)h(t − τ)dτ=∫−∞ h(τ)x(t − τ)dτ
Lab :4 Discrete Convolution

2. DISCRETE TIME

In discrete time convolution we take input, impulse response and the output in discrete time.
The output then is represented by the convolution sum given by:

𝑦[𝑛] = 𝑥[𝑛] ∗ ℎ[𝑛] = ∑∞ ∞


𝑘=−∞ 𝑥[𝑘]ℎ[𝑛 − 𝑘] = ∑𝑘=−∞ ℎ[𝑘]𝑥[𝑛 − 𝑘]
(4-2)

STEPS TO CARRY OUT CONVOLUTION

Convolution is a step-by-step procedure. Basic points to carry out the convolution are bulleted
below:

 Assume input x[n] and impulse response h[n] are available.


 Change the variable from n to k of impulse response i.e. h[n] h[k].
 Flip h[k] to make it h[-k]. (We can reverse either x[n] or h[n] on our choice).
 Shift h[k] to the left by n i.e. make it h[n-k].
 Slide h[n-k] to the right over x[n] index by index.
 Multiply the overlapping weights of indices and calculate the sum.
 Carry on this process unless h[n-k] slides past all of x[n].
 The value of sum will be the convolution sum of x[n] and h[n].

EXAMPLE OF CONVOLUTION

Let us take on an example of convolution. Consider the input signal

x[n] = u[n] – u[n-3]

and the impulse response

h[n] = u[n] – u[n-2]

The input signal is:


Lab :4 Discrete Convolution

Fig.4-2

The impulse response is given by:

Fig.4-3

And finally the output of the convolution of x[n] and y[n] is given by:
Lab :4 Discrete Convolution

Fig.4-4
Lab :4 Discrete Convolution

LAB TASK

By reading out the pre lab document most of us would have an idea about convolution. But that
was how we can perform convolution mathematically. In this lab handout we will explain how
convolution is done in MATLAB. By following the explained step by step procedure, we will
learn convolution using MATLAB.

TASK1: CONV FUNCTION

MATLAB provides a built in function named conv(x,h) that computes the convolution of two
signals. Let us take on an example to explore this function.

EXAMPLE

>> x = [3,11,7,0,-1,4,2];

>> h = [2,3,0,-5,2,1];

>> y = conv(x,h)

y=

6 31 47 6 -51 -5 41 18 -22 -3 8 2

This convolution functions proves to be very useful but it misses one of the most important
things of convolution which is discussed below.

PROBLEM WITH BUILT-IN CONV FUNCTION


Lab :4 Discrete Convolution

The conv function assumes that the two sequences begin at n = 0. Moreover, this function
neither provides nor accepts any timing information if the sequences have arbitrary starting
points. What is required is the beginning and end point of y[n]. Given finite duration x[n] and
h[n], it is easy to determine these points. Let

{x[n]; nxb ≤ n ≤ nxe}

And

{h[n]; nhb ≤ n ≤ nhe}

be the two finite duration sequences, then starting and end points of y[n] can be written as

nyb = nxb + nhb and nye = nxe+nhe

TASK2: MAKING OUR OWN CONVOLUTION FUNCTION (MYCONV)

Now we can define our own function in MATLAB that is slightly modified from the built in conv
function. Our modified function will use the conv function as underlying function but it will
perform convolution of arbitrary support sequences. How to define our own function?

First go to File and then click New and then M-File. Write the following code in the newly
opened editor file.

function [y,ny] = myconv(x,nx,h,nh)

%[y,ny] = convolution result

%[x,nx] = First signal

%[h,nh] = Second signal

nyb = nx(1) + nh(1);

nye = nx(length(x)) + nh(length(h));

ny = [nyb : nye];

y = conv(x,h);

end
Lab :4 Discrete Convolution

This is our own defined function named myconv. Now we can use this function just as any other
function in MATLAB.

Now we will use this function for the convolution of signals having arbitrary starting and ending
points. Consider the signals in previous example

TASK3: MY CONV FUNCTION

>> x = [3,11,7,0,-1,4,2];

>>nx = [-3:3];

>> h = [2,3,0,-5,2,1];

>>nh = [-1:4];

>> [y,ny] = myconv(x,nx,h,nh)

y=

6 31 47 6 -51 -5 41 18 -22 -3 8 2

ny =

-4 -3 -2 -1 0 1 2 3 4 5 6 7

Now this function also shows the starting and ending points of y[n] and values are
corresponding indices. We can plot this y[n] by the commands:

>> stem(ny,y);

>> xlabel('n');

>>ylabel('y[n]');

>> title('Convolution');

The output should be like this:


Lab :4 Discrete Convolution

Fig.4-5

TASK4:

Consider the input signal

x[n] = u[n+2] – u[n-2]

and impulse response of the system as

h[n] = u[n+2] – u[n-2]

Both input and impulse response is taken to be the same for the sake of simplicity. We define
output as the convolution of input and impulse response.

y[n] = x[n] * h[n].

You are required to:

 Write the MATLAB code to find y[n].


 Plot x[n], h[n] and y[n] in one window. (Hint: Use subplot).
 Label x-axis of all three graphs as n.
 Label y-axis of x[n], h[n] and y[n] as Input x[n], Impulse Response h[n] and Output y[n]
respectively.
 Apply grid on all three graphs.
 Give the title Convolution to all three graphs.
Lab :4 Discrete Convolution

TASK5: CONV FUNCTION

Consider the input signal

x[n] = 1, 0 ≤ n ≤ 4

0, otherwise

and the impulse response of the system as

h[n] = 0.5n, 0 ≤ n ≤ 6

0, otherwise

then the output will be

y[n] = x[n] * h[n]

You are required to:

 Write the MATLAB code to find y[n].


 Plot x[n], h[n] and y[n] in one window. (Hint: Use subplot).
 Label x-axis of all three graphs as n.
 Label y-axis of x[n], h[n] and y[n] as Input x[n], Impulse Response h[n] and Output y[n]
respectively.
 Apply grid on all three graphs.
 Give the title Convolution to all three graphs.
Lab :4 Discrete Convolution

LAB ASSIGNMENTS

P.1

When the convolution of a signal is performed with itself, it is called correlation which is a very
important concept in communications. This task is related to this concept. You have to convolve
the signal with itself. The signal is given by:

x[n] = 1, -2≤n≤0

-1, 0<n≤2

You are required to:

 Write the MATLAB code to find y[n].


 Plot x[n] and y[n] in one window. (Hint: Use subplot).
 Label x-axis of both graphs as n.
 Label y-axis of x[n] and y[n] as Input x[n] and Output y[n] respectively.
 Apply grid on all three graphs.
 Give the title Convolution to both the graphs.
Lab :4 Discrete Convolution

Observations/Comments/Explanation of Results:

Score of Student in this Lab: ___________________________________________

Instructor’s Signatures with Date: _______________________________________


Lab :4 Discrete Convolution

Questions:

1. What is convolution?
2. Explain types of convolution.
3. What are different steps to carry out convolution?
4. How will you find convolution in MATLAB?
5. What is the difference between plot and stem?
6. How will you apply grid on graphs?
7. How will you write x[n] in MATLAB?
x[n] = 1, 0 ≤ n ≤ 4
0, otherwise

You might also like