+ All Categories
Home > Documents > Dsp Using Matlab® - 1

Dsp Using Matlab® - 1

Date post: 11-Apr-2015
Category:
Upload: api-3721164
View: 549 times
Download: 3 times
Share this document with a friend
22
Lecture 1: Introduction To MATLAB Mr. Iskandar Yahya Prof. Dr. Salina A. Samad
Transcript
Page 1: Dsp Using Matlab® - 1

Lecture 1: Introduction To MATLAB

Mr. Iskandar Yahya

Prof. Dr. Salina A. Samad

Page 2: Dsp Using Matlab® - 1

Information2 Lectures (Thursday the 14th and 21st FEB)Email: [email protected]

Reserved computers with MATLAB application at the Microcomputer Lab:Monday – Thursday (2pm – 5pm)Friday (3pm – 5pm)

MATLAB 7.5 (MATLAB R2007b)

Page 3: Dsp Using Matlab® - 1

History of MATLABAncestral software to MATLAB

Fortran subroutines for solving linear (LINPACK) and eigenvalue (EISPACK) problems

Developed primarily by Cleve Moler in the 1970’s

Later, when teaching courses in mathematics, Moler wanted his students to be able to use LINPACK and EISPACK without requiring knowledge of Fortran

MATLAB developed as an interactive system to access LINPACK and EISPACK

Page 4: Dsp Using Matlab® - 1

History of MATLABMATLAB gained popularity primarily through

word of mouth because it was not officially distributed

In the 1980’s, MATLAB was rewritten in C with more functionality (such as plotting routines)

The Mathworks, Inc. was created in 1984The Mathworks is now responsible for

development, sale, and support for MATLABThe Mathworks is located in Natick,

Massachusetts MA, USA.

Page 5: Dsp Using Matlab® - 1

MATLAB EnvironmentWhen you open MATLAB, you will see this window:

Main typing/command window

Variable lists /Current Directory

Command history

Page 6: Dsp Using Matlab® - 1

MATLAB EnvironmentMATLAB can function as normal calculator:

Can define variables in MATLAB:>> a = 5;>> b = 3;>> c = 8;>> x = a + b/cx = 5.3750

Can be any character or strings of character as long as it is not separated by “space”. **But we cannot use names that have already been used by MATLAB as the predefined functions and variables.

Page 7: Dsp Using Matlab® - 1

MATLAB EnvironmentTo see the variables currently stored: WHO

Detailed variable information: WHOS

>> whoYour variables are:A a b c x

>> whos Name Size Bytes Class

A 1x1 8 double array a 1x1 8 double array b 1x1 8 double array c 1x1 8 double array x 1x1 8 double array

Grand total is 5 elements using 40 bytes

Page 8: Dsp Using Matlab® - 1

MATLAB Environment

To clear variables: CLEAR

Saving and loading variables: SAVE/LOAD

MATLAB is Case-Sensitive. Be careful when typing.

>> clear a b c>> whoYour variables are:A x

>> clear>> who>>

>> saveSaving to: matlab.mat>> loadLoading from: matlab.mat

>> save test>> load test

Page 9: Dsp Using Matlab® - 1

MATLAB EnvironmentMost powerful command in MATLAB: HELP

Try typing “help” - list of all help topicsYou can get help of specific functions and

commands that you likeTry “help variables”, and also “help save

variables” etc.All about MATLAB is accessible by clicking

START and then choose MATLAB Help.

Effectively, you can leave this lecture now and learn everything from MATLAB Help.

On the MATLAB Help window, run demos.

Page 10: Dsp Using Matlab® - 1

Built-in math functions:

MATLAB Environment

Page 11: Dsp Using Matlab® - 1

MATLAB EnvironmentPredefined Variables:

“Ans”, “sin()” and “pi” are examples of predefined variables. Do Not define a variable with these names, or try to change them.

What other predefined variables are there?

Handle Complex numbers:>> n = 3+4i;>> m = 1+i;>> mul = m*nmul = -1.0000 + 7.0000i>> real(mul)ans = -1

Page 12: Dsp Using Matlab® - 1

MATLAB EnvironmentMatrices and Vectors:

Create matrices by using square brackets, separate rows by semicolon, each element separated by coma or space:

