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;