Lab 2

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

LAB-2

Digital Signal Processing Lab


508518

Name

Registration Number

Batch & Section

Instructor’s Name
Lab # 02 Introduction to Discrete Time Signal Processing on MATLAB

Objective
This lab allows students to understand the basic concept of the discrete-time signals and their
representation on MATLAB. Furthermore, students will be able to perform signal shifting and
folding operations in MATLAB followed by arithmetic operations like adding, subtracting or
multiplying signals of different lengths.

Pre-Lab:

(Some more points related to MATLAB)


MATLAB is a high-level programming language that has been used extensively to solve
complex engineering problems.

MATLAB works with three types of windows on your computer screen. These are the
‘Command window’, the Figure window and the Editor window. The Figure window only
pops up whenever you plot something. The Editor window is used for writing and editing
MATLAB programs (called M-files) and can be invoked in Windows from the pull-down menu
after selecting File |New| M-file. In UNIX, the Editor window pops up when you type in
the command window: edit filename (‘filename’ is the name of the file you want to create).

The ‘ Command window’ is the main window in which you communicate with the
MATLAB interpreter. The MATLAB interpreter displays a command >> indicating that
i t is ready to accept commands from you.

Handling Complex Numbers in MATLAB


MATLAB also supports complex numbers. The imaginary part of the signal can be represented
with the symbol i or j , assuming that you did not use these symbols anywhere in your
program (that is very important!). Try the following:

>> z=3 + 4i % note that you do not need the ‘*’ after 4

>> conj (z) % computes the conjugate of Signal z

>> angle (z) % computes the phase of Signal z

>> real (z) % computes the real part of Signal z


>> imag (z) % computes the imaginary part of z

>> abs (z) % computes the magnitude of Signal z

You can also define the imaginary number with any other variables you like. Try the following:
>> img = sqrt (-1)

>> z = 3+4*img

>> exp(pi*img)

Array Indexing
In MATLAB, all arrays (vectors) are indexed starting with 1, i.e., y(1) is the first element of
the array y.

Note that the arrays are indexed using parenthesis (.) and not square brackets [.] as in
C/C++. To create an array having as elements the integers -5 through 5, just enter:
>> x= [-5,-4,-3,-2,-1,0,1,2,3,4,5]

Alternatively, you can use the : notation,


>> x = -5:5

The : notation above creates a vector in steps of 1. We can visualize the array ‘x’ and
its indexes by the following table:

array ‘x’ -5 -4 -3 -2 -1 0 1 2 3 4 5

indexes of ‘x’ 1 2 3 4 5 6 7 8 9 10 11

If you want to create a vector from 1 to 6 in steps of say 2, then type:


>> x = 1:2:6

ans=1 3 5

Try the following code:


>> jj= 2 : 4 : 17

>> jj=20 : -2 : 0

>> ii=2 : (1/10) : 4

Extracting or inserting numbers in a vector can be done very easily. To concatenate an


array, you can use the [ ] operator, as shown in the example below:
>> x=[1:3, 4, 6, 100:110]

Loading and saving data:


You can load or save data using the commands load and save. To save the variable x of
the above code in the file data.mat, type:

>> save data.mat x

Note that MATLAB’s data files have the extension .mat. To retrieve the data that was saved
in the vector x, type:

>> load data.mat

The vector x is loaded in memory. To see the contents of memory use the command whos:

>> whos

Name Size Bytes Class


X 1x8193 65544 double array
Grand total is 8193 elements using
array65544 bytes

The command whos gives a list of all the variables currently in memory, along with their
dimension. In our case, x contained 8193 samples.

To clear up memory after loading a file, you may type clear all when done.That is very
important, because if you do not clear all the variables in memory, you may run into problems
with other programs that you will write that use the same variables.

In-Lab:
A signal is a function that conveys some useful information about the behavior or attributes of
some phenomenon. Signals are mathematically represented as a function of one or more
independent variables i.e. y =ƒ( x1, x2 ,..,xN ) where x1, x2 , .., xN are the independent variables that
could be time or space and ‘y’ is the dependent variable that in this case represent the signal values.
In this lab, we will mostly be dealing with signals which are the function of time only and in case
of discrete signals, we will replace time with the variable ‘n’ which will only take discrete values.

1. Signals in MATLAB
Signals are divided into two major types.

1.1. Analog Signals


