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

Lab#1

signals lab

Uploaded by

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

Lab#1

signals lab

Uploaded by

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

Digital Signal Processing Lab 1

1 Review of MATLAB fundamentals:

In this course we will use software MATLAB to implement and simulate digital signal processing
algorithms.
The name MATLAB stands for MATrix LABoratory. Originally MATLAB was developed to deal
with only one single but universal data type: the matrix.
A matrix is defined by [1]
 Its associated name
 The number of rows
 The number of column
 The value of all matrix elements

The matrix data type includes vectors, for the case when either of the number of rows or the
number of column equals to 1; and it includes scalars when both the number of rows and
columns equal 1. Furthermore, matrices can be either real-valued or complex valued, which is
the most general case.
There is a small tutorial in this exercise for newcomer to become familiar with the MATLAB
fundamentals. It is recommended to try out introduced methods while solving the described
tasks.

1.1 How to get help?


There are several methods in MATLAB to access texts, which explain the usage and behavior of
given functions:
 The HELP command is the basic help feature to get help of a function. For example, to
get help on the SUM function, type at the command prompt:
help sum
 HELPWIN is used in the same manner as help, but it opens a separate window with
advance search facilities and links to related topics.
 HELPDESK uses a window of an installed web browser to display the help texts.
 If the name of a command is not exactly known, the LOOKFOR function helps by seeking
through the first comment line of all available functions and listing all functions where a
desired expression is found.

1.2 Statement, expressions and variables:


MATLAB is an expression language. The expressions you type are interpreted and evaluated.
MATLAB statements are usually of the form
variable = expression, or simply
expression
Expressions are usually composed from operators, functions and variable names. Evaluation of
the expression produces a matrix, which is then displayed on the screen and assigned to the
variable for future use.
A statement is normally terminated with the carriage return. However, a statement can be
continued to the next line with three or more periods followed by a carriage return. On the
other hand, several statements can be placed on a single line if separated by commas or
semicolon.

1.2.1 The variable ANS:


If the variable name and = sign are omitted, a variable ANS (for answer) is automatically created
to which the result is assigned.

Sir Syed University of Engineering & Technology


Digital Signal Processing Lab 1

1.2.2 Suppressing screen output:


If the last character of a statement is a semicolon, the printing is suppressed, but the assignment
is carried out. This is essential in suppressing unwanted printing of intermediate results.

1.2.3 Colon notation:


Colon notation is used to generate vectors, and reference submatrices, known as subscripting.
Creative use of this feature makes MATLAB programming code simple, readable and minimizes
the use of loop, which slows MATLAB.
 An index vector is created by using the colon notation
first : last
For example, creates the vector .
 An spacing can be introduced between two elements of a vector by using the colon
notation
first : spacing : last
For example, creates the vector .

1.3 Workspace:
The contents of all the variables are stored in the MATLAB workspace. This is the memory region
allocated for variable.

 The command WHO lists all variable which currently exist in the workspace.
 WHOS additionally lists the size and amount of allocated memory.
 The entire workspace or single variable can be cleared by using the CLEAR command.

1.4 Elementary operations:


Algebraic expressions can be formed by the following basic operations:

 Transpose of a vector/matrix can be produced by


e.g.
 + is used to add two vectors/matrices of identical size, or a vector/matrix and a scalar,
e.g.

 - subtracts two vectors/matrices of identical size, or a vector/matrix and a scalar, e.g.:

 performs element-wise multiplication of two vectors/matrices of identical size, or


a vector/matrix and a scalar. For example, to square all elements of , we may write

 performs element-wise division of two vectors/matrices of identical size, or a


vector/matrix and a scalar. For example, the reciprocal of all elements in is computed
through

 performs vector/matrix multiplication. The number of columns in the first


vector/matrix must equal to the number of rows in the second. Example:

1.5 Real and complex matrices:

Sir Syed University of Engineering & Technology


Digital Signal Processing Lab 1

In general, any matrix within the MATLAB can be complex-valued. However, for efficient
storage, MATLAB distinguishes between real-valued and complex-valued matrices. Real valued
matrices are matrices where the imaginary parts of all matrix elements are zero. The following
essentials must be known to deal with complex-valued matrices:
 The variables i and j are assigned, by default, the value . This is used to define
the complex values. For example,

generates a complex-valued variable.


 The real part of a complex-valued matrix can be extracted by using the function REAL
and imaginary part can be extracted by using the function IMAG. Both functions deliver
the real-valued matrices as outputs.
 The function CONJ is used to produce the complex conjugate of a matrix.
 The special character ’ generates the complex conjugate transpose of a matrix, the
hermitian matrix. This character is written after an expression or variable.
For example, the hermitian of a matrix A can be obtained through:

