Introduction To Matlab and Basic Operations With Signals: Exercises

You might also like

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

Introduction to MatLab and basic operations with signals

Jan Cernock, FIT VUT Brno y MATLAB (MATrix LABoratory) software for scientic computation and visualization.

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 youll 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, youll 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 example, you can return to already typed plot-commands by: pl ...

Basic functionalities of Matlab


1. Variables: scalar: a=1; column vector: vecsl=[1; 2; 3] row vector: vecrad=[2.5 16e-3 85] matrix: mat=[1 2 3; 4 5 6; 7 8 9] 2. Visualization of variables contents: text: the name of the variable without semicolon+Enter. graphical with x-coordinates equal to indices of elements 1,2,3,. . . : plot(vec) graphical with both x- and y-coordinates: plot(vecx,vecy) size of a variable: length(vec) length of a vector size(mat) number of rows and columns of a matrix (2-point vector). 3. Help !!! help name of function lookfor 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 ? Whats the result of visualization of complex numbers ?

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, log,. . . ) work mostly also with vectors and matrices point-by-point. zero- and unity-matrices: 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,:), mat(1:5,4) nding indices of vector/matrix satisfying a condition: find, for example: n = -10:10; ii = find(n<10);

Exercises
1. Create a 45 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.

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 dene 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: n = -10:10; delta = zeros(size(n)); delta(find(n==0)) = 1; stem(n,delta) Note the use of the condition in the argument this trick will be used quite often below!. In theory, continuous-time signals 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: x(t) = 2 0 for 1 t 2 elsewhere

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: t+1 t/3 + 1 x(t) = 0 for 1 t 0 for 0 < t 3 elsewhere

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 modied time-axis. The signal is generated according to this modied 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 & tplus2<=2)) = 2; plot(t,splus2)

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 modications 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 modications 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.

You might also like