Sie sind auf Seite 1von 3

Introduction to MatLab and basic operations with signals

ˇ

Jan Cernocky,´ FIT VUT Brno

MATLAB (MATrix LABoratory) – software for scientiﬁc computation and visualization.

1 Some hints:

after the start of Windows, run Matlab from Q:\matlab6p1 by double-clicking on its icon. Ignore the error messages.

in Matlab, commands can be entered only in case the cursor is on the command line: >>

run any text editor in another window — you may use also built-in editor of Matlab, that can make Matlab code more beautiful (colors, etc.). In this editor, open a text ﬁle, where you’ll write everything (commands, results, your comments). The commands can be copied to Matlab using standard copy-paste (Ctrl-c Ctrl-v) sequence. After the end of the lab, you’ll have an exact “log” of what you did and what (hopefully) worked.

In case the Matlab line contains %, everything after this character is considered as a comment and is not interpreted.

in Matlab, you can return to previous commands using the arrow key .
.

The return can be also “selective”, for

.

.

2 Basic functionalities of Matlab

1. Variables:

scalar: a=1;

column vector: vecsl=[1; 2; 3]

matrix: mat=[1 2 3; 4 5 6; 7 8 9]

2. Visualization of variable’s contents:

text: the name of the variable without semicolon+Enter.

graphical with x-coordinates equal to indices of elements

graphical with both x- and y-coordinates: plot(vecx,vecy)

size of a variable:

: plot(vec)

length(vec) – length of a vector size(mat) – number of rows and columns of a matrix (2-point vector).

3. Help !!!

help

lookfor

name

of

function

keyword

4. Case sensitivity: in names of variables, Matlab is like C:

myvector

= Myvector

= MYVECTOR.

Exercises

1. Fill scalar, vector and matrix variable. Visualize their contents as text and graphically.

2. How do you enter a complex number ? What’s the result of visualization of complex numbers ?

3 Basic operations with vectors and matrices

addition/subtraction/multiplication/division vector or matrix vs. scalar: v+s, v-s, v*s, v/s

scalar product (inner product) of two vectors: line v*col v

multiplication of two vectors point-by-point (dot-product): line v.*line v, col v.*col v, mat.*mat

generation of an arithmetic sequence with step 1: min value:max value

with arbitrary step:

min

value:step:max

value

1

transpose of a vector/matrix: vector’, matrix

functions (abs, sin, cos,

zero- and unity-matrices:

) work mostly also with vectors and matrices point-by-point.

zeros(number

rows,number

columns),

ones(number

rows,number

columns)

accessing elements of vectors and matrices: vec(1), vec(2:5), mat(3,4), mat(3,:),

ﬁnding indices of vector/matrix satisfying a condition: find, for example: n = -10:10; ii = find(n<10);

mat(1:5,4)

Exercises

1. Create a 4×5 matrix with all elements equal to 1. Add 3.25 to all elements, then multiply all elements by -1.5. Visualize the third and the fourth columns of the matrix.

2. Fill a vector with values cos(x) for 0 x < 2π. Visualize with correct values on both axis.

3. Find all places where the function cos(x) is negative and change their color in the plot to green.

4 Generation and visualization of signals

Discrete signals can be generated quite easily – a discrete signal is nothing but a vector of values. At ﬁrst, we have to deﬁne the range of the independent variable (most often n). The visualization can be done using function stem. For example, a discrete unit-pulse can δ[n] can be “programmed” in the following way:

=

delta

delta(find(n==0))

stem(n,delta)

n

-10:10;

=

zeros(size(n));

=

1;

Note the use of the condition in the argument — this trick will be used quite often below!.

can not be handled on computer. The time-axis contain points. We must help

ourselves by a sampling with a small time-step (the theory of sampling will be covered later). The visualization will be done

using plot, to achieve the impression of continuous time. An example for the rectangular pulse:

In theory, continuous-time signals

x(t) =

2

0 elsewhere

for

1 t 2

 t = -10:0.01:10; s = zeros(size(t));
 s(find(t>=-1 & t<=2)) = 2; plot(t,s)

Exercises

1. Generate and visualize a discrete unit-step σ[n] for n [10, 10].

2. Generate a continuous-time triangular pulse:

x(t) =

t + 1 t/3 + 1

0

for

for

elsewhere

1 t 0 0 < t 3

5 Transformation of the independent variable – time

The lecture covered time-shifts (delay, advancing), ﬂipping of the time-axis and contraction and dilatation of time. In Matlab, these operations can be done in the following way: First, we generate the standard time axis. After, we generate the modiﬁed time-axis. The signal is generated according to this modiﬁed time-axis. Example for the delayed unit-pulse (discrete-time) δ[n 1]: n
=
-10:10;
nminus1
=
n-1;
deltaminus1
=
zeros(size(nminus1));
deltaminus1(find(nminus1==0))
=
1;
stem(n,deltaminus1)

Note, that we have to visualize the signal against the original time-axis! A similar example for a delayed rectangular pulse (continuous time – see above), advanced by 2 seconds: x(t + 2):

2

t

=

-10:0.01:10;

 tplus2 = t+2; splus2 = zeros(size(tplus2));

splus2(find(tplus2>=-1

plot(t,splus2)

&

tplus2<=2))

=

2;

Exercises

Work with discrete-time unit-step σ[n] and continuous-time triangular pulse from the previous exercises:

1. Generate and visualize the unit-step with the following modiﬁcations of the time axis: σ[n 2], σ[n + 2], σ[n 2],

σ[n + 2].

Explain, what you see.

2. Generate and visualize the triangular pulse for the following modiﬁcations of the time-axis: s(t 2), s(t + 2), s(t 2),

s(t + 2), s(t/2), s(2t).

Explain, what you see.

3