MATLAB Assignmnet

profileamarafie
Lab4-Handout.pdf

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

1

LAB 4: CONVOLUTION

Background & Concepts

Convolution is denoted by:

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

Your book has described the "flip and shift" method for performing convolution. First, we

set up two signals 𝑥[𝑘] and ℎ[𝑘]:

Flip one of the signals, say ℎ[𝑘], to form ℎ[−𝑘]:

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

2

Shift ℎ[−𝑘] by n to form ℎ[𝑛 − 𝑘]. For each value of 𝑛, form 𝑦[𝑛] by multiplying and

summing all the element of the product of𝑥[𝑘]ℎ[𝑛 − 𝑘], −∞ < 𝑘 < ∞. The figure

below shows an example of the calculation of𝑦[1]. The top panel shows𝑥[𝑘]. The

middle panel showsℎ[1 − 𝑘]. The lower panel shows𝑥[𝑘]𝑦[1 − 𝑘]. Note that this is a

sequence on a 𝑘 axis. The sum of the lower sequence over all k gives 𝑦[1] = 2.

We repeat this shifting, multiplication and summing for all values of 𝑛 to get the

complete sequence 𝑦[𝑛]:

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

3

The conv Command

conv(x,h) performs a 1-D convolution of vectors 𝑥 and ℎ. The resulting vector 𝑦

has length length(𝑦) = length(𝑥) + length(ℎ) − 1. Imagine vector 𝑥 as being

stationary and the flipped version of ℎ is slid from left to right. Note that conv(x,h) =

conv(h,x). An example of the convolution of two signals and plotting the result is

below:

>> x = [0.5 0.5 0.5]; %define input signal x[n]

>> h = [3.0 2.0 1.0]; %unit-pulse response h[n]

>> y = conv(x,h); %compute output y[n] via convolution

>> n = 0:(length(y)-1); %for plotting y[n]

>> stem(n,y) % plot y[n]

>> grid;

>> xlabel('n');

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

>> title('Output of System via Convolution');

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

4

Deconvolution

The command [q,r] = deconv(v,u), deconvolves vector u out of vector v, using long

division. The quotient is returned in vector q and the remainder in vector r such that

v = conv(u,q)+r. If u and v are vectors of polynomial coefficients, convolving them is

equivalent to multiplying the two polynomials, and deconvolution is polynomial

division. The result of dividing v by u is quotient q and remainder r. An examples is

below:

If

>> u = [1 2 3 4];

>> v = [10 20 30];

The convolution is:

>> c = conv(u,v)

c =

10 40 100 160 170 120

Use deconvolution to recover v.

>> [q,r] = deconv(c,u)

q =

10 20 30

r =

0 0 0 0 0 0

This gives a quotient equal to v and a zero remainder.

Structures

Structures in Matlab are just like structures in C. They are basically containers that

allow one to group together more than one type of data under the umbrella of one

variable name. As we mentioned previously, Matlab always assumes that the index

of the first element of an array is 1. So, when we just say:

>> x = [1 2 3 4 5];

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

5

This means that 𝑥[1] = 1, 𝑥[2] = 2, and so on. But, what if we want to express a

sequence 𝑥[−1] = 1, 𝑥[0] = 2, . ..? We obviously need to provide the user with some

additional information in addition to the array data, namely an offset. An offset of -1

tells the user that "we want you to interpret the first point in the Matlab array as

corresponding to 𝑥[−1]. We can use structures for this purpose. The form of a

structure is variable Name.field. For example:

>> x.data = [1 2 3 4 5];

>> x.offset = -1;

>> x

x =

data: [1 2 3 4 5]

offset: -1

We have declared a variable x, with two fields. The data field contains an array; the

offset field contains another number. Structures of this type can be useful for us in

expressing sequences in Matlab.

ELEG 320L – Signals & Systems Laboratory /Dr. Jibran Khan Yousafzai Lab 4

6

EXERCISES

Exercise 1:

Let

𝑓[𝑛] = 𝑢[𝑛] – 𝑢[𝑛 − 4]

𝑔[𝑛] = 𝑛. 𝑢[𝑛] – 2(𝑛 − 4). 𝑢[𝑛 − 4] + (𝑛 − 8). 𝑢[𝑛 − 8]

Make stem plots of the following convolutions using MATLAB.

1. 𝑓[𝑛] ∗ 𝑓[𝑛]

2. 𝑓[𝑛] ∗ 𝑓[𝑛] ∗ 𝑓[𝑛]

3. 𝑓[𝑛] ∗ 𝑔[𝑛]

4. 𝑔[𝑛] ∗ 𝛿[𝑛]

5. 𝑔[𝑛] ∗ 𝑔[𝑛]

Exercise 2:

Consider the interconnection of the LTI systems depicted in the following figure:

Generate the impulse response of the overall system when:

ℎ1[𝑛] = 2𝑛 {𝑢[𝑛] − 𝑢[𝑛 − 4]}, 0 ≤ 𝑛 ≤ 4

ℎ2[𝑛] = 𝛿[𝑛] − 4𝛿[𝑛 − 4], 0 ≤ 𝑛 ≤ 5

ℎ3[𝑛] = ℎ4[𝑛] = 𝛿[𝑛 − 5], 0 ≤ 𝑛 ≤ 6

Exercise 3:

Using structures & conv command, find out the convolution of two sequences with

arbitrary starting points.