Inverse a matrix using “inv(A)”, where “A” is your matrix. Similarly, we can get the Determinant by using “det(A)”.

Find out what other predefined functions are available to manipulate matrices and vectors.

>> Matrix_A = [1 2 3 ; 4 5 6 ; 7 8 9]Matrix_A = 1 2 3 4 5 6 7 8 9

>> Matrix_B = [9,8,7;6,5,4;3,2,1]Matrix_B = 9 8 7 6 5 4 3 2 1

Page 13: Dsp Using Matlab® - 1

MATLAB EnvironmentStrings:

Character strings are enclosed in single quote –

To use quotes, use double single quotes for a single quote, and one double quote for a double quote (What?!) E.g:

String indexing is used to refer to a specific “letter” in the string:

>> Y = 'MATLAB is very powerful‘Y =MATLAB is very powerful

>> word = 'did''t‘word =did't

>> word = 'did"t‘word =did"t

Page 14: Dsp Using Matlab® - 1

MATLAB EnvironmentExample:

>> me = 'Iskandar‘me =Iskandar

>>me(1)ans =I

>>me(5)ans =n

>>me(8)ans =r

Page 15: Dsp Using Matlab® - 1

MATLAB EnvironmentOther string funtions:

length - Return the number of character strings

strcmp - Compares two stringsstr2num - Converts a string to a numerical

value, i.e. converts ‘123’ to 123.strrep - Replaces characters in a string with

different characters (very useful in editing programs and debugging)

upper - Converts a string to uppercaselower - Converts a string to lowercase

Page 16: Dsp Using Matlab® - 1

MATLAB EnvironmentInput and Output statement:

Use semicolon at the end of your line to suppress the display- matlab will carry out the calculation without displaying the result. Try this

fprintf – Use this command to generate a text line:

The “\n” is included to prompt the display to start a new line, e.g.:

>> fprintf('This is a test. \n')This is a test.

>> fprintf(' This \n is \n a \n test. \n ') This is a test.

Page 17: Dsp Using Matlab® - 1

MATLAB Environmentfprintf – to display the value of variables, we

can include a “%g” format character:

Information from user can be asked using input function:

>> A = 3.45A = 3.4500

>> fprintf('The value of A is %g. \n', A)The value of A is 3.45.

>> num = input('Specify a number. \n');Specify a number. 2>> fprintf('The number u entered is %g. \n', num);The number u entered is 2.

Page 18: Dsp Using Matlab® - 1

MATLAB EnvironmentPlotting in MATLAB Intro:

Use plot function. Example of plotting then function y=|x|sin(x). Use linspace function to generate domain variable x, i.e. linspace(x1,x2,n).

This function generates n equally spaced values from x1 to x2 and stores them in x as a one-dimensional array with one row: >> x = linspace(0,5,11)x = Columns 1 through 9 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000Columns 10 through 11 4.5000 5.0000

Page 19: Dsp Using Matlab® - 1

MATLAB EnvironmentPlotting in MATLAB Intro:

Use plot as plot(x,y). This function plots the values stored in array y versus the array of x. The two arrays must have the same number of values:>> x=linspace(-100,100,5000);

>> y=abs(x).*sin(x);>> plot(x,y)

• The abs function returns the absolute values of x. •This is the generated plot:

Page 20: Dsp Using Matlab® - 1

MATLAB EnvironmentPlotting in MATLAB Intro:

We can also plot multiple traces on a single plot:

>> x=linspace(0,2*pi,100);>> y1=sin(x);>> y2=cos(x);>> y3=sin(x).*sin(x);>> plot(x,y1,x,y2,x,y3)

Page 21: Dsp Using Matlab® - 1

MATLAB EnvironmentPlotting in MATLAB Intro:

We use xlable, ylable, title and legend tocomplete the plot:

>> xlabel('x');>> ylabel('y');>> title('Various trigonometric Function');>> legend('sin(x)', 'cos(x)', 'sin(x)*sin(x)');• This example is just a tiny fraction of MATLAB’s full range plotting capabilities. We shall see that later.

Page 22: Dsp Using Matlab® - 1

MATLAB EnvironmentPredefined Functions in this chapter:

sin abs fprintf log help save

load sqrt real imag angle inv

rand det lentgh strcmp str2num upper

lower input plot linspace xlabel ylabel

title legend lookfor which roots


Recommended