0% found this document useful (0 votes)
7 views

S_S.Lab.4

Uploaded by

Usama Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

S_S.Lab.4

Uploaded by

Usama Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

EXPERIMENT NO.

OBJECTIVE:

To perform basic arithmetic operations on signals using MATLAB.

LEARNING OUTCOME:

In this lab, you will be able to understand the following points:

• Summing different signals of same and different time index.


• Subtracting unit step and unit ramp function.
• Subtracting different signals of different time index.
• Multiplication of the unit step and unit ramp function.
• Multiplication different signals of different time index.
• Division of unit step and unit ramp function.
• Division of different signals of different time index.

BASIC OPERATIONS:
Signal Adding: This is a sample-by-sample addition given by
x ( n ) + x ( n ) = x ( n ) + x ( n )
1 2 1 2

and the length of x1(n) and x2(n) must be the same.

Signal Multiplication: This is a sample-by-sample multiplication (or “dot”


multiplication) given by

x ( n ) • x ( n ) = x ( n ) x ( n )
1 2 1 2

and the length of x1(n) and x2(n) must be the same.

Signal Subtraction: This is a sample-by-sample subtraction given by


{𝑥(𝑛)} − {𝑦(𝑛)} = {𝑥(𝑛) − 𝑦(𝑛)}
and the length of x(n) and y(n) must be the same.

Signal Division: This is a sample-by-sample division (or “dot” division) given by


{𝑥(𝑛)} ÷ {𝑦(𝑛)} = {𝑥(𝑛) ÷ 𝑦(𝑛)}
and the length of x1(n) and x2(n) must be the same.

Scaling: In this operation each sample is multiplied by a scalar 


 x ( n ) =  x ( n )
Shifting: In this operation each sample of x(n) is shifted by an amount k to obtain a shifted
sequence y(n).

y ( n ) = x ( n − k )

Folding: In this operation each sample of x(n) is flipped around n = 0 to obtain a folded
sequence y(n).

y ( n ) = x ( −n )

SIGNAL ADDITION:

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

Signal Addition with Same time Index:


Example 1

Code:
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:

Signal Addition with Different time Index:


In the above example index value of both the signals x and y are the same. If the index values
are different we have to find the range of output by comparing the index values of both signals.

Example 2

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

Code Interpretation:

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

o For making the duration of input signal equivalent to that of output we have to
generate two signals s1, s2

o 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

o 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

(( 𝑛3 >= 𝑚𝑖𝑛( 𝑛1 ) ) & ( 𝑛3 <= 𝑚𝑎𝑥 ( 𝑛1 ) ) == 1

• An array of 1 is created in the respective position which satisfies the condition e.g.,
[ 𝟏 𝟏 𝟏 ] 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 e.g.: [ 𝟏 𝟏 𝟏 𝟎 𝟎]

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 e.g.,

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

We need 𝑠 (1 2 3) = 𝑥; i.e. 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 have 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 𝑠(1 2 3) = 𝑥 ; now the elements of x will be filled in the respective position of
s1. Same with the other signal
Output:

Example 3:

Generate code that can multiply unit step and ramp signals of same time index.

Code:
n = -5:10;
I = [zeros(1,5) 1 ones(1,10)];
subplot(3,1,1)
stem(n,I);
title('Unit Step Function');
axis([-5 10 0 10]);
ramp= n .*(n >=0);
subplot(3,1,2)
stem(n, ramp);
axis([-5 10 0 10]);
title ('Ramp Function');
multiply = I .* ramp;
subplot(3,1,3)
stem(n, multiply);
title('Product of the above');
Output:
Task 1:
Write the syntax and function of all the commands used in above example:

1. subplot:
Syntax:
subplot(m,n,p)
Function:. This allows multiple plots to be displayed in the same figure window.
2. stem:
Syntax:
stem(x,y)
Function: it is used to plot a discrete-time signal. It displays data as a series of vertical lines
(stems) that are placed on a horizontal axis.
3. title:
Syntax:
title('text')
Function: The title function adds a title to the current axes or chart. The title can be a string of
text that describes the content of the plot or chart.
4. axis:
Syntax:
axis([-5 10 0 10]);
Function: to define the axis of the graph. It is not necessary to use because MATLAB
automatically sets the default axis according to a given function. The limits can be set for the
x-axis and y-axis separately, and can be used to zoom in or out of the plot.
5. ramp:
Syntax:
ramp=n.*(n >=0);

Function: it is used to create a slope point to point through . operator. The ramp vector can be
used to create a ramp signal, which is a type of signal used in digital signal processing
Task 2:
Generate code that can subtract two signals of same time index.

Code:
x=[1 2 3 4];
subplot(3,1,1);
stem(x);
title('A');
y=[1 1 1 1];
subplot(3,1,2);
stem(y);
title('B');
z=x-y;
subplot(3,1,3);
stem(z);
title('Z=A+B');

Output:

Task 3:
Generate code that can subtract two signals of different time index.

Code:
n1=-2:1;
x=[1 2 3 4];
subplot(3,1,1);
stem(n1,x);
title('A') ;
axis([-3 3 0 5]);
n2=0:3;
y=[1 1 1 1];
subplot(3,1,2);
stem(n2,y);
title('B');
axis([-3 3 0 5]);
n3 =min(min(n1),min(n2)) : max(max(n1),max(n2s1 =zeros(1,length (n3));
s2 =s1;
s1 (find ((n3>=min(n1)) & (n3 <=max(n1))==1 ))=x; s2 (find ((n3>=min(n2))
& (n3<=max(n2))==1))=y;
subtract=s1-s2; % subtraction
subplot(3,1,3)
stem(n3,subtract)
title('Z=A-B');
axis([-3 3 0 5]);

Output:

Task 4:
Generate code that can multiply two signals of different time index.

Code:
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)); s1 =zeros(1,length
(n3));
s2 =s1;
s1 (find ((n3>=min(n1)) & (n3 <=max(n1))==1 ))=x; s2 (find ((n3>=min(n2))
& (n3<=max(n2))==1))=y;
multiply=s1.*s2; % multiplication
subplot(3,1,3)
stem(n3,multiply)
title('Z=X*Y');
axis([-3 3 0 5]);

Output:
Task 5:

Generate code that can divide unit step and ramp signals of same time index.

Code:
n = -5:10;
I = [zeros(1,5) 1 ones(1,10)];
subplot(3,1,1)
stem(n,I);
title('Unit Step Function');
axis([-5 10 0 10]);
ramp= n ./(n >=0);
subplot(3,1,2)
stem(n, ramp);
axis([-5 10 0 10]);
title ('Ramp Function');
divide = I ./ ramp;
subplot(3,1,3)
stem(n, divide);
title('Division of the above');

Output:

Task 6:
Generate code that can divide two signals of different time index.

Code:
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));
s1 =zeros(1,length (n3));
s2 =s1;
s1 (find ((n3>=min(n1)) & (n3 <=max(n1))==1 ))=x; s2 (find ((n3>=min(n2))
& (n3<=max(n2))==1))=y; divide=s1.*s2; % division
subplot(3,1,3)
stem(n3,divide)
title('Z=X/Y');
axis([-3 3 0 5]);

Output:

DISCUSSION & CONCLUSION:


In this lab we learned and perform the basic arithmetic operation of signal. In the basic
arithmetic operation, we can add subtract, multiply, shifting, scaling and divide of two
different signals of same and different time index. A ramp function or ramp signal is a type of
standard signal which starts at 𝑡 = 0 and increases linearly with time. The unit ramp function
has unit slop.

You might also like