Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Saturday, February 9, 2013

Signal addition and multiplication of two signals in MATLAB


Signal addition , signal multiplication are two major operation which we need in the manipulation of signals in DSAP
During addition and signal multiplication ,we first need to arranged the both the signals in proper order
with respect to the range of the signal.

SIGNAL ADDITION:
It is implemented in Matlab by the arithematic operation ".+".HOwever the length of the two
signals must be same. i.e x1(n) and x2(n) should have same length. Moreover
a slight modification in the code should be done if the length of two signals are not the same


function [y,n] = sigadd(x1,n1,x2,n2)
% implements y(n) = x1(n)+x2(n)
% -----------------------------
% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum 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; % initialization
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 addition


SIGNAL MULTIPLICATION

It is implemented in Matlab by the arithematic operation ".*".


function[y,n]=sigmulti(x1,n1,x2,n2);
n=min(min(n1),min(n2)):
max(max(n1),max(n2));
y1=zeros(1,length(n));
y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1.*y2;


Finally in the matlab command window give the input of two signals and their length and call the respective function to get the output.

logical operation of intersection “&”, relational operations like “<=” and “==”, and the find function are required to make x1(n) and x2(n) of equal length.So the above function is a genaralize function which results a appropriate answer for both equal aswell as non equall length signals.





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