Verification of Commutative, Distributive and Associative Property of Linear Convolution in MATLAB

 MATLAB Program to verify the linear convolution between two discrete sequences and also verify the Commutative, Distributive and Associative properties of linear convolution where, x1[n] = {1,0,1,0}; x2[n] = {1,2,1,2} & x3[n] = {1,-1,1,-1} (underline indicates the value at the zero index).


Copy and paste the code in the Editor to run the code.

Program Code:

clc;

clear all;

close all;


n1 = input("\n enter the time index of x1: ");

x1 = input(" enter the dicrete sequence x1: ");

n2 = input("\n enter the time index of x2: ");

x2 = input(" enter the dicrete sequence x2: ");

n3 = input("\n enter the time index of x3: ");

x3 = input(" enter the dicrete sequence x3: ");


% Plot of three discrete sequences

subplot(3,3,1);

stem(n1,x1);

xlabel('time index');

ylabel('amplitude');

title('discrete sequence of x1');

subplot(3,3,4);

stem(n2,x2);

xlabel('time index');

ylabel('amplitude');

title('discrete sequence of x2');

subplot(3,3,7);

stem(n3,x3);

xlabel('time index');

ylabel('amplitude');

title('discrete sequence of x3');


% Linear Convolution

n = [min(n1)+min(n2):max(n1)+max(n2)];

y1 = conv(x1,x2)

subplot(3,3,2);

stem(n,y1);

xlabel('time index');

ylabel('amplitude');

title('linear convolution of x1 & x2 (x1*x2)');


% Commutative Law

y2 = conv(x2,x1)

subplot(3,3,3);

stem(n,y2);

xlabel('time index');

ylabel('amplitude');

title('linear convolution of x2 & x1 (x2*x1)');


% Associative Law

m = [min(n)+min(n3):max(n)+max(n3)];

y3 = conv(x2,x3);

z1 = conv(y1,x3)

subplot(3,3,5);

stem(m,z1);

xlabel('time index');

ylabel('amplitude');

title('convolution of x1,x2&x3 [x1*(x2*x3)]');

z2 = conv(x1,y3)

subplot(3,3,6);

stem(m,z2);

xlabel('time index');

ylabel('amplitude');

title('convolution of x1,x2&x3 [(x1*x2)*x3]');


% Distributive Law

p = [min(min(n1,n3)):max(max(n1,n3))];

p1 = [x1,zeros(1,(length(p)-length(x1)))];

p2 = [zeros(1,(length(p)-length(x3))),x3];

p3 = p1 + p2;

q = [min(p)+min(n2):max(p)+max(n2)];

z3 = conv(x2,p3)

subplot(3,3,8);

stem(q,z3);

xlabel('time index');

ylabel('amplitude');

title('discrete sequence of [x2*(x1+x3)]');

r = [min(n2)+min(n3):max(n2)+max(n3)];

s = [min(min(n,r)):max(max(n,r))];

s1 = [y2,zeros(1,(length(s)-length(y2)))];

s2 = [zeros(1,(length(s)-length(y3))),y3];

z4 = s1 + s2

subplot(3,3,9);

stem(s,z4);

xlabel('time index');

ylabel('amplitude');

title('discrete sequence of [(x2*x1)+(x2*x3)]');


Output:


For any code related queries comment below. Thanks for reading and happy learning!

Oh, by the way, If you like reading tech related articles, visit Modern Technology: Boon or Bane? or if you want to read some 'ambiguous' blogs then visit "Out of the Box" ;) Hope you'll like it.

Comments