1.6 M-files:
MATLAB can execute a sequence of statements stored in a file. Such files are called M-files
because they have “.m” extension as the last part of their file name.
There are two types of M-types:
 Script files
 Function files
1.6.1 Script file:
In a script file, script is a sequence of commands as they could be entered at the prompt. The
script allows to execute the entire sequence multiple times in a comfortable way, or to test
modified versions.
1.6.2 Function file:
In a function file, function has the additional feature of passing parameters. On calling a
function, it may read input arguments and after execution it may return output values. The
“FUNCTION” command specifies the input and output parameters of a function. It must be very
first command in a function file. Only comment may be written before the FUNCTION command.
Any text after a % sign inside an m-file is comment.

1.7 Flow control:


In MATLAB flow control means that a program may not only be executed straight down from top
to bottom but with branches and repetitions.

 The FOR function allows to create a loop which is controlled by a counter.


 More general exit conditions can be implemented in a WHILE loop.
 The IF command allows for conditional programming, together with ELSE.
 All these commands come along with an END command which acts as a closing brace,
together with the opening brace, i.e. the introducing FOR, WHILE or IF command.

1.8 Introduction to SIMULINK


Simulink is a software package for modeling, simulating, and analyzing dynamical systems. It
supports linear and nonlinear systems, modeled in continuous time, sampled time, or a hybrid
Sir Syed University of Engineering & Technology
Digital Signal Processing Lab 1

of the two. Systems can also be multirate, i.e., have different parts that are sampled or updated
at different rates.

For modeling, Simulink provides a graphical user interface (GUI) for building models as block
diagrams, using click-and-drag mouse operations. With this interface, you can draw the models
just as you would with pencil and paper (or as most textbooks depict them). This is a far cry from
previous simulation packages that require you to formulate differential equations and difference
equations in a language or program. Simulink includes a comprehensive block library of sinks,
sources, linear and nonlinear components, and connectors. To customize and create own blocks
S-Functions can be used, which is another skill.

MATLAB and Simulink are integrated; you can simulate, analyze, and revise your models in
either environment at any point.

1.8. Getting Started with Simulink

1.8.1. Opening Simulink Library Browser:

1. In the Matlab command window click on the


Simulink Library Browser or just write
“Simulink” in Command window prompt.
Expending these libraries can access various
Toolboxes, Blocksets and Simulink
functions.

Fig. 1: Simulink Library Browser Fig. 2: Expended Libraries


2. Write “Simulink3” in CW to get Simulink Block Library. This is another way of access Simulink
Library. Double clicking each icon will open the corresponding library.
Blocksets and Toolboxes can be opened in the same way.

Sir Syed University of Engineering & Technology


Digital Signal Processing Lab 1

Fig. 3: Simulink Block Library

Fig. 4: Simulink Library of Blocksets and Toolboxes

1.8.2. Opening new Simulink model:

Click to create new model button on SLB.

Fig. 5: Opening New model (.mdl file)


Exercise:

Task # 1: Try all help methods to get help of any function.

Sir Syed University of Engineering & Technology


Digital Signal Processing Lab 1

Task # 2: (a) Assign 10 samples, from 0 to 9, of time to the vector t.


(b) Assign a vector of samples without assigning it to a variable.
(c) Assign 10 samples, from 0 to 9, of time to any vector without printing it to screen.

Task # 3: Investigate the difference between multiplication and element-wise multiplication


of vectors/matrices.

Task # 4: Generate a complex-valued matrix

and calculate the absolute square of all elements of this matrix.


Task # 5: Implement a function
[x]=sinewave(t)
Which produces a sine wave of 1001 samples with spacing of .
Task # 6: Use MATLAB help to get familiar with the syntax of FOR, WHILE and IF-ELSE statement.

Task # 7: Use FOR loop to generate a single vector y, which is a digitized unit sine wave with ten
samples per cycles, with 100 elements.

Task#8:
(a): Using Simulink, plot a sine wave between t=0s and t=10s.
(b): Using Simulink, plot a sine wave between t=0s and t=100s and also plot its Spectral
Density by setting Max step size to 0.02.

Task#9: Using Simulink, plot a Unit step Signal between t=0s and t=10s.

Task#10: Implement figure 6 in Simulink to understand the concept of synthesizing square wave.

Task#11: Implement the following continuous-time system with a Simulink model:


y(t) = 0.2x(t) +0.8 x(t -2) -0.4x(t -5)
Simulate the system between t=0s and t=10s with the following inputs:
a) x(t) = u(t) (Unit step)
b) x(t) = 2.5 sin(2*pi*f*t)

Task#12: Make a Simulink Model to record and write wave file from a wave device for
the duration of 10 sec at 8 kHz sampling rate and also show its time plot.

Sir Syed University of Engineering & Technology

You might also like