S_S.Lab.4
S_S.Lab.4
OBJECTIVE:
LEARNING OUTCOME:
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
x ( n ) • x ( n ) = x ( n ) x ( n )
1 2 1 2
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
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:
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 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
• 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,
x= [1 2 3] and s1= [0 0 0 0 0]
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: