-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_5_tut2.m
91 lines (78 loc) · 1.64 KB
/
lab_5_tut2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
clear variables
close all
clc;
n1 = -20:1:20;
n2 = -40:2:40;
x = sin(n1);
h = exp(-0.2 * n2);
y2 = conv(x,h);
N = length(x);
M = length(h);
Ny = N + M -1;
y = zeros(1,Ny);
for i = 1:N
for k = 1:M
y(i+k-1) = y(i+k-1) + h(k)*x(i);
end
end
m = 0: Ny-1; %% time scale of plots
% Make plot
figure
subplot(2,1,1);
stem(m,y,'linewidth',2,'color','m')
grid;
a = title('Output of an LTI System y(n)');
set(a,'fontsize',10);
a = ylabel('y(n)');
set(a,'Fontsize',10);
a = xlabel('n');
set(a,'Fontsize',10);
% Using matlab built in function (you get the same results)
subplot(2,1,2);
stem(m,y2,'linewidth',2,'color','r')
grid;
a = title('Output y(n) using conv(x,h)');
set(a,'fontsize',10);
a = ylabel('y(n)');
set(a,'Fontsize',10);
a = xlabel('n ');
set(a,'Fontsize',10);
%%
clear variables
close all
clc;
t1 = 0:0.1:100;
t2 = -50:0.1:50;
h1 = exp(-2*t2);
x1 = sin(t1);
y_auto = conv(x1,h1);
N1 = length(x1);
M1 = length(h1);
Nyct = N1 + M1 -1 ;
y1 =zeros(1,Nyct);
for i = 1:N1
for k = 1:M1
y1(i+k-1) = y1(i+k-1) + h1(k)*x1(i);
end
end
m1 = 0:Nyct-1;
figure(2);
subplot(2,1,1);
plot(m1,y1,'linewidth',3,'color','m')
grid;
a = title('Output of an LTI System y(n)');
set(a,'fontsize',14);
a = ylabel('y(n)');
set(a,'Fontsize',14);
a = xlabel('n');
set(a,'Fontsize',14);
% Using matlab built in function (you get the same results)
subplot(2,1,2);
plot(m1,y_auto,'linewidth',3,'color','r')
grid;
a = title('Output y(n) using conv(x,h)');
set(a,'fontsize',14);
a = ylabel('y(n)');
set(a,'Fontsize',14);
a = xlabel('n ');
set(a,'Fontsize',14);