plot convolution of two descrete signals in matlab

To plot convolution of two discrete signals in Matlab, you can use the conv function.

main.m
% define the two discrete signals
x = [1 2 3 4];
y = [4 3 2 1];

% compute convolution of x and y using conv function
conv_xy = conv(x, y);

% plot the convolution
stem(conv_xy)
xlabel('n')
ylabel('conv(x,y)')
title('Convolution of two discrete signals')
255 chars
13 lines

This will generate a stem plot of the convolution of the two signals. The stem plot shows the values of the convolution at each sample index.

You can also specify the range of n values for which to compute the convolution by using the conv function with three input arguments:

main.m
% define range of n values
n = [0:length(x)+length(y)-2];

% compute convolution of x and y using conv function with range of n values
conv_xy = conv(x, y);
conv_xy_n = conv(x, y, 'full');

% plot the convolution with n values
stem(n, conv_xy_n)
xlabel('n')
ylabel('conv(x,y)')
title('Convolution of two discrete signals')
323 chars
13 lines

This will generate a stem plot of the convolution with values of n on the x-axis. The full option in the conv function pads the vectors with zeros so that the length of the convolution is equal to the sum of the lengths of the input vectors minus 1.

related categories

gistlibby LogSnag