Friday, February 8, 2013

Convolution of two signal without use of builtin conv(x,h) function in matlab

digital signal processing is very common in Matlab. Matlab has several  built in function and variables which makes the operation of signals easy and effective. Similarly  in the DSAP problem of convolution we can convolute the two signal  input signal and impulse signal by the use of builtin function conv(x,h) 
Where X= input signal
           h= impulse signal

However the main mathematics involved is hidden inside it. For it to understand properly i.e the manipulation inside the builtin function ,instead of using the function we can form our own code without using it. This own manipulation can makes us better understand the things in convolution of signals.

Convolution:
Three things are very important in convolution.
1) Mirroring of signal  i.e folding of signal w.r.t  Y-axis
2) shifting og signal
3) finally multiplying the signals and adding

these 3 things should be implemented in our code to perform the convolution of the signal  given
  The overall code is as follows:



Problems

  1. Find the convolution result of the following signal without       using basic convolution formula :
X1(n1) = [1,1,1,1,1]
n1= [-2,-1,0,1,2]

h1(n2) = [1,0,0,0,0,0,0,0,0,0]
n2 =  [-4,-3,-2,-1,0,1,2,3,4,5]
X2 is a periodic signal.
Y2= X1*X2

Solution:
                                           Sig_Mirror_Function:

% signal mirror function 

function [y,n] = Sig_Mirror_Function(x,n)
% implements y(n) = x(-n)
% -----------------------% [y,n] = sigfold(x,n)
%
y=fliplr(x);
n = -fliplr(n);
end


                   Sig_Shift_Function:



                           function [y,n] = Sig_Shift_Function(x,m,k)
                             % implements y(n) = x(n-k)
                              % -------------------------% [y,n] = sigshift(x,m,k)

                               n=m+k;
                               y = x;
                               end



                                  Sig_Mul_Function


  function [y,n] = Sig_Mul_function(x1,n1,x2,n2)
% implements y(n) = x1(n)*x2(n)
% -----------------------------% [y,n] = sigmult(x1,n1,x2,n2)
% y=product sequence over n, which includes n1 and n2
%x1=first sequence over n1
%x2=second sequence over n2 (n2 can be different from n1)
%
n=min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 = zeros(1,length(n)); y2 = y1; %
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y=y1 .* y2; % sequence multiplication
end



                            Sig_Conv_Function



function  Sig_Conv_Function(x1,n1,h1,n2)
l=1;
for k=(min(n1)+min(n2)): (max(n1)+max(n2));

[First_y,First_n]=Sig_Mirror_Function(h1,n2);
[Second_y,Second_n]=Sig_Shift_Function(First_y,First_n , k);
[Result_y,Result_n]=Sig_Mul_Function(x1,n1,Second_y,Second_n);
temp=0;

m=1;

y=0;
for i=min(Result_n):max(Result_n);
  
  y=temp+Result_y(m);
  temp=y; 
  m=m+1;
end
Final_Result(l)=y;
l=l+1;
end
Final_Result
Final_n=(min(n1)+min(n2)): (max(n1)+max(n2))
stem(Final_n,Final_Result)


end

 Finally in the matlab window command 
type

x1=[values as per question ]
n1=["]
h1=["]
n2=["]


Sig_Conv_Function(x1, n1, h1,n2)

and you will get the desired output







1 comment:

  1. Really nice post. Your post is expressing what I actually wanted to know, thanks for sharing…
    relocation services in ghaziabad.

    ReplyDelete