Activity#3-Operations On Sequences

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Digital Signal Processing

Name:________________________________ Date Performed:______________


Activity #3
Operations on Sequences
Addition can be carried out using the ‘+‘symbol and plotting will give you the result.

Addition

Example 1:

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');

Note: In the above example index value of both the signals x and y are the same. If the indexvalues are

different we have to find the range of output by comparing the index values of both signals.

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.

• Generating a zero matrix of 1 row having elements with length = duration of output

• Making the length of input signals equal by making duration of both equal to that of output

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

1
(( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1

4. An array of 1 is created in the respective position which satisfies the condition eg: [ 1 1 1 ] and the other
position will be having a zero value as the s is zero matrix defined by us so that the altogether result will
be an array of 1’s and 0’s eg: [ 1 1 1 0 0]
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 example:
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

Example 2:

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) );

2
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]);

Functions Used :
1. min() and max() : used to find minimum and maximum value.
Syntax:
• min()
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.
• max()
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]

3
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([xminxmaxyminymax])- set the limits for x and y axes
5.stem(): Used for discrete time ploting of signals

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

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)

4
stem(n3,sub)
title('Z=X-Y');
axis([-4 4 -5 5]);

NOTE :
• Ensure that axis is specified correctly to show the negative values too.

Multiplication
By using ‘ * ‘ ( asterisk) operator we can perform multiplication of signals.
Example 4:

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]);

5
Shifting a Signal
MATLAB can be used to perform shifting of signals. A signal can be delayed as well as advanced.
• The basic idea is to add the shift value to indices and thereby plotting the signal.
The following is a program to delay or advance a signal x(n). The shift value is decided at the run time.
Example 5:

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)');

Reversing a Signal
The inbuilt function fliplr() function can be used to perform reversing or folding a signal.
Syntax:
• fliplr(a) : if a is row vector it returns a vector with the same size of a but with reversed order.
if a is column vector it flips the elements one column to the other.
Basic idea :
• flip the elements
• flip the indices with a ‘ – ‘ sign.
Example 6:

n=-1:2;
x=[3 -1 0 -4];
6
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)') ;

Procedure:
1. Run the Example Program 1 to 6 and capture the result.
For example #5, amount to be delayed =2 and amount to be advanced is 3.
For example #7, Enter the limit for x=2, Enter the limit for y=2, Enter the element of x=4and element of y=4
2. Discuss the purposes of the following commands;
a. subplot(3,1,1)
b. stem(x) in Example 1 where x=[1 2 3 4]
c. n1=-2:1 in Example 2
d. stem(n1,x) in Example 2
3. Write a program and sketch the result of the basic signals operations of the sequences given in the figure
below. Thatis find
a. y1[n] + y2[n]
b. y1[n] - y2[n]
c. y1[n] * y2[n]
d. y1[n] + y2[n]
e. y1[n-2]
f. y2[n+3]

You might also like