You are on page 1of 8

Lab Sheet 3

System Response, Convolution and Correlation


 Prerequisites
 Concepts of MATLAB
 Contents of Lab 2

 Outcomes
Upon completion of this lab, students should be able to
 solve difference equation using filter function.
 determine system response using filter function.
 use convolution and correlation functions.

 Outlines of This Lab


This lab comprises of the following:

 Lab Session 3.1: Solution to difference Equation


 Lab Session 3.2: Determination of system response using filter function
 Lab Session 3.3: Convolution and Correlation of sequences
 Lab Session 3.3: In Lab Evaluation
 Home Work

3.1: Solution to difference equations


Consider the difference equation
N M

a
k 0
k y(n k)  bm x(n m)
m0
A routine called filter is available in MATLAB to solve difference equation numerically,
given the input and the difference equation coefficients. In its simplest form, this routine is
invoked by,

y = filter(b,a,x)
where, b=[b0, b1...............bM]
and, a= [a0, a1………… ..aN]
Example 3.1:
Given the following difference equation
y(n) y(n 1) 0.9y(n 2) x(n); n
(a) Calculate and plot the impulse response h(n) at n= [ -20:120]
(b) Calculate and plot the step response s(n) at n= [ -20:120]
(c) Is the system specified by h(n) stable? Why?
Solution:
Here, a0 = 1, a1 = -1, a2 = 0.9,
b0 = 1, n1 = -20 and n2 = 120.

MATLAB code
b=[1];
Impulse Response
a=[1,-1,0.9]; 1
n1= -20; n2=120;
n=[-20:120]; 0.5
% Part a
h(n)

x=delta(0,n1,n2); 0
h=filter(b,a,x);
% Part b -0.5
x1=u(0,n1,n2);
-1
s=filter(b,a,x1); -20 0 20 40 60 80 100 120
n
figure Step Response
subplot(2,1,1);stem(n,h); 3
title('Impulse Response');
xlabel('n'); 2
ylabel('h(n)');
s(n)

subplot(2,1,2);
1
stem(n,s);
title('Step Response');
xlabel('n'); 0
-20 0 20 40 60 80 100 120
ylabel('s(n)');
n

To find the stability


>> sum(abs(h)) o/p : ans = 14.8785
h(n) is absolutely summable and so is stable.

3.2: Determination of system response using filter function


Example 3.2:
n
Consider an Accumulator system represented by y n    xk  . This can also be represented in a 1st
k  

order difference equation as yn  yn  1  xn . Find the output from this accumulator for an input of
rectangular pulse with a duration of 5 samples and amplitude 1.
The input can be represented as x(n) u(n) u(n 5).

MATLAB code
n1= -5; 1
n2= 10;
x= u(0,n1,n2)-u(5,n1,n2); 0.8
a=[1 -1];
0.6
b=[1];

x
y= filter(b,a,x); 0.4

n=n1:n2; 0.2
figure 0
subplot(2,1,1) -5 0 5 10
stem(n,x)
ylabel('x')
5
subplot(2,1,2)
stem(n,y) 4
ylabel('y')
3
y

0
-5 0 5 10

3.3: Convolution and Correlation


3.3.1. Convolution
Mathematically, a linear convolution sum is defined as


y(n) x(n) * h(n)  x(k)h(n k )


k 
In general, the convolution operation is used to describe the response of an LTI system. In DSP it is an
important operation and has many other uses. MATLAB does provide a built-in function called conv
the computes the convolution between two finite-duration sequences.

Description:
w = conv(u,v) convolves vectors u and v. Algebraically, convolution is the same operation as
multiplying the polynomials whose coefficients are the elements of u and v. Thus the conv function
assumes that the two sequences begin at n = 0 and is invoked by

>> y = conv(x,h);

Example 3.3:
If x= {3 11 7 6} and h=[4 -3 2], find their convolution.
MATLAB Code and Output:

>> x=[3 11 7 6]
>> h=[4 -3 2];
>> y=conv(x,h)

Output:
y=
12 35 1 25 -4 12
Modified function for convolution:
However, the conv function neither provides nor accepts any timing information if the sequences have
arbitrary zero position. To solve this inconvenience, a simple extension of the conv function, called
conv_m, which performs the convolution of arbitrary support sequences can now be designed as

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


% Modified convolution routine for signal processing
% --------------------------------------------------------------
% [y, ny] = concolution 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);

3.3.2 Correlation of sequences


Correlation is an operation used in many applications in digital signal processing. It is a measure of
the degree to which two sequences are similar. Given two real-valued sequences x(n) and y(n) of
finite energy, the crosscorrelation of x(n) and y(n) is a sequence rxy (l) , defined as


rxy (l)  x(n) y(n l)


n

The index l is called the shift or lag parameter. The special case when autocorrelation and is defined by
y(n) x(n) is called


rxx (l)  x(n)x(n l)


n
If we compare the convolution operation with the crosscorrelation of two sequences, we observe a close
resemblance. The crosscorrelation rxy (l) can be put in the form
rxy (l) x(l) y(l)
with the autocorrelation rxx (l) in the form
rxx (l) x(l) x(l)
Therefore these correlations can be computed using the conv function if sequences are of finite
duration.
Example 3.4: Correlation using xcorr function
Find and plot the correlation between the two following sequences using xcorr function.

x={1 2 3 4}
h={4 3 2 1};

MATLAB Code:
x=[1 2 3 4];
h=[4 3 2 1];
y=xcorr(x,h);

Example 3.5: Radar Application


In radar application, a narrow pulse x(n) is transmitted to the target at a distance. The reflected signal
y(n) from the target is received as a delayed and decayed version of x(n). The delay can be estimated
by correlating y(n) with x(n) and from this delay the distance between the transmitter and the target can
be found.
Let x(n) = u(n) - u(n-5) for n=0 to 20. Here y(n) = 0.8x(n-2) + 0.1w(n) where w(n) is Gaussian
sequence with mean 0 and variance 1. Compute the cross-correlation between y(n) and x(n).

MATLAB Code and Solution:


clear all Necessary functions:
close all
clc function[x n]= u(n0,n1,n2)
n1= 0; n=[n1:n2];
n2= 10; x=[n-n0]>=0;
n=n1:n2;
x= u(0,n1,n2)-
u(5,n1,n2);
shift=2; n1 n0 n2

[y1 ny1]= Where,


sigshift(x,shift,n); function[s n]=sigshift(x,shift,ind)
y= if shift >0 x=input signal
0.8*y1+0.1*randn(1,leng n=ind(1):(length(ind)+shift-1); shift=shifting
th(y1)); s = zeros(size(n)); index
[z lags]= xcorr(y,x); s(shift+1:end) = x(1:end); ind=input index
elseif shift <0 array
figure n=(shift):ind(end); s=shifted signal
subplot(3,1,1) s = zeros(size(n)); n=new index array
stem(n,x) s(1:end+shift) = x(1:end);
ylabel('x') end
subplot(3,1,2)
stem(ny1,y)
ylabel('y')
subplot(3,1,3)
stem(lags,z)
ylabel('z')
1

0.5
x

0
0 1 2 3 4 5 6 7 8 9 10
1

0.5
y

-0.5
0 2 4 6 8 10 12
4

2
z

0
-15 -10 -5 0 5 10 15

3.3.3 Correlation Coefficient


The correlation coefficient, denoted by r, is a measure of strength of the straight line or linear
relationship between two variables. Correlation coefficient take values between -1 and +1. A
positive value implies a positive association

Correlation coefficients measure the strength of association between two variables. The most
common correlation coefficient, called the Pearson product-moment correlation coefficient,
measures the strength of the linear association between variables.

The crosscorrelation coefficient of two sequences x(n) and y(n) is given by,
N

r (l) 
 [x(k) x][y(kl) y]
k 1
xy
N N

 [x(k) x]2  [y(k) y]2


k 1 k 1
The terms x and y are the mean values of x(n) and y(n) respectively.
The following points are the accepted guidelines for interpreting the correlation coefficient:

Value of coefficient Meaning


0 No linear relationship
+1 A perfect positive linear relationship
-1 A perfect negative linear relationship
0 to 0.3 Weak positive linear relationship
0 to -0.3 Weak negative linear relationship
0.3 to 0.7 Moderate positive linear relationship
-0.3 to -0.7 Moderate negative linear relationship
0.7 to 1 Strong positive linear relationship
-0.7 to -1 Strong negative linear relationship

Example 3.6:
Write a MATLAB program to compute correlation coefficient of two sequence.

MATLAB Code:
X=input('Input the 1st sequence:');
Y=input('Input the 2nd sequence:');
Xm=mean(X);
Ym=mean(Y); [R,C]=size(X); N=C; V1=0; V2=0;
for k=1:N
V1=V1+(X(k)-Xm)^2;
V2=V2+(Y(k)-Ym)^2;
end
DEN=sqrt(V1*V2); NUM=0;
for k=1:N
NUM=NUM+(X(k)-Xm)*(Y(k)-Ym);
end
disp('The correlation coefficient is:')
disp(NUM/DEN)

Run the above program for the following pairs of sequences and comment on the results:
(a) x(n) = [1 2 3] y(n) = [1 2 3]
(b) x(n) = [1 2 3] y(n) = [-1 -2 -3]
(c) x(n) = [1 2 3] y(n) = [3 2 1]
3.4: In Lab Evaluation
a) Two sequences are given by,
x(n) = [1 2 3] y(n) = [4 5 6]
i. Find their crosscorrelation using xcorr function
ii. Find their crosscorrelation using conv function

Home Work:

Q1. Consider a 20 point moving average filter


1
y(n)  [x n x n 1....................................... x(n 19)]
20
a) Generate 200 samples of a 1 Hz sine wave sampled at 40 Hz.
b) Add some noise to generate a noisy signal.
c) Filter the noisy signal through the 20 point MA filter.
d) Plot each signal to display the effects of noise and smoothing.

Q2. Consider an accumulator system. The input to the system is x(n) = u(n) – u(n-10).

a) Find the difference equation for the accumulator.


b) Find the output y(n) using filter function.
c) Explain the nature of the output.

You might also like