-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolution.m
executable file
·71 lines (61 loc) · 1.36 KB
/
convolution.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
% Program to convolute x[n] with h[n]
% where x[n] = u(n)-u(n-4)
% h[n] = n*u(n) - 2*(n-4)*u(n-4) + (n-8)*u(n-8)
clc; clear all;
% u(n) UNIT Step Function
n = -5:0.01:15;
u = zeros(size(n));
u (n > 0) = 1;
figure('Name','Convolution','NumberTitle','off','Color','w')
subplot(3,3,1)
plot(n,u,'b'),grid on, grid minor
title('u(n)')
xlabel('Time')
ylabel('Amplitude')
% u(n-4) Unit Step function delayed 4 sec.
u4 = zeros(size(n));
u4 (n > 4) = 1;
subplot(3,3,2)
plot(n,u4,'r'),grid on, grid minor
title('u(n-4)')
xlabel('Time')
ylabel('Amplitude')
% x(n) = u(n) - u(n-4)
xn = u - u4;
subplot(3,3,4)
plot(n,xn),grid on, grid minor
title('x[n]')
xlabel('Time')
ylabel('Amplitude')
% u(n-8) Unit Step function delayed 8 sec.
u8 = zeros(size(n));
u8 (n > 8) = 1;
subplot(3,3,3)
plot(n,u8,'r'),grid on, grid minor
title('u(n-8)')
xlabel('Time')
ylabel('Amplitude')
% h(n) = n*u(n) - 2*(n-4)*u(n-4) + (n-8)*u(n-8)
hn = n.*u - 2*(n-4).*u4 + (n-8).*u8;
subplot(3,3,5)
plot(n,hn),grid on, grid minor
title('h[n]')
xlabel('Time')
ylabel('Amplitude')
p = length(xn);
q = length(hn);
k = p+q-1;
N = linspace(-5,15,k);
conv = zeros(1,k);
for i=1:k
for j=1:p
if (i-j+1 < q && i-j+1 > 0)
conv(i) = conv(i) + xn(j)*hn(i-j+1);
end
end
end
subplot(3,3,7)
plot(N,conv,'b'),grid on, grid minor
title('x[n]*h[n]')
xlabel('Time')
ylabel('Amplitude')