If either of the independent (Time) or dependent (Signal values) variables are continuous, the
signal is termed as analog. In this lab, we will mostly be dealing with discrete time signals.

MATLAB representation
Output:
In MATLAB, signals are represented by using two
variables i.e. y=sin (t) where variable ‘t’ represents the
time axis and ‘y’ represents the signal values.
>> t=0:0.01:7;

>> y=sin(t);

>> plot(y)

1.2. Digital signals


If both independent (Time) or dependent (Signal
values) variables are discrete, the signal is termed as Output:
digital. Digital signals uses discrete or discontinuous
values to represent information.

MATLAB representation:
>> n=0:7;

>> y=sin(n);

>> stem(n,y)

2. Some fundamentals sequences


2.1. Impulse Sequence

>> n=-5:5;

>> x=[n==0];

>> stem (n,x)

>> n=-5:5;

>> x=[(n-2)==0];

>> stem (n,x)

2.2. Step Sequence


>> n=-5:5;

>> x=[n >=0];

>> stem (n,x)

>> n=-5:5;

>> x=[(n-2) >=0];

>> stem (n,x)

2.3. Real-valued exponential Sequence

 The operator “.^” is required to implement a real exponential sequence. For example,
to generate x(n) = (0.9)n, 0<= n<=10, we will need the following script:

 Example tutorial:

>> n = [0:10];

>> x = (0.9).^n;

 Create a function (M-file) to generate an exponential sequence. Use the previous


examples as your guide.

2.4. Complex-valued exponential Sequence


A matlab function “exp” is used to generate exponential sequences.

For example, to generate x(n) = exp[(2+j3)n], 0<= n<=10, we will need the following
script:

>> n = [0:10];

>> x = exp((2+3j)*n);

Note that you need different plot commands for plotting real and imaginary components.

2.5. Sinusoidal sequence


A matlab function “cos” (or sin) is used to generate sinusoidal sequences.
To generate x(n) = 3cos (0.1πn + π/3) + 2sin (0.5 πn), 0<= n<=10, we will need the
following script:
>> n = [0:10];

>> x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

2.6. Random Sequences


A random or stochastic sequence is characterized by parameters of the associated
probability density functions or their statistical moments. In matlab, 2 types of (psuedo )
random sequences are available: “rand(1,N)” generates a length N random sequence whose
elements are uniformly distributed between 0 and 1. “randn(1,N) generates a length N
gaussian random sequence with mean 0 and variance 1.

2.7. Periodic sequence


A sequence is periodic if x(n) = x(n +N). To generate P periods of x(n) from one period,
we can copy x(n) P times:

>> xtilde = [x,x,x,x...,x];

An elegant approach is to use matlab’s indexing capabilites:

Generate a matrix containing P rows of x(n) values. Concatenate P rows into a long row
vector using the construct (:).

Have to use matrix transposition operator (‘) to provide the same effect on rows:
>> xtilde = x' * ones(1,P); %P columns of x; x is a row vector

>> xtilde = xtilde(:); %long coloumn vector

>> xtilde = xtilde'; %long row vector

3. Important Signal Operations in Matlab:

3.1. Signal Scaling


In this Operation each sample is multiplied by a scalar. Use the command “*” for scaling.

3.2. Signal Shifting


During a shift operation a signal changes its position in time where each sample of x(n) is
shifted by an amount k to obtain a shifted sequence y[n].

y (n)  {x(n  k )}
In Matlab, this operation is not so simple, both “x” and “n” need to be processed separately in
order to get the shifted signal x[n-k]. Perform following steps to in Matlab to perform the signal
shift.
i) Check whether k is positive or negative?
ii) If k is positive it means shift is towards right i.e. x[n-k] then do following
a. As signal is shifting towards right time axes “n” should be extended towards right
side by “k” samples i.e. if n is ending at n2 then it should now end at n2 + k.
b. Since the signal has now moved towards right meaning initial “k” samples in “x”
are empty so concatenate “k” zeroes in the beggining of the sequence x.
iii) If k is negative it means shift is towards left i.e. x[n+k] then do following
a. As signal is shifting towards left time axes “n” should be extended towards left side
by “k” samples i.e. if n is starting at n1 then it should now start at n1 - k.
b. Since the signal has now moved towards left meaning last “k” samples in “x” are
empty so concatenate “k” zeroes in the end of the sequence x.
3.3. Folding
Folding operation is also termed as flipping a signal where each sample is of x(n) is mirrored
around n=0 to obtain a folded sequence y(n) = x[-n].

