You are on page 1of 12

Signals and Systems

(EE-223)

LABORATORY MANUAL

SIGNAL GENERATION AND OPERATIONS IN DISCRETE TIME SYSTEM

(LAB # 05)
Engr. Fakhar Abbas

Engr. Nimra Fatima


Student Name: ______________________________________________

Roll No: ________________ Section: ____

Date performed: _____________, 2016

MARKS AWARDED: ________ / 10


____________________________________________________________________________________________________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD

Prepared by: Engr. Aneela Sabir Version: 1.1


Last Edited by:
Verified by: Updated: Spring 2016
Signal Generation and Operations In Discrete Time System LAB 5

Lab # 05: SIGNAL GENERATION AND OPERATIONS IN DISCRETE TIME


SYSTEM
Objective: In this lab, you will learn:

 Signals Generation in Discrete Time


 Unit Impulse Function
 Unit Sequence Function
 Signum Function
 Unit Ramp Function
 Impulse Train
 Signal Properties
 Time Reversal
 Time Shifting
 Time Scaling
 Amplitude Scaling
 Differencing and Accumulating the Signals
 Energy and Power Calculations for a Signal

Tools Used: MATLAB

Signals Generation in Discrete Time:


Unit Impulse Function

MATLAB Code Output


function y = impD(n)
y = double(n == 0); For impD(-5:0.5:5), the graph is given below:
%convert binary to double
y((round(n) ~= n)) = NaN
%remove non integer index
stem(n,y,'filled','r')
grid on
title('Unit Impulse
Function')

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 2 of 11


Signal Generation and Operations In Discrete Time System LAB 5

Unit Sequence Function:

MATLAB Code Output


function y = usD(n)
y = double(n >= 0);
y((round(n) ~= n)) = NaN ;
For usD(-10:0.5:10), the graph is given below:
stem(n,y,'filled','r')
grid on
title('Unit Sequence Function')

Signum Function:

MATLAB Code Output


function y = signD(n) For signD(-10:0.5:10), the graph is given below:
y = sign(n); %Use the MATLAB sign
function
y(round(n) ~= n) = NaN;
stem(n,y,'filled','r')
title('Signum Function')
grid on

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 3 of 11


Signal Generation and Operations In Discrete Time System LAB 5

The Unit Ramp Function:

MATLAB Code Output


function y = rampD(n) For rampD(-5:0.5:15), the graph is given below:
y =n.*(sign(n) + 1)/2;
y(round(n) ~= n) = NaN; %Set
those return values to NaN
stem(n,y,'filled','r')
title('Signum Function')
grid on

Impulse Train or Unit Periodic Impulse Function:

MATLAB Code Output


function y = impND(N,n) For impND(3,-10:10), the graph is given below:
if (N == round(N) & N > 0)
y = double(n/N ==
round(n/N));
y(round(n) ~= n)=NaN; %
Index non-integer values of n

else
y = NaN*n; % Return a
vector of NaN’s
disp('In impND, the period
parameter N is not a positive
integer');
end
stem(n,y,'filled','r')

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 4 of 11


Signal Generation and Operations In Discrete Time System LAB 5

Signal Operations:
1) Time Reversal
Flipping the signal about the y axis.
function y = g(n)
y = sin(3*pi*n/10).*(n>=0)+3.*((n>=5)&(n<7));
y((round(n) ~= n)) = NaN;

Signal Flipping for the above g(n) function.


n = 0:0.1:10;
y2= g(n);
subplot(211)
stem(n,y2,'r','filled')
grid on
axis([ -10 10 -1 10])
title('Original Signal')
subplot(212)
stem(-(n),y2,'r','filled')
grid on
axis([ -10 10 -1 10])
title('Flipped Signal')

2) Time Shifting
Delaying or advancing a signal.
function y = g(n)
y = sin(3*pi*n/10).*usD(n);
y((round(n) ~= n)) = NaN;
Time shifting for g(n):
n=-5:5;
n0 = input('Enter a number to shift the signal:\n')
x=g(n);
subplot(2,1,1);
stem(n,x,'r','filled');

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 5 of 11


Signal Generation and Operations In Discrete Time System LAB 5

title('Signal g(n)');
axis([-10 10 0 3])
grid on
shifted_axis=n-n0 ;
y=x;
subplot(2,1,2);
stem(shifted_axis,y,'r','filled');
title('Delayed signal with a factor of n0');
axis([-10 10 0 3])
grid on

3) Time Scaling
Compressing or Expanding a signal.
function y = g(n)
y = sin(3*pi*n/10).*usD(n);
y((round(n) ~= n)) = NaN;

Time scaling for the above g(n) function:


n=-5:0.01:20
g0 = g(n);
g1 = g(2*n);
g2 = g(n/3) ;
subplot(3,1,1) ;

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 6 of 11


Signal Generation and Operations In Discrete Time System LAB 5

stem(n,g0,'r','filled');
ylabel('g[n]');
subplot(3,1,2);
stem(n,g1,'r','filled');
ylabel('g[2n]');
subplot(3,1,3);
stem(n,g2,'r','filled') ;
xlabel('Discrete time, n');
ylabel('g[n/3]');

4) Amplitude Scaling
Increasing the amplitude of the signal, multiplication with a scalar.
function y = g(n)
y = sin(3*pi*n/10).*usD(n);
y((round(n) ~= n)) = NaN;

Amplitude Scaling:
G1 = 2*g(n)
G2 = g(n) / 5

Differencing and Accumulating the Signals:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 7 of 11


Signal Generation and Operations In Discrete Time System LAB 5

MATLAB can compute differences of discrete-time functions using the diff


function. The diff function accepts a vector of length N as its argument and returns
a vector of forward differences of length N − 1. MATLAB can also compute the
accumulation of a function using the cumsum (cumulative summation) function. The
cumsum function accepts a vector as its argument and returns a vector of equal length
that is the accumulation of the elements in the argument vector.
For example:

>> A=1:10
A=
1 2 3 4 5 6 7 8 9 10
>> B = diff(A)
B=
1 1 1 1 1 1 1 1 1
>> C = cumsum(A)
C=
1 3 6 10 15 21 28 36 45 55

Signals Energy and Power:

For Periodic Signals:

Book Example:
Using MATLAB find the signal energy or power of the signals:

Code:
%Program to compute the signal energy and power of a signal
n = -100:100 ; %Set up a vector of discrete times at which to compute the value %of
the function
% Compute the value of the function and its square
x = (0.9).^abs(n).*sin(2*pi*n/4) ;
xsq = x.^2 ;
Energy = sum(xsq) %Use the sum function in MATLAB to find the total energy and
N0 = 35; % The fundamental period is 35
Power = sum(xsq)/N0 % Use the sum function in MATLAB to find the average power % and
display the result.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 8 of 11


Signal Generation and Operations In Discrete Time System LAB 5

Output:
The output of this program is
Energy = 4.7107
Power = 0.1346

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 9 of 11


Signal Generation and Operations In Discrete Time System LAB 5

TASKS:

Q. No. 01 Create a function “impseq”, which performs following operations:


Function [x,n]=impseq(n0,n1,n2)
 Takes three parameters (n0, n1, n2) as input, where ‘n1’ and ‘n2’ are lower and upper limits of n-axis, and ‘n0’ is
the delay.
 Generates a unit-impulse sequence using above mentioned three parameters.
 There should be two output arguments [x, n] of function ‘impseq’, where ‘x’ is impulse sequence and ‘n’ is its
corresponding n-axis.
 Finally, plot unit impulse ‘x’ against vector ‘n’.
 On the main window, type “[x,n]=impseq(0,-5,5)”
o Unit Sample Sequence
 The resulting plot looks like this
Unit Sample Sequence
2

1.8

1.6

1.4

1.2
x(n)

0.8

0.6

0.4

0.2

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

Q. No. 02: Make a function to form “stepseq” function which will output unit-step sequence.
Function [x,n]=stepseq(n0,n1,n2)
 Unit Step Sequence
 We can have another elegant way to produce a step function
 Alternatively, we can use the “ones” function
 Type “stepseq[x,n]=(0,-5,5)” we get:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 10 of 11


Signal Generation and Operations In Discrete Time System LAB 5

Unit Sample Sequence


2

1.8

1.6

1.4

1.2
x(n)

0.8

0.6

0.4

0.2

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Q. No. 03: Create a function “rampseq”, which performs following operations:
Function [x,n]=rampseq(n0,n1,n2)
 Takes three parameters (n0, n1, n2) as input, where ‘n1’ and ‘n2’ are lower and upper limits of n-axis, and ‘n0’ is
the delay.
 Generates a ramp sequence using above mentioned three parameters.
 There should be two output arguments [x, n] of function ‘rampseq’, where ‘x’ is impulse sequence and ‘n’ is its
corresponding n-axis.
 Finally, plot ramp impulse ‘x’ against vector ‘n’.

Q.No. 04:
Create a function “sigseq”, which performs following operations:
Function [x,n]=sigpseq(n0,n1,n2)
 Takes three parameters (n0, n1, n2) as input, where ‘n1’ and ‘n2’ are lower and upper limits of n-axis, and ‘n0’ is
the delay.
 Generates a signum sequence using above mentioned three parameters.
 There should be two output arguments [x, n] of function ‘sigseq’, where ‘x’ is impulse sequence and ‘n’ is its
corresponding n-axis.
 Finally, plot signum sequence ‘x’ against vector ‘n’.

Q. No 05: Write a function which plot or stem a unit step signals. The function takes values for starting and ending value of
independent variable, i.e. t and n, and a character for identification of discrete and continuous signal. Finally t plot or stem the
function or signal. e.g;
function f_name ( arg1 (start) , arg2 (end) , arg3 (D/C) )

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 11 of 11


Signal Generation and Operations In Discrete Time System LAB 5

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 12 of 11

You might also like