Digital Signal Processing: Intoduction to MATLAB
EXERCISE NO. 1
Introduction to MATLAB - 1
I.
Introduction
MATLAB is a high-level computer language for scientific computing and
data visualization built around an interactive programming environment. It is
becoming the premiere platform for scientific computing at educational
institutions and research establishments. The great advantage of an
interactive system is that programs can be tested and debugged quickly,
allowing the user to concentrate more on the principles behind the program
and less on programming itself. Since there is no need to compile, link and
execute after each correction, MATLAB programs can be developed in much
shorter time than equivalent FORTRAN or C programs. On the negative side,
MATLAB does not produce stand-alone applicationsthe programs can be
run only on computers that haveMATLAB installed. MATLAB has other
advantages over mainstream languages that contribute to rapid program
development:
MATLAB contains a large number of functions that access proven
numerical libraries, such as
LINPACKandEISPACK.Thismeansthatmanycommontasks (e.g.,solution of
simultaneous equations) can be accomplishedwith a single function
call.
There is extensive graphics support that allows the results of
computations to be plotted with a few statements.
All numerical objects are treatedasdouble-precisionarrays.Thus there
isno need to declare data types and carry out type conversions.
MATLAB can be operated inthe interactivemode through its command
window, where each command is executed immediately upon its entry. In
[Type text]
this mode MATLAB acts like an electronic calculator. Here is an example of an
interactive session for the solution of simultaneous equations:
>> A = [2 1 0; -1 2 2; 0 1 4]; % Input 3 x 3 matrix
>> b = [1; 2; 3]; % Input column vector
>> soln = A\b % Solve A*x = b by left division
soln =
0.2500
0.5000
0.6250
II. Objectives
Create a simple M-file in MATLAB
Differentiate data types to variables
Create array, cells and strings in MATLAB using several ways
The symbol >> is MATLABs prompt for input. The percent sign (%) marks
the beginning of a comment. A semicolon (;) has two functions: it suppresses
printout of intermediate results and separates the rows of a matrix. Without
a terminating semicolon, the result of a command would be displayed. For
example, omission of the last semicolon in the line defining the matrix A
would result in the following.
Type the following into the MATLAB
>> A = [2 1 0; -1 2 2; 0 1 4]
DSP
Page 2
[Type text]
Creating simple function and program in MATLAB
1.
Now create an M-file with MATLAB, type the fucntion below and
save the file with the .m extension. The file name of a saved
function should be identical to the name of the function.
function b = gauss(A,b)
n = length(b);
%-----------------Elimination phase------------for k = 1:n-1
for i = k+1:n
if A(i,k) = 0
lambda = A(i,k)/A(k,k);
A(i,k+1:n) = A(i,k+1:n) - lambda*A(k,k+1:n);
b(i)= b(i) - lambda*b(k);
end
end
end
%--------------Back substitution phase----------for k = n:-1:1
b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k);
end
2.
After saving the M-file, you are now ready to solve problems.
For example if the function for Gauss elimination listed above
is saved as gauss.m, it can be called just like any MATLAB
function:
>> A = [2 1 0; -1 2 2; 0 1 4];
>> b = [1; 2; 3];
>> soln = gauss(A,b)
soln =
0.2500
0.5000
DSP
Page 3
[Type text]
0.6250
Data Types and Variables
Data Types
The most commonly used MATLAB data types, or classes, are double,
char and logical, all of which are considered by MATLAB as arrays. Numerical
objects belong to the class double, which represents double-precision arrays;
a scalar is treated as a 1 1 array. The elements of a char type array are
strings (sequences of characters), whereas a logical type array element may
contain only 1 (true) or 0 (false).
Another important class is function handle, which is unique to MATLAB.
It contains information required to find and execute a function. The name of
a function handle consists of the character @, followed by the name of the
function; e.g., @sin. Function handles are used as input arguments in
function calls. For example, suppose that we have a MATLAB function
plot(func,x1,x2) that plots any user-specified function func from x1 to x2. The
function call to plot sin x from 0 to would be plot(@sin,0,pi).
Additional classes can be defined by the user. The class of an object
can be displayed with the class command.
3.
Type the following MATLAB commands.
>> x = 1 + 3i % Complex number
>> class(x)
DSP
Page 4
[Type text]
MATLAB contains several built-inconstants and special variables,most
important of which are
ans
eps
4.
Default name for results
Smallest number for which 1
+ eps > 1
Infinity
Not a number
inf
NaN
i or j
Pi
realmi
Smallest usable positive
n
realm
number
largest usable positive
ax
number
Type the following MATLAB commands:
>> warning off % Suppresses print of warning messages
>> 5/0
>> 0/0
>> 5*NaN % Most operations with NaN result in NaN
>> NaN == NaN % Different NaNs are not equal!
DSP
Page 5
[Type text]
>> eps
5.
Type the following to the MATLAB command prompt to create
arrays.
>> A = [ 2 -1 0
-1 2 -1
0 -1 1]
>> A = [2 -1 0; -1 2 -1; 0 -1 1]
>> b = [1 2 3]
>> b = [1; 2; 3]
DSP
Page 6
[Type text]
>> b = [1 2 3]
>> A = [8 1 6; 3 5 7; 4 9 2]
>> A(2,3)
A(:,2)
>> A(2:3,2:3)
DSP
Page 7
[Type text]
6.
Type the following to the MATLAB command prompt to create
cell arrays.
>> c = {[1 2 3], one two three, 6 + 7i}
>> celldisp(c)
>> c{1}
DSP
Page 8
[Type text]
>> c{1}(2)
7.
Type the following to the MATLAB command prompt to create
strings.
>> s1 = Press return to exit;
>> s2 = the program;
>> s3 = strcat(s1,s2)
>> s4 = s1(1:12)
DSP
Page 9
[Type text]
Observations
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
DSP
Page 10