Tuesday 26 December 2017

Introduction To MATLAB

MATLAB is the most popular tool used for Digital Signal Processing. It provides one of the strongest environments for study and simulation of the real-world problems and their solutions, especially in the field of engineering. For Signal Processing, it has a very comprehensive and easy-to-use toolbox with lots of DSP functions implemented. Moreover, with the aid of Simulink, we can create much more complex situations very easily, and solve them.
6.
If you don’t know anything about MATLAB, then MATLAB help is the best way to learn about something. In the command window of MATLAB simply write ‘help’;
>> help
It will display list of all toolboxes included in MATLAB. Then by investigating the name of toolbox or the name of a function, which you would like to learn how to use, use the ‘help’ command:
>> help functionname
This command displays a description of the function and generally also includes a list of related functions. If you cannot remember the name of the function, use the ‘lookfor’ command and the name of some keyword associated with the function:
>> lookfor keyword
This command will display a list of functions that include the keyword in their descriptions. Other help commands that you may find useful are ‘info’, ‘what’, and ‘which’. Descriptions of these commands can be found by using the help command. MATLAB also contains a variety of demos that can be with the ‘demo’ command.
Wide tutorials are available on internet for help on any function or how to use it.
The Semicolon (;)
When command is executed the very next will display the output of the command. If a semicolon is typed at the end of command the output is not displayed (called suppress the output).
Typing % or %{ and }%
When the symbol % (percent symbol) is type in the beginning of a line, the line is designated as a comment, which is called single line comments.
When any number of statements written between symbols %{ and %} is treated comments, which is called multi-line comments or blocks commenting.
Workspace All variables used in the current MATLAB session are saved in the Workspace and Save in. mat file
clc The clc command clears the command window and does not change anything that was that was done before
clear Remove all variable from the current workspace/memory.
clear x y z Removes only variable x, y and z from current workspace/memory.
who Display a list of the variables in the current workspace/memory.
whos Display a list of the variables in the current workspace/memory and their size together with information about their bytes and class.
what Show all M-files in the current directory (Z:\)
pwd Show current working directory path (z:\)
7.
Vector & Matrix Operations
x = [1 2 3] ;
x = [1 2 3; 4 5 6; 7 8 9]
x = [0:0.5:10]
A = x(1,3)
A = [1 2; 3 4]
B=A*A
C=A.*A
Operations *,-,+,/,.*,./ etc
Logical Operations: <,>, <=,~= etc
Math Functions:
sin Sine
cos Cosine
tan Tangent
asin Inverse sine
acos inverse cosine
atan inverse tangent
exp exponential
log natural logarithm
log10 common logarithm
sqrt Square root
abs Absolute value
sign signum
Logical Operations
x=5
x = =2  Is x equal to 2?
x ~= 2  Is x not equal to 2?
x > 2  Is x greater than 2?
x < 2  Is x less than 2?
x >= 2  Is x greater than or equal to 2?
x <= 2  Is x less than or equal to 2?
Lab Questions
Problem #1 Run the MATLAB help desk by typing helpdesk. The help desk provides a hypertext interface to the MATLAB documentation.
Observation:
Problem #2 Use MATLAB as a calculator. Try the following:
pi*pi - 10
sin(pi/4)
ans ˆ 2 %<--- "ans" holds the last result
Observation:
Problem #3 Do variable name assignment in MATLAB. Try the following:
x = sin( pi/5 );
cos( pi/5 ) %<--- assigned to what?
y = sqrt( 1 - x*x )
Observation:
Problem #4 Complex numbers are natural in MATLAB. The basic operations are supported. Try the following:
z = 3 + 4i, w = -3 + 4j
real(z), imag(z)
abs([z,w]) %<-- Vector constructor
conj(z+w)
angle(z)
exp( j*pi )
exp(j*[ pi/4, 0, -pi/4 ])
Observation:
Problem #5 Make sure that you understand the colon notation. In particular, explain in words what the following MATLAB code will produce.
jkl = 0 : 6
jkl = 2 : 4 : 17
jkl = 99 : -1 : 88
ttt = 2 : (1/9) : 4
tpi = pi * [ 0:0.1:2 ];
Observation:
Problem #6 Extracting and/or inserting numbers into a vector is very easy to do. Consider the
following definition of xx:
xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ]
xx(4:6)
size(xx)
length(xx)
xx(2:2:length(xx))
Explain the results echoed from the last four lines of the above code.
Observation:
Problem #7 Observe the result of the following assignments:
yy = xx; yy(4:6) = pi*(1:3)
Now write a statement that will take the vector xx defined in part (b) and replace the even indexed
elements (i.e., xx(2), xx(4), etc) with the constant . Use a vector replacement, not a loop.
Observation:
Problem #8 Enter the following in MATLAB.
A = 



6 4
2 3
, B = 



6 2
1 6
Find AB, and A-1.
9. Matrices Handling:
size (A), size(x), A’,
ones (2, 3), zeros (2,3), eye (3), diag (d),
fliplr Flip matrices left-right
flipud Flip matrices up-down
repmat Replicate and tile an array
reshape Reshape array
rot90 Rotate matrix 90 degrees
rand (m, n) Generate a matrix of m x n with random values from 0 to 1
size Dimension of the matrix
length Returns length of vector Max ( Size ( Matrix ) )
>>diary mysession
>>diary off
>> save thissession
>> load thissession
>> who
>> whos
Plot & Stem
Plot sine and cosine wave in stem and plot.
plot:
PLOT Linear plot.
PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up.
subplot:
SUBPLOT Create axes in tiled positions.
H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc.
stem:
STEM Discrete sequence or "stem" plot.
STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value.
STEM(X,Y) plots the data sequence Y at the values specified in X.
title:
TITLE Graph title.
TITLE('text') adds text at the top of the current axis.
xlabel:
XLABEL X-axis label.
XLABEL('text') adds text beside the X-axis on the current axis.
ylabel:
YLABEL Y-axis label.
YLABEL('text') adds text beside the Y-axis on the current axis.
10.
Matlab Constructs:
For, While, If Then,
» x = 1:10;
» for i = 1:10
» y (i) = exp (x (i))
11
Script Files:
How to make and run it
How to make a functions
12.
Lab Questions
Problem # 1 Generate a Truth Table for XOR gate using relational and logical operators.
Observation:
Problem # 2 If x = [5 -3 18 4] and y = [-9 13 7 4], what will be the result of the following operations?
z = ~y > x
z = x & y
z = x | y
z = xor (x, y)
Observation:
Problem # 3 Type this matrix in MATLAB and answer the following questions:
A =
Create a 3x3 array B consisting of all elements in the second and last column of A.
Create a 2x4 array C consisting of all elements in the second and last row of A.
Create a 2x4 array D consisting of all elements in the first and last row of A.
Compute the length, size, and sum of array A.
Observation:
Problem # 4 Enter the matrices A = and B = and use MATLAB to compute -1.4A+3.1B and 2.67A-11.B.
Observation:
Problem # 5 Take two inputs from the user. If both inputs named as ‘x’ and ‘y’ are non-negative then compute z and w, and display its result.
z = √x + √y
w = log x – 3 log y
Observation:
Bonus Question:
Generate sine wave of varying frequencies and play them

0 comments:

Post a Comment