1.5
magnitude
0.5
-0.5
0 10 20 30 40 50 60 70 80 90 100
n
ans =
6.5714
c. Write MATLAB code to plot the step response of the system for 0 ≤ 𝑛 ≤ 100.
clc;
close all;
clear all;
n = 0:100;
b = [1 2 1];
a = [1 -0.5 0.25];
x = ones(1,101);
s = filter(b,a,x);
stem(n,s,'r')
xlabel('n')
ylabel('magnitude')
title('Step response of y(n)- 0.5y(n-1)+ 0.25y(n-2)= x(n)+ 2x(n-1)+ x(n-3)')
Step response of y(n)- 0.5y(n-1)+ 0.25y(n-2)= x(n)+ 2x(n-1)+ x(n-3)
6
4
magnitude
0
0 10 20 30 40 50 60 70 80 90 100
n
d. If the input to the system is 𝑥(𝑛) = [5 + 3 cos(0.2𝜋𝑛) + 4 sin(0.6𝜋𝑛)]𝑢(𝑛). Write MATLAB code to
plot the response 𝑦(𝑛) 𝑓𝑜𝑟 0 ≤ 𝑛 ≤ 200.
clc;
close all;
clear all;
n = 0:200;
b = [1 2 1];
a = [1 -0.5 0.25];
x = (5 + 3*cos((0.2*pi).*n) + 4*sin((0.6*pi).*n)).*ones(1,201);
h = filter(b,a,x);
stem(n,h,'r')
xlabel('n')
ylabel('magnitude')
title('Output Signal')
Output Signal
50
45
40
35
30
magnitude
25
20
15
10
0
0 20 40 60 80 100 120 140 160 180 200
n
In MATLAB, the function filter is used to solve the difference equation numerically, given the input and
the difference equation co-efficients. In simple it is
>> y=filter(b,a,x)
where b=[b0, b1, b2,…bM] and a=[a0, a1, a2, … , aN] and x is input sequence. The output y has the
same length as the input x. The co-efficient a0 must not be zero.
Task 2: A discrete system is realized by the structure as shown in Fig 3.
clc
close all;
clear all;
n = 0:20;
b = [1 1 1 1];
a = [11.1 10.1 11 1];
x = [1 zeros(1,20)];
h = filter(b,a,x);
stem(n,h)
xlabel('n')
ylabel('magnitude')
title('Impulse response')
Impulse response
0.1
0.08
0.06
0.04
magnitude
0.02
-0.02
-0.04
-0.06
-0.08
0 2 4 6 8 10 12 14 16 18 20
n
clc
close all;
clear all;
n = 0:20;
b = [1 1 1 1];
a = [11.1 10.1 11 1];
u = ones(1,21);
u1 = filter(b,a,u);
stem(n,u1)
xlabel('n')
ylabel('magnitude')
title('Step response')
Step response
0.18
0.16
0.14
0.12
magnitude
0.1
0.08
0.06
0.04
0.02
0
0 2 4 6 8 10 12 14 16 18 20
n
h=
Columns 1 through 9
Columns 10 through 18
Columns 19 through 21
As h[n] is not zero for n less than zero thus system is not causal.