matlab basics. useful for design filter *iir, fir9. dsp tutorial
DESCRIPTION
You can learn all basis about MATLAB from this document.Filter DesignFIRIIRTRANSCRIPT
-
29
MATLAB basics
Prof. Dr. M. Krauledat
Faculty of Technology and Bionics
Audio and Speech Processing, Prof. Krauledat
-
MATLAB Desktop
Command Window
Current Folder Workspace Command
History Help Browser
30
-
Order of precedence
Suggestion: Use parentheses!! Also improves readability.((a*b*c)/(d*e*f))*123 instead of a*b*c/d/e/f*123
MATLAB as Calculator
31
-
MATLAB as Calculator
Test your understanding: Calculate the following expressions
1.
/ .
32
-
MATLAB as Calculator
4 2 2
2
>> ((4+2*2)/2)^2ans =
16
610
13
18
57 59
>> 6*(10/13) + 18/(5*7) + 5 * (9^2)ans =
410.1297
6 35/ 14.
>> 6*35^(1/4)+14^0.35ans =
17.1123
33
-
MATLAB as Calculator
Help-function provides description of the function, e.g. help sin
Detailed documentation is given by doc sin help matlab\elfun provides list of elementary
functions lookfor searches for key words, e.g. lookfor sine Exercise: Find out how to read in audio files into MATLAB
34
-
MATLAB as Calculator
Exercise: Find out how to read in audio files into MATLAB
>> lookfor audio
audioplayer - Audio player object.audiorecorder - Audio recorder object.audiodevinfo - Audio device information.audioplayerreg -audioread - Read audio files>> help audioread
35
-
MATLAB as Calculator
Exercise: 1. Calculate the Nyquist rate for a signal with frequency
content below .2. The frequency threshold is now .
Calculate the Nyquist rate again by using the command history.
3. Save the latest area variable in the file nyquist. Close MATLAB and start it again. Load the file and see if the variable is known.
36
-
Vector- and Matrix-Elements Matrix-oriented approach In MATLAB a vector/matrix is simply an array of scalars
Example: measurement of the temperature once per hour
representation of the data as a vector: [19, 21, 25, 20]
5 5
7
8
5 1 3 5 1 2
8 5 6scalar
Column vector
Row vector
Matrix / array
15
20
25
30
10:00 11:00 12:00 1:00
Temperature
37
-
Vector- and Matrix-Elements
Vectors: To create a row vector, separate the elements by comma or space. Use square brackets. For example,
>>p = [3,7,9]p =
3 7 9
>>p = [3 7 9]p =
3 7 9
38
-
Vector- and Matrix-Elements You can create a column vector by separating the elements by semicolons.
For example,
>>g = [3;7;9]g =
379
You can also create a column vector by using the transpose notation (').
>>p = [3,7,9]'p =
379
39
-
Vector- and Matrix-Elements
Creating Matrices
If the matrix is small you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing
>>A = [2,4,10;16,3,7];
creates the following matrix: 2 4 10
16 3 7
Remember, spaces or commas separate elements in different columns, whereas semicolons separate elements in different rows.
40
-
Vector- and Matrix-ElementsFind out how your variables look like!
Command size shows dimensions of the vector/matrix Command length gives the number of elements in the
vector See also workspace
41
-
Initializing with Shortcut Expression
The colon operator (:) easily generates a large vector of regularly spaced elements. Parentheses are not needed but can be used for clarity. Do not use square brackets.
Typing
>>x = m:q:n
creates a vector x of values with a spacing q. Rule of thumb:>>x = first:increment:last
42
-
Exercise
1. Calculate the sine for the angles , , by using shortcut expressions, where Define the angle vector , , Calculate the sine for each angle
2. Consider the function
. We want to assign the
row vector to the variable y.
43
-
Indexing / Addressing
Usage of array indicesMATLAB command Resultb(1) the first element of b in case b contains either
one row or one column.a(1,2) Gives the element on the first row and second
column of a.a(1,:) Gives the first row of a.a(:,3) Gives the third column of a.a(:,2)=[10;10;10] Changes the second column of a into a
column with 10's only. Only works if a is amatrix,
a(1,[3,1]) A matrix consisting of the 3rd and 1st elements of the first row of a.
44
-
Exercise
Considering the 3x3 matrix A=randn(3). Please create/show the numbers by using indexing methods:1. Show 3rd column of the 1st row2. Show 2nd row3. Define a sub-array consisting of numbers from column 1
and 2 and row 1 and 24. Replace 3rd column by ones5. Interchange 2nd and 3rd row
45
-
Element-by-Element OperationSymbol
+
-
+
-
.*
./
.\
.^
Examples
[6,3]+2=[8,5]
[8,3]-5=[3,-2]
[6,5]+[4,8]=[10,13]
[6,5]-[4,8]=[2,-3]
[3,5].*[4,8]=[12,40]
[2,5]./[4,8]=[2/4,5/8]
[2,5].\[4,8]=[2\4,5\8]
[3,5].^2=[3^2,5^2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Operation
Scalar-array addition
Scalar-array subtraction
Array addition
Array subtraction
Array multiplication
Array right division
Array left division
Array exponentiation
Form
A + b
A b
A + B
A B
A.*B
A./B
A.\B
A.^B
46
-
2D Graphics
>> plot(x,y) plots vector y over x x has to be the same size as y If x is not defined, y is plotted over index Example:>> y=[-1 3 5 6 7 3]y =
-1 3 5 6 7 3>> plot(y)
47
-
2D Graphics
>> x=0:0.01:2*pi;>> y=3*cos(2*x);>> plot(x,y)
48
-
2D Graphics
Annotations and Beautifications
grid puts grid lines on the plottitle('mytitle') puts mytitle on top of the plotxlabel('my_xlabel') puts my_xlabel under the horizontal axisylabel('my_ylabel') puts my_ylabel next to the vertical axislegend('one','two') generates a legend with the words one and
two for two plots
49
-
Subplots
>> subplot(m,n,p) Creates several plots in one figure Breaks down the figure into a m-by-n
matrix of plots uses subplot number p as active
plot
Example:>> subplot(2,3,4); >> plot(sin(0:pi/50:2*pi));
50
n=3
m=2
p=1 p=2 p=3
p=4 p=5 p=6
-
MATLAB Script
A script is a sequence of MATLAB instructions Stored in an M-file *.m type displays the content of the script edit opens editor Script can be executed or run by simply entering the name of
the file (w/o .m extension) Search path: Current Folder Documentation: A comment is anything from a % to the end of
the line help command works with scripts as well: first block of
comments will be displayed
51
-
MATLAB ScriptCreating and Using a Script File
52
-
Function User-defined function defined by programmer Can be used either in the command window or in a script Name of the function (functionname) should be the same as the
name of the M-file Local variables General structure:
53
-
Function
Pre-defined structure of a function
54
-
Function
ExampleWrite a function audiostats which takes the number of bits nbits and the sampling rate fs
as input returns the SQNR sqnr and the Nyquist frequency nyfs
for an audio file with the given characteristics.
55
-
Control Flow IF Statement If statement chooses whether another
statement is executed or not (case-by-case-analysis)
General formif logicalexpression1
statementgroup1elseif logicalexpression2
statementgroup2else
statementgroup3end logicalexpression is a
logical/relational expression that is true or false
Action is a statement that will be executed if the logicalexpression is true
Every if statement must have an accompanying end statement. The end statement marks the end of the statements that are to be executed if the logical expression is true.
56
-
Control Flow
Flowchart of a for-end loop
Syntax of a for-end loop
for k = m:s:nend
k: loop index variablem: the value of k in the first passs: the increment in k after each passn: the value of k in the last pass
57
-
Control Flow
ExerciseThe energy of a signal over interval [-N,N] is defined as
Write a script file to compute the signal energy of sin
where N=100.1. Use a for loop to do this.2. Use vector summation and exponentiation.
58
-
Control Flow
Flowchart of the while-loop
Syntaxwhile logical expression
statementsend
59