0% found this document useful (0 votes)
27 views

MATLAB Codes For Practice

Uploaded by

sviji
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

MATLAB Codes For Practice

Uploaded by

sviji
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Product and sum of sequences:

x1=[1 2 3 4 5 6];
x2=[0 1 0 1 1 1];
n1=-3:2;
n2=-1:4;
k=min(min(n1),min(n2)):max(max(n
1),max(n2));
y1=zeros(1,length(k));
y2=y1;
y1(find((k>=min(n1))&(k<=max(n1)
)==1))=x1;
y2(find((k>=min(n2))&(k<=max(n2)
)==1))=x2;
s=y1+y2;
p=y1.*y2;
%ps=y1*y2';
figure(1);
subplot(6,1,1);
stem(n1,x1);
title('input x1');
subplot(6,1,2);
stem(n2,x2);
title('input x2');
subplot(6,1,3);
stem(k,y1);
title('output y1');
subplot(6,1,4);
stem(k,y2);
title('output y2');
subplot(6,1,5);
stem(k,s);
title('output sum');
subplot(6,1,6);
stem(k,p);
title('output product');
Shifting and reversal:

x1=[1 2 3 4 5 6];
x2=[0 1 0 1 1 1];
n1=-3:2;
n2=-1:4;
%left shift by 3 units x1(n1+3)
l=n1-3;
%right shift by 5 units x2(n2-5)
r=n2+5;
figure(1);
subplot(4,1,1);
stem(n1,x1);
title('Original sequence');
subplot(4,1,2);
stem(l,x1);
title('left shift by 3 units');
subplot(4,1,3);
stem(n2,x2);
title('original x2');
subplot(4,1,4);
stem(r,x2);
title('right shift by 5 units');
%reversal
fx1=fliplr(x1);
fn1=-fliplr(n1);
figure(2);
subplot(2,1,1);
stem(n1,x1);
title('original sequence x1');
subplot(2,1,2);
stem(fn1,fx1);
title('reversed x1 = x1(-n1)');
Linear Convolution :

%%%linear convolution
x=[6 1 2 3 4 6];
h=[1 1 1 1 1];
n1=-3:2;
n2=-1:3;
[fh,fn2]=sigfold(h,n2);
n=(min(n1)-max(fn2)):(max(n1)-
min(fn2));
z=[];
for i=min(n):max(n)
[y,k]=sigshift(fh,fn2,i);
[p]=sigmult(x,n1,y,k);
z=[z p];
end
figure(1);
subplot(3,1,1);
stem(n1,x);
title('input x(n)');
subplot(3,1,2);
stem(n2,h);
title('input h(n)');
subplot(3,1,3);
stem(n,z);
title('output y(n)=x(n)*h(n)');
Different functions:

function [y,n]=sigfold(x,m)
y=fliplr(x);
n=-fliplr(m);
end

function [p]=sigmult(x1,n1,x2,n2)
k=min(min(n1),min(n2)):max(max(n1),max(n2))
;
y1=zeros(1,length(k));
y2=y1;
y1(find((k>=min(n1))&(k<=max(n1))==1))=x1;
y2(find((k>=min(n2))&(k<=max(n2))==1))=x2;
p=y1*y2';
end

function [y,n]=sigshift(x,m,n0)
y=x;
n=m+n0;
end

You might also like