Digital Signal Processing Lab: week 4

advertisement
Mobile Robotics COM596
Laboratory Session: week 1
Introduction to MATLAB and Mathematical Preliminaries
Integrated environment of MATLAB is shown in the diagram below. You can create
your own tools to customize toolbox or harness with another toolbox such as Control,
Signal Processing, Optimisation or Neural Network toolbox. You can write M-files
and run at command prompt. Simulink is a tool in Matlab that allows users to simulate
system behaviour using block diagrams. Introduction to Toolboxes and Simulink will
be given in later labs.
Toolbox
Simulink
User-written
M-files
MATLAB
Figure 1: Matlab environment
Exercise 1: Variables
The prompt (>>) in the command window indicates that Matlab is ready to accept
input. A variable (x or a) is entered at the prompt and Matlab responds in the
following way.
>>x=2.66
x=
2.6600
>>a=x+0.0099
a=
2.6699
Numerical output is suppressed by putting a semicolon (;) at the end of the line. For example
>>x=2.66;
>>x=x+0.33
x=
2.9900
http://www.scis.ulster.ac.uk/~siddique
1
Exercise 2: Representing vectors
A vector in Matlab is represented by any variable e.g. x. A row vector x can be
initialised as
>> x = [6 5 4 8]
It will display the vector x as
>>x =
6 5

4
8
If elements are separated by semicolon, a column vector y is initialised, e.g
>> y = [1;2;3;4]
It will display the vector y as
y=
1
2
3
4

Individual elements of a vector can be accessed by the vector name followed by
an index running from 1 to n (max number of elements) to point to the elements
e.g.
>> x(4)
ans =
8
It will return the 4th element of vector x which is 8

The transpose of a column vector results in a row vector, and vice versa. For
example
>>z=y’
will produce
1 2 3 4

Vectors of the same size can be added or subtracted, where addition is performed
component wise. However, for multiplication, specific rules must be followed in
order to obtain the correct resulting values. The operation of multiplying a vector
X with a scalar k is performed component wise. For example
>>X=[1 3 –5];
>>k=5;
>>P=k*X
http://www.scis.ulster.ac.uk/~siddique
2
produces the output
5 15 -25

The operator .* performs element by element operation. For example,

>>x.*z
% x and z are both row vectors
results in
6 10 12 32

Inner product or dot product of two vectors x and z (both are row vectors) is a
scalar quantity. The inner product is given by
>>S=x*z’
yields
S= 60

Various norms (measure of size) of a vector can be obtained. For example, the
Euclidean norm is the square root of the inner product of the vector and itself. e.g.
>> N=norm(z)
produces the output
ans =
5.4772

The angle between two vectors X and Y is defined by cos 
X *Y
, where
X *Y
X*Y is the inner product and X , Y are the norms of the vectors. The angle
between the vectors is calculated as follows
>>theta=acos(X’*Y/(norm(X)*norm(Y)))

The zero vector is a vector with all components equal to zero
>>Z=zeros(1,4)
produces a row vector of 4 elements
Z=
0

0
0
0
Sum vector is a vector with each component equal to one. To generate a sum
vector of size 4, use
>>I=ones(1,4)
results in
http://www.scis.ulster.ac.uk/~siddique
3
I=

1
1
1
1
In Matlab the colon (:) can be used to generate a row vector. For example,
>>x=1:8
generates a row vector of integers from 1 to 8
x=
1

2
3
4
5
6
7
8
For increments other than unity, any value can be used
>> z=0:pi/3:pi
results in
z=
0
1.0472
2.0944
3.1416
 For negative increments
>> x=5:-1:1
results in
x=
5
4
3
2
1
Exercise 3: Representing matrices
A matrix is represented with a variable name e.g. w, elements in each row are separated
by blanks or commas. A semicolon must be used to indicate the end of a row. If a
semicolon is not used, each row must be entered in a separate line. Matrix elements can
be any Matlab expression. For example, a 3x3 matrix can be initialised as
>> w = [4 5 6; 0 4 7; 3 5 1]
will display the matrix w as
w=
4
0
3
5
4
5
6
7
1
>>w=[2*2 5 3*2; 0 2*2 7; 3 5 1]
results in
w=
4
5
6
0 4
7
3 5
1
http://www.scis.ulster.ac.uk/~siddique
4

