electroSome

Signal Operations in MATLAB

MATLAB Logo

MATLAB Logo

Contents

Here I’m going to discuss about basic signal operations that can be done in MATLAB. Visit our tutorial guide on MATLAB to get familar with the basic concepts.

This tutorial includes :

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Shifting a Signal
  5. Reversing a Signal
  6. Linear Convolution of 2 signals

 Addition

Addition can be carried out  using the ‘ + ‘ symbol and plotting will give you the result.

Eg :

x=[1 2 3 4];
subplot(3,1,1);
stem(x);
title('X');
y=[1 1 1 1];
subplot(3,1,2);
stem(y);
title('Y');
z=x+y;
subplot(3,1,3);
stem(z);
title('Z=X+Y');

OUTPUT :

Plot of Signal Addition With Same time Index

NOTE:

Step 1 : After the 2 signals are defined find the duration of output signal using min & max functions

Step 2: Initialize the signals with the duration found, else  mismatch in length of the input signals error will be shown

  1. For making the duration of input signal equivalent to that of output we have to generate two signals s1,s2
  2. s1 and s2 are generated as a zero matrix using the zeros function having a length, equivalent to the duration found using min and max function earlier in Step 1
  3. Now the next step is filling in the input elements of x and y in appropriate position of s1 and s2. For it we have to find the indices corresponding to fill out.The  logical statement is as follows. Here the elements satisfying the condition will be assigned 1
    (( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1

and using  find function we can find the index values of the position which are 1 now and assigning the variables of input signal there,

Say for eg :

x=[1 2 3] and s1=[0 0 0 0 0]

We need s(1 2 3) =x ; ie we have to fill the first 3 position of s1 as x elements. similarly with the 2nd signal. In the above condition we are making the elements of s1 equal to 1 .where the elements of x has to be filled in so the output will be an array like this s1= [ 1 1 1 0 0]. Now using find () function calculating the indices of the position whose elements are equal to 1.

so that the s(1 2 3)= x ;  now the elements of x will be filled in the respective position of s1.Same with the other signal

The complete statement is

s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
 % signal x with the duration of  output signal 
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y; 
% signal y with the duration of  output signal

Eg:

n1=-2:1;
x=[1 2 3 4];
subplot(3,1,1);
stem(n1,x);
title('X') ;
axis([-3 3 0 5]);
n2=0:3;
y=[1 1 1 1];
subplot(3,1,2);
stem(n2,y);
title('Y');
axis([-3 3 0 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) );  %  finding the duration of output signal
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x; 
% signal x with the duration of  output signal add
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
 % signal y with the duration of  output signal add
add=s1 +s2; % addition
subplot(3,1,3)
stem(n3,add)
title('Z=X+Y');
axis([-3 3 0 5]);

OUTPUT:

Plot of Signal Addition with Different Time Indices

Functions Used :

1. min() and max() : used to find minimum and maximum value.

 Syntax:

  1. min(x)     -returns smallest element in an array if x is an array.
    -returns a row vector containing minimum element from each column if x is a matrix
  2. min(x,y)- returns an array with the same size of x and  y.Elements of corresponding indices are checked and minimum value is
    returned.x and y must be of same length.
  1. max(x)      -returns largest element in an array if  x is an array.
    -returns a row vector containing maximum element from each column if x is a matrix
  2. max(x,y)

-returns  an array with the same size of x and  y.Elements of corresponding indices are checked and maximum value is
returned.x and y must be of same length.

2. zeros() : returns a zero matrix

Synatx:

  1. zeros( n )              –  returns a n x n matrix of zeros.
  2. zeros(m,n)           – returns a m x n matrix of zeros.
  3. zeros(m,n,p,…)  – returns a m x n x p … array f zeros

3. find() : returns the indices of non zero elements.

Syntax:

  1. find(x) – returns the liner indices of non zero elements in an array x
    eg : if x= [ 0 4 0 5 6]
    find(x);
    output  :  2  4  5
  2. find(x,n)- returns atmost first n indices of non zero element in an array

A relational operator can also be implemented in find().
For eg : find( x>10) – wil return the indices of element which are greater than 10

4. axis() : used to change the attributes of the axes

Syntax:

  1. axis([xmin xmax ymin ymax])- set the limits for  x and y axes

5.stem(): Used for discrete time ploting of signals

For more help in ploting and other functions in MATLAB visit our tutorials.

Subtraction

With the same program code as of addition replacing arithmetic operator ‘ – ‘ we can perform subtraction in signals.

n1=-2:1;
x=[1 2 3 4];
subplot(3,1,1);
stem(n1,x);
title('X') ;
axis([-4 4 -5 5]);
n2=0:3;
y=[1 1 1 1];
subplot(3,1,2);
stem(n2,y);
title('Y');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output signal
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x; 
% signal x with the duration of output signal 'sub'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y; 
% signal y with the duration of output signal 'sub'
sub=s1 - s2; % subtraction
subplot(3,1,3)
stem(n3,sub)
title('Z=X-Y');
axis([-4 4 -5 5]);

OUTPUT :

Plot of Subtraction of Signals

NOTE :

Multiplication

By using ‘ * ‘  ( asterisk) operator we can perform multiplication of signals.

Eg:

n1=-2:1;
x=[1 2 3 4];
subplot(3,1,1);
stem(n1,x);
title('X') ;
axis([-4 4 -5 5]);
n2=0:3;
y=[1 1 1 1];
subplot(3,1,2);
stem(n2,y);
title('Y');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output signal (out)
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x; 
% signal x with the duration of output signal 'mul'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y; 
% signal y with the duration of output signal 'mul'
mul=s1 .* s2; % multiplication
subplot(3,1,3)
stem(n3,mul)
title('Z=X*Y');
axis([-4 4 -5 5]);

OUTPUT :

Plot of Multiplication of Signals

Shifting a Signal

MATLAB can be used to perform shifting of signals. A signal can be delayed as well as advanced.

The following is a program to delay or advance a signal x(n). The shift value is decided at the run time.

Eg:

n1=input('Enter the amount to be delayed');
n2=input('Enter the amount to be advanced');
n=-2:2;
x=[-2 3 0 1 5];
subplot(3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');

OUTPUT :

Plot of Shifted Signal n1=2 ; n2=3
Delayed by 2
Advanced by 3

Reversing a Signal

The inbuilt function fliplr() function can be used to perform reversing or folding a signal.

Syntax:

Basic idea :

Eg:

n=-1:2;
x=[3 -1 0 -4];
subplot(2,1,1)
stem(n,x);
axis([-3 3 -5 5]);
title('Signal x(n)');
c=fliplr(x);
y=fliplr(-n);
subplot(2,1,2);
stem(y,c);
axis([-3 3 -5 5]);
title('Reversed Signal x(-n)') ;

OUTPUT :

Plot of a Reversed Signal

Linear Convolution of Signals

Linear convolution between signals can be easily performed in MATLAB using conv() function. I hope you are familiar with the linear convolution of 2 signals.

Syntax:

  1. conv (a,b)- Convolves the vectors a and b.

 Eg:

p=input('Enter the limit for x');
q=input('Enter the limit for y');
x=input('Enter the elements for x');
y=input('Enter the elements for y');
n1=0:p ;
n2=0:q;
subplot(3,1,1);
stem(n1,x);
title('Signal - x(n)');
subplot(3,1,2);
stem(n2,y);
title('Signal - h(n)');
z=conv(x,y);
t=length(n1)+length(n2)-1;
s=0:t-1;
subplot(3,1,3);
stem(s,z);
title('Output - y(n)');

OUTPUT:

Plot of Convolution of 2 Signals
p=3 ; q=2

NOTE :

SYNTAX:

length(a)- returns the size of the array a.

Exit mobile version