finished exercise except for some parts of number 1
This commit is contained in:
39
Assignment2_211/assignment2_3.m
Normal file
39
Assignment2_211/assignment2_3.m
Normal file
@@ -0,0 +1,39 @@
|
||||
clear
|
||||
close all
|
||||
clc
|
||||
|
||||
|
||||
% Convolution function
|
||||
function [ Y ] = convolution( h, x )
|
||||
Ny = length(x)+length(h)-1
|
||||
Y = zeros(1, Ny);
|
||||
for n = 1:Ny
|
||||
for i = 1:length(h)
|
||||
if (n-i+1 >= 1) && (n-i+1 <= length(x))
|
||||
Y(n) = Y(n) + h(i) * x(n-i+1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
n = 0:49
|
||||
hn = [0.25, 0.5, 0.25]
|
||||
xn = cos(((2*pi)/20)*n)
|
||||
|
||||
% Perform convolution of xn and hn
|
||||
Y = convolution(hn, xn);
|
||||
Y2 = conv(xn, hn)
|
||||
|
||||
% Plot the results
|
||||
plot(Y);
|
||||
xlabel('Sample Index');
|
||||
ylabel('Amplitude');
|
||||
title('Convolution Result');
|
||||
grid on;
|
||||
|
||||
% Plot Y2 on same graph
|
||||
hold on;
|
||||
plot(Y2, '--');
|
||||
legend('Custom Convolution', 'MATLAB conv Function');
|
||||
hold off;
|
||||
|
||||
Reference in New Issue
Block a user