Individual elements of a matrix can be accessed by the matrix name followed by a
two indices running from 1 to n (max row) and 1 to m (max column) to point to
the elements e.g.
>> w(2,3)
ans =
7
It returns the element on row 2 column 3 which is 7
Entire row or column of a matrix can be addressed by means of the symbol (:). For
example
>>r2w=w(2,:)
returns the 2nd row of the matrix
0
4
7
>>c2w=w(:,2)
returns the 2nd column of the matrix
5
4
5

Matrix w can be transposed (rows will convert to columns) as
>> w’
It will display the transpose of matrix w as
ans =
4 0
5 4
6 7
3
5
1
Exercise 4: Input and Output Commands
Matlab supports simple input-output commands. The command ‘input’ can be used
for assigning values to variables. The general form of use is shown as follows.
variable = input('prompt')
For example
>> x = input('Enter x: ')
Enter x: 5
x=
5
http://www.scis.ulster.ac.uk/~siddique
5
Matlab automatically generates a display when commands are executed. In addition to
this automatic display, Matlab has several commands that can be used to generate
displays or outputs. Commands that is frequently used to generate output are: disp().
The command disp outputs (displays) text or array. The general form is as follows
disp(x)
disp(x) displays an array. It does not print the array name. If x contains a text string,
the string is displayed. For example
>> x=5;
>> disp(x)
5
>> x='yes';
disp(x)
Yes
Exercise 5: Handling Mathematical functions
Sine, Cosine, Tangent, Logarithm and Exponential functions can be used in Matlab e.g.
sin(X) is the sine of the elements of X.
cos(X) is the cosine of the elements of X.
tan(X) is the tangent of the elements of X.

log
Natural logarithm i.e Log base e. log(X) is the natural logarithm of the
elements of X. Complex results are produced if X is not positive. log2 is Log base
2, log10 is Log base 10.
Example: How many bits are required to represent the decimal number 128 in binary
number
>> log2(128)
ans =
7
Exercise 6: Reading data files, reading a column from a file
Matlab provides many ways to load data from disk files into workspace, the process
called importing data, and to save workspace variables to disk files, the process called
exporting data. Suppose you have a data file named ‘signal.dat’ which contains 3 columns
of data. To read this data file use command as
>> load d:\signal.dat;
d:\ specifies the path and semicolon stops displaying values
on the screen
 Data can be assigned to any other variable e.g.
>>s = signal;
http://www.scis.ulster.ac.uk/~siddique
6
>> load FILENAME X
%loads only X.
>> load FILENAME X Y Z %loads just the specified variables.

The wildcard '*' loads variables that match a pattern (MAT-file only).
Exercise 7: Plotting, labelling, and printing graphs
Matlab has an excellent set of graphic tools for representing experimental results in 2D and 3-D plots. Plotting a given data set or the results of computation is possible
with very few commands.
>>plot
>>plot(x,y)

If X or Y is a matrix, then the vector is plotted versus the rows or columns of the
matrix, whichever line up. If X is a scalar and Y is a vector, length(Y)
disconnected points are plotted.
>>plot(y)

% Linear plot.
%plots vector x versus vector y.
%plots the columns of Y versus their index.
Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S)
where S is a character string made from one element from any or all the following
3 columns:
b
g
r
c
m
y
k
blue
green
red
cyan
magenta
yellow
black
. point
o circle
x x-mark
+ plus
* star
s square
d diamond
v
^
<
>
p
h
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram
:
-.
--
solid
dotted
dashdot
dashed
For example,
>>plot(X,Y,'c+:')
%plots a cyan dotted line with a plus at each data point;
>>plot(X,Y,'bd')
%plots blue diamond at each data point but does not draw any
line.
 Title and Labels for x-axis and y-axis created by
>>xlabel(‘x-axis’)
>>ylabel(‘y-axis’)
>>title(‘Plot of Gaussian MF’)
 To print a graph to a file, use commands
>>print –dmeta c:\pathname\<filename>
This prints plot into a file, which can be later used in a MS word document by simply
inserting the picture.
http://www.scis.ulster.ac.uk/~siddique
7
Exercise 8: Writing m-files
You can write lines of Matlab codes in a file and save it as m-file. The m-files can be
run from command prompt. For example
Write the following codes in a new file
%This m-file creates a Gaussian function – Gauss.m
x=1:0.1:10
m=4.5;
s=1;
xm=(x-m)/s;
A=exp(-0.5*xm.^2);
plot(A)
xlabel(‘x-label’)
ylabel(‘y-label’)
grid %creates a grid lines across the plot

Save the file as Gauss.m and run the file from command prompt
>>Gauss
http://www.scis.ulster.ac.uk/~siddique
8
Download