Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Convolution Function :

function [y]=conv(x,h) lx=length(x); %getting the length of x lh=length(h); %getting the length of h zx=[x,zeros(1,lh)]; %zero padding for making the length of h equal to x zh=[h,zeros(1,lx)]; %zero padding for making the length of x equal to h for i=1:lh+lx-1 %length of convolved variable y is = length of x + length of h-1 y1(i)=0 %initializing the index array of y. i.e y(1) index initial value is 0, y(3) index value initially is 0, etc for j=1:lx %loop from 1 to length of x if(i-j+1>0) %condition for cross multiplication method for convolution %i.e if x=1:3, h=1:3 , then y(4)=x(1)*h(4)+x(2)*h(3)+x(3)*h(2)+x(4)*h(1) y1(i)=y1(i)+zx(j)*zh(i-j+1); %formulae for the cross multiplication method for convolution else end end end figure stem(x,'fill'); %plotting x[n] ylabel('x[n]'); xlabel('-----> n'); figure stem(h,'fill'); %plotting h[n] ylabel('h[n]'); xlabel('-----> n'); figure stem(y1,'fill'); %plotting y[n] ylabel('y[n]=x[n]*h[n]'); xlabel('-----> n'); Command Window:
>> x=[1 2 3 4] x= 1 2 3 4

>> h=[1 2 3 4] h= 1 2 3 4

>> conv(x,h) y1 = 0 y1 = 1 y1 = 1 y1 = 4 0 0

1 y1 = 1 y1 = 1 y1 = 1

4 10

4 10 20

4 10 20 25

4 10 20 25 24

You might also like