In Matlab, folding operation can be performed in three steps

i) Flip the magnitude sequence “x”


ii) Flip the time index “n”
iii) Now multiply the flipped time vector “n” with a minus

Note that in Matlab a built-in function “fliplr(m) can be used to flip any sequence.

3.4. Add/Subtract/Multiply two signals


Adding a signal in Matlab is not as easy as on paper. In order to add two sequences x1 and
x2 in Matlab, both sequences have to be of same length, same is the case for subtraction,
multiplication and division.

In signal processing signal elements are added/subtracted or multiplied corresponding to


their time index i.e. a signal element at time -1 will be added to the signal element of the other
signal at the same time.

We know that signals can have different starting and ending times, here we want to modify
time indexes such that both signals start from the lowest time index and end at highest time index
and accordingly concatenate zeros or at the start or end of the signal. Suppose we want to
add/multiply two signals x1[n] specified by x1 and n1 in Matlab and and x2[n] specified by x2
and n2 in Matlab. Following two steps can be followed to make the lengths of x1 & x2 and making
n1 and n2 same as well.

a. Compare the starting points of both signals time index vectors i.e. n1(1) and n2(1).
If n1(1) is smaller than n2(1)

 Concatenate n2(1) – n1(1) number of zeros before x2


 And put n2(1) = n1(1);
Else-If n2(1) is smaller than n1(1)

 Concatenate n1(1) – n2(1) number of zeros before x1


 And put n1(1) = n2(1);
b. Compare the ending points of both signals time index vectors i.e. n1(end) and n2(end).
If n1(end) is greater than n2(end)

 Concatenate n1(end) – n2(end) number of zeros at the end of x2


 And put n2(end) = n1(end);

Else-If n2(end) is greater than n1(end)

 Concatenate n2(end) – n1(end) number of zeros at the end of x1


 And put n1(end) = n2(end);
c. Now make a universal time index n = n1(1):n2(1)
d. Add the magnitude vectors by as x = x1+x2 or multiply them as x = x1.*x2 etc.

Lab Performance Tasks


Task-1: Write a MATLAB program to process ‘running average’, a running total is a
sequence of partial sum of a given sequence/signal. For example, the running totals of the
signal {a, b, c, …}are a, a+b, a+b+c, ... Use that program to find the running total of the discrete
time signal of length N=100. Write your program so that it is flexible. That is, you should
be able to invoke your program from the command window as follows:

>> y=running_averagel(x)

where x is the input signal, and y is the running total of that signal.

Task-2: Write a program to compute and analyze the variance and mean of a signal x. The
variance σ is defined to be:
𝑁
1
𝜎 = ∑(𝑥𝑖 − 𝑥̅ )
𝑁
1=1

where ‘ 𝑥
̅’ is the mean value of the signal x. For signal x, use all the integers from 1 to
1000.

Task-3: Take an exponential signal and perform scaling operation with a negative integer as given
in In-Lab Work section and plot the result.
Task-4: Write a Matlab function “sigshift” for producing a delay of ‘k’ in a given sequence ‘x[n]’
defined by arrays “x” and “n” by using the pseudo code given in In-Lab Work section. Your
function should yield y[n] = x[n-k].

Function [y,n1]=sigshift(x,n,k)

Lab Report Tasks


Task-1: Generate a step sequence u [n] as described in In-Lab section, use it to generate an
impulse as δ [n] = u[n] – u[n-1].

Task-2: Generate an impulse sequence δ [n] as described in In-Lab section, use it to generate a
step sequence

u [n] = ∑ δ[n − k]
𝑘

Task-3: Generate the following sequences using step and impulse sequence.

a. x[n] = u[n] – u[n-10]


b. x[n] = an u[n]
c. ∑an δ[n-k] , k = -10 to 10

Task-4: Write a Matlab function “sigadd” for addition of two sequences x1[n] and x2[n] by using
the pseudo code given in In-Lab Work section.

Function [y,n]=sigadd(x1,n1,x2,n2)

Task-5: Let x(n)  {1, 2,3, 4,5, 6, 7, 6,5, 4,3, 2,1}


Plot the following sequences


a. x1 (n)  2 x(n  5)  3 x(n  4)
b. x2 (n)  x(3  n)  x(n) x(n  2)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy