LAB 2: Discrete Time Signals & Linear System Objectives
LAB 2: Discrete Time Signals & Linear System Objectives
Student’s Information:
1. Name:
2. Student ID:
3. Email ID:
LAB 2:
Discrete Time Signals & Linear System
Objectives
The main objectives of this lab are:
PART 1
Introduction
Depending on the nature of independent variables, signals are broadly classified into con-
tinuous and discrete signals. A continuous signals will be denoted by x(t) in which the
independent variable t can take any value. A discrete signal will be denoted by x[n] in which
the independent variable n can take only integer values. First we’ll discuss different types of
DT signals, some elementary operations on the signals and finally we’ll discuss the idea of
a linear and non-linear system. The DT signals discussed here very important because they
can be used to construct or represent more complex signals.
1
Some Elementary DT Signals
Unit Impulse Sequence
It is also called unit sample sequence or Kronecker Delta function and is denoted by δ[n]. It
is defined by the following way:
1, n = 0
δ[n] = (1)
0, n 6= 0
More generally,
1, n = n0
δ[n − n0 ] = (2)
6 n0
0, n =
Sinusoidal Sequence
DT sinsoidal sequence is represented by: x[n] = A sin(nω + φ) where ω is the angular
frequency and φ is the phase. Unlike continuous sinusoid signals, DT sinusoids may or may
not be periodic depending on the value of ω. For a DT sinusoid to be periodic, ω must be a
rational multiple of 2π i.e. ω = k × 2π where, k is a rational number.
Time Shifting
Let us consider a discrete-time signal x[n]. If we replace the independent variable n by n-k
(k is an integer), this results in a shift of the signal in time. If k > 0, the time shift result in
a delay of the signal by k units in time. On the other hand, if k < 0, the time shift results
in an advance of the signal by |k| units in time.
2
Time Reversal
If we replace the independent signal n by -n, the transformation results in the folding or
reflection of the signal about the time origin n = 0. This is called Time Reversal operation.
Time reversal and time shifting operations are required while computing convolution of two
signals.
Time Scaling
A third modification of the independent variable is the replacement of n by nk where k is an
integer. This is termed as Time Scaling. Time scaling of a discrete signal is related to the
rate of sampling. If k > 1, this modification is downsampling or decimation and if 0 < k < 1,
this is called upsampling or interpolation. We’ll learn about downsampling and upsampling
in detail in Lab 4.
3
(a) Even Signal
10
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
10
-10
-20
-10 -8 -6 -4 -2 0 2 4 6 8 10
Discrete time systems can be subdivided into linear and non-linear system. A linear
system is one that satisfies the additive and homogeneity property. Additivity property
implies that the sum of the input signals will be transformed into the sum of the individual
4
output signals. For example, suppose y1 [n] and y2 [n] are the corresponding output signals
for inputs x1 [n] and x2 [n] respectively. Now if the input is the sum of x1 [n] and x2 [n] i.e.
x[n] = x1 [n] + x2 [n] the output will be the sum of y1 [n] and y2 [n]: y[n] = y1 [n] + y2 [n]. And
homogeneity means if the input is scaled by a factor, the output will be scaled by the same
factor. For example, if y[n] is the output for an input sequence x[n], then ax[n] will give the
output ay[n]. If a system satisfies these two properties, it is called Linear System otherwise
it is known as Non-Linear System. The two properties are, sometimes, combined into one:
superposition principle. So if a signal satisfies the superposition principle, it is linear
otherwise, it is non-linear. For example, system described by y[n] = 10x[n] is linear but
y[n] = x2 [n] is non-linear.
Linear Systems are important and useful in digital signal processing. Because linear systems
are easier to analyze and many non-linear systems are approximately linear to the first order.
PRELAB TASKS
• Sketch qualitatively the following DT signals in a paper: i) δ[n + 2] + 2 ∗ u[n −
1] + 3 ∗ u[n − 4].
√ π π
• Find the period of the following DT signals: i) sin( 3πn), ii) sin( n)+cos( n).
10 5
• If x[n] = {2, 1, 3, 4}, sketch x[-n+3].
↑
5
Part 2
Generating Signals in MATLAB/OCTAVE
Instructions
• Organizing files and folder properly for later use.
• For every week make a subfolder to keep the all the tasks in a specific week
separated from other tasks.
• Always try to work in the folder specifically created for the current week.
In this part of our lab, we’ll show how to generate the discussed Discrete time signals and
how to implement various signal operations i.e. time shifting, time reversal, addition and
multiplication of signals using MATLAB/OCTAVE. Finally, we’ll see the definitive property
of linear and non-linear system using some examples.
In this week’s MATLAB/OCTAVE code, we’ll write some functions so that we can use them
later. The basic syntax of any function is:
Listing 1: Syntax for writing a Function in MATLAB/OCTAVE
1 function [output vectors] = name_of_the_function(input vectors)
2
3 %Write the code what you want to do with the function
4
5 end
6
Remember: the function name must not conflict with any built in function’s name and save
the function with the name written in the .m file. For example, here we’ve written a function
to generate δ[n − n0 ] named unit_impulse. The inputs to this function are the starting (n1 )
and end point(n2 ) of the index/time variable and the location of the impulse (n0 ).
Listing 2: Unit Impulse Sequence
1 function [x,n] = unit_impulse(n0, n1, n2)
2
3 % Generates x[n] = delta(n−n0)
4 % n1<=n<=n2
5 % n0 is the origin of the unit impulse function
6 % We'll use logical index concept here to compute
7 % unit impulse
8
9 n = n1:n2; % defining the index vector n
10
11 x = (n==n0); % here logical indexing is used i.e. x = 1 if n==n0.
12
13 end
Later call this function with appropriate inputs and outputs in your main code like the
following code snippet. Here the unit_impulse function has been called to generate δ[n−2]
and plot them for −5 ≤ n ≤ 5.
Listing 3: Examples of generating unit impulse sequence
1 clc;
2 clear all;
3 close all;
4
5 [x,n] = unit_impulse(2,−5,5); %calling unit_step function
6 stem(n,x); %displaying the output
Similarly, unit step function u[n] can be generated as shown in the following code snippet:
Listing 4: Unit Step Function
1 function [u, n] = unit_step(n0, n1, n2)
2
3 % n0 = the origin
4 % n1 = starting index number
5 % n2 = ending index number
6
7 n = n1:n2; %defining the index vector
8
9 u = (n>=n0); % u[n−n0] = 1 if n>=n0. This implemented here using
10 ... logical indexing
11 end
7
Listing 5: Example Code to generate unit step sequence
1 clc;
2 clear all;
3 close all;
4
5
6 [x,n] = unit_step(2,−5,7); %calling unit_step function
7 stem(n,x); %displaying the output
The following code has been written to generate a general sinusoidal with zero damping. The
expression for this function is:
where A is the amplitude, a is the damping constant, ω0 is the angular frequency and φ is
the phase. In the following code, A = 10, a = 0, ω0 = π5 and φ = 0. It has been plotted for
−10 ≤ n ≤ 10.
Listing 6: Generating Sinusoidal Sequence
1 clc;
2 clear all;
3 close all;
4
5 %In this code we'll show a sinusoidal function
6 % of the form x[n] = A*exp(a*n)*sin(w0*n+phi)
7 n = −10:10; %defining the index variable
8 w0 = pi/5; %angular frequency of the sinusoid
9 a = 0; %damping factor
10 phi = 0; %phase in radian
11 A = 10; %amplitude
12 x = A*exp(a*n).*sin(w0*n+phi); %Look at the elementwise operation!!
13
14 stem(n,x); %plotting the output
Now we’ll write two functions to show time shifting and time reversal operation. In time
reversal operation, a built in function is used: fliplr(). You can know more about this
function by going through the documentation of this function.
Listing 7: sigshift Function to implement time shifting
1 function [y,n] = sigshift(x,n1,n0)
2 %We'll generate y[n] = x[n−n0]
3
4 n = n1+n0; %new index
5 y = x; % Here only the index will change, not the actual signal's value
6
7 end
8
Listing 8: sigfold Function to implement time reversal
1 function [y,n] = sigfold(x, m)
2
3 % This code will generate y[n] = x[−n]
4 % The folding operation
5 % Use fliplr function which flips the elements of an array left to right
6 n = −fliplr(m); % generating n = −m index
7 y = fliplr(x);
8 end
You can call these functions to your main code to implement x[n − 4] or x[−n] given function
x[n].
Now we’ll add two signals using MATLAB/OCTAVE. Remember from part 1, while adding,
the length of the signals must be the same. In order to make the length of the signals same,
required additional zeros can be inserted before or after a signal. For example: in case of
x1 = {1, 2, 3} x2 [n] = {3, 4, 5, 6}, a zero can be inserted at the beginning of x1 so that
↑ ↑
its length and indices get matched to those of x2 [n]. This is called zero padding. In the
following code snippet, at first this zero padding is done. Then element wise addition has
been carried out.
Listing 9: sigadd function to add two signals
1 function [y,n] = sigadd(n1, x1, n2, x2)
2
3 % In order to add two signals, we need to
4 % make their length equal by padding zeros otherwise dimension mismatch
5 % will occur
6 %n1 = index vector of signal x1
7 %n2 = index vector of signal x2
8 %x1 = signal 1
9 %x2 = signal 2
10
11 n = min(min(n1),min(n2)):max(max(n1),max(max(n2))); % Defining the index
12 %vector for the output signal. For example if n1 = −3:2 and n2 = 0:4
13 % then n will run from −3 to 4. So we need to define 'n' from the minimum
14 % of individual minimum of n1 and n2. The same goes for the maximum.
15 y1 = zeros(1, length(n)); %initializing two vectors to store
16 y2 = y1; ... the zero padded signals
17 y1 (((n>=min(n1)) & (n<=max(n1)))) = x1; % The idea is that if min(n1)<=n<=
max(n1)
18 y2 (((n>=min(n2)) & (n<=max(n2)))) = x2; ... then y1 = x1(y2=x2).
19 y = y1+y2;
20
21 end
9
Listing 10: Code to add two signals using sigadd
1 clc;
2 clear all;
3 close all;
4
5
6 n1 = −2:2;
7 n2 = −3:1;
8
9 x1 = n1; %x1 = 2n1
10 x2 = n2; %x2 = square(n2)
11
12 [y,n] = sigadd(n1,x1,n2,x2); %calling sigadd function to add two signals
13 %you can plot the output signals to visualize them
In this exact process following addition, signal multiplication can also be done. Using sigadd
and sigfold functions, one can decompose any signal x[n] into even and odd components.
First generate x[−n] and then use the formula for even and odd component given in part 1.
Finally we’ll demonstrate the definitive property of linear and non-linear system. We’ll show
that if the superposition of two signals are given input to a linear system, the output will
also be the superposition of the individual outputs and for non-linear system this will not be
the case.
For this reason, we’re defining two systems: system1 and system2. system1 is defined by
the equation: y[n] = 0.5x[n] and system2 is defined by: y[n] = x2 [n] + 5x. Then we’ll take
two signals x1 [n] = n + 2 and x2 [n] = n for 0 ≤ n ≤ 9. We’ll give the following inputs to
system1 and system2: 2x1 [n], 3x2 [n] and 2x1 [n] + 3x2 [n]. Then we show the output curves
for each of these signals. The code for doing this task is given below. In the following codes,
it has been assumed that the input signals are of the same length to avoid complexity.
Listing 11: Defining system1
1 function [y,n] = system1(x,n)
2
3 y = 0.5*x;
4
5 end
10
Listing 13: Output results for system1
1 clc;
2 clear all;
3 close all;
4
5 %Linear System & Non Linear system demonstration
6
7 n = 0:9; %index vector
8 x1 = n+2; %input signal 1
9 x2 = n; %input signal 2
10 %output of system1 & system2 defined by functions system1 and system2
11 % we're assuming x1 and x2 are of the same length for avoiding complexity
12 [y1, n1] = system1(2*x1, n);
13 [y2,n2] = system1(3*x2,n);
14 [y3,n3] = system1(2*x1+3*x2,n);
15
16 subplot(211)
17 stem(n1, y1+y2);
18 subplot(212)
19 stem(n3, y3);
20
10
0
0 1 2 3 4 5 6 7 8 9
20
10
0
0 1 2 3 4 5 6 7 8 9
Observing the plot figure(2.3), it is seen that the sum of the individual output is equal to
the output corresponding to the superposed inputs. So system 1 is a linear system.
11
Post Lab Tasks
• Problem 1: Modify the function sigadd renaming sigmult to multiply two
signals.
• Problem 2: Using function sigfold and sigadd, write a code that will decom-
pose any real signal into its even and odd counterparts. Plot the component.
• Problem 3: Verify that system2 is non-linear by writing a code just like the
linear one.
• Problem 4: If x[n] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}, using the functions sigfold
↑
and sigshift show i) x[-n-3] and ii) x[-n+4].
• Problem 5: Are time shifting and time reversal operations commutative? Verify
this by using problem 4.
12