Professional Documents
Culture Documents
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.
a
k 0
k y(n k) bm x(n m)
m0
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
order difference equation as yn yn 1 xn . 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
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
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
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);
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
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(kl) y]
k 1
xy
N N
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:
Q2. Consider an accumulator system. The input to the system is x(n) = u(n) – u(n-10).