-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2_2.m
More file actions
64 lines (59 loc) · 2.22 KB
/
Copy pathlab2_2.m
File metadata and controls
64 lines (59 loc) · 2.22 KB
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
%% This script is for EE301L lab2 Q2.
% Modified from B. P. Lathi, Linear Systems and Signals, page 232, ex M2.4.
% Modified from Jacob Swanson's lab report.
%
% Destructive and constructive interference by pulse spacing.
% Wayne Weiyi Chen, 1/25/2017
% Loop for plots with 2 different pulse spacing.
for i=[1,2]
% Create figure window
figure(i)
% The pulse width must be large enough to register a y(t) value during
% constructive interference, and small enough to sufficiently
% demonstrate destructive interference.
% you can play around with this parameter.
dt = 0.01;
%% What do you observe for different value of T
% Answer this questions: when is the interference constructive, and
% when is it destructive?
T = (i/5)*pi;
x = @(t) heaviside(t)-heaviside(t-dt)...
+heaviside(t-dt-T)-heaviside(t-dt-T-dt);
h = @(t) sin(5*t).*heaviside(t);
dtau = 0.0005;
%% convolution and plot
tau = -1:dtau:4;
ti = 0;
tvec = -1:0.1:4;
% Pre-allocate memory
y = NaN*zeros(1,length(tvec));
for t = tvec
% Time index
ti = ti+1;
xh = x(t-tau).*h(tau);
lxh = length(xh);
% trapezoidal approximation of integral
y(ti) = sum(xh.*dtau);
subplot(2,1,1), plot(tau,h(tau), 'r-', tau, x(t-tau), 'b--',t,0,'ok');
axis([tau(1) tau(end) -2.0 2.5]);
% patch command is used to create the gray-shaded area of convolution
patch([tau(1:end-1); tau(1:end-1); tau(2:end); tau(2:end)],...
[zeros(1,lxh-1); xh(1:end-1); xh(2:end);
zeros(1,lxh-1)],...
[0.8 0.8 0.8], 'edgecolor','none');
xlabel('\tau');
legend('h(\tau)', 'x(t-\tau)','t','h(\tau)x(t-\tau)',3);
c = get(gca, 'children');
set(gca,'children', [c(2);c(3);c(4);c(1)]);
subplot(2,1,2), plot(tvec,y,'k',tvec(ti),y(ti),'ok');
xlabel('t');
ylabel('y(t)');
title('y(t) = \int h(\tau)x(t-\tau) d\tau');
axis([tau(1) tau(end) -0.1 0.1]);
grid;
% drawnow command updates graphics window for each loop iteration
drawnow;
% Using the pause command allows one to manually step through the
% pause;
end
end