0% found this document useful (0 votes)
58 views

MATLAB and The File System

MATLAB is a technical computing environment and programming language used for scientific computing and data analysis. It allows matrix manipulations, plotting of data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. MATLAB contains an extensive library of mathematical functions and tools for algorithm and code development, data visualization, data analysis, and numeric computation.

Uploaded by

vinoth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

MATLAB and The File System

MATLAB is a technical computing environment and programming language used for scientific computing and data analysis. It allows matrix manipulations, plotting of data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. MATLAB contains an extensive library of mathematical functions and tools for algorithm and code development, data visualization, data analysis, and numeric computation.

Uploaded by

vinoth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

MATLAB

MATLAB is a environment for scientific computing that is ideal for computations that require
extensive use of arrays and graphical analysis of data.
Matlab basics

It is a interpreted language (no compiler); scripts can be saved as .m files

Array indices begin with 1 (compare to 0 in C or Java)

Arrays are passed by value to functions (no pointers)

Array elements are accessed with the format A(1,2) (compare to the format A[0][1] in C or
Java)

Powerful matrix mathematical functions are built-in (e.g., \ for Gaussian elimination or leastsquares solution methods for linear systems)

MATLAB and the file system


It is important to understand the relationship between the MATLAB operating environment and your
computer's file system structure because

This will simplify organizing your work

Object-oriented programming in MATLAB is implemented by defining object classes through


user-created directories

Directory structures can be navigated through unix-like commands.

Overview of the MATLAB Environment


MATLAB (Matrix Algebra) is the commercial Matrix Laboratory package which operates as an
interactive programming environment. It is a mainstay of the mathematics department software lineup
and is also available for PCs and Macintoshes
MATLAB is a high-level technical computing language and interactive environment for algorithm
development, data visualization, data analysis, and numeric computation. Using the MATLAB product,
you can solve technical computing problems faster than with traditional programming languages, such
as C, C++, and Fortran.
You can use MATLAB in a wide range of applications, including signal and image processing,
communications, control design, test and measurement, financial modeling and analysis, and
computational biology. Add-on toolboxes (collections of special-purpose MATLAB functions,
available separately) extend the MATLAB environment to solve particular classes of problems in these
application areas.
1

MATLAB provides a number of features for documenting and sharing your work. You can integrate
your MATLAB code with other languages and applications, and distribute your MATLAB algorithms
and applications.

Features include:

High-level language for technical computing

Development environment for managing code, files, and data

Interactive tools for iterative exploration, design, and problem solving

Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization,
and numerical integration

2-D and 3-D graphics functions for visualizing data

Tools for building custom graphical user interfaces

Functions for integrating MATLAB based algorithms with external applications and languages,
such as C, C++, Fortran, Java, COM, and Microsoft Excel

The MATLAB System


The MATLAB system consists of these main parts:

Desktop Tools and Development Environment


This part of MATLAB is the set of tools and facilities that help you use and become more productive
with MATLAB functions and files. Many of these tools are graphical user interfaces. It includes: the
MATLAB desktop and Command Window, an editor and debugger, a code analyzer, and browsers for
viewing help, the workspace, and folders.

Mathematical Function Library


This library is a vast collection of computational algorithms ranging from elementary functions, like
sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix
eigenvalues, Bessel functions, and fast Fourier transforms.

The Language
The MATLAB language is a high-level matrix/array language with control flow statements, functions,
data structures, input/output, and object-oriented programming features. It allows both "programming
2

in the small" to rapidly create quick programs you do not intend to reuse. You can also do
"programming in the large" to create complex application programs intended for reuse.
FUNCTIONS USED:
Elementary Matrices:
ONES

Ones array.

ONES(N) is an N-by-N matrix of ones.


ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones.
ONES(M,N,P,...) or ONES([M N P ...]) is an M-by-N-by-P-by-... array of ones.
ONES(SIZE(A)) is the same size as A and all ones.
ZEROS

Zeros array.

ZEROS(N) is an N-by-N matrix of zeros.


ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros.
ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-... array of

zeros.

ZEROS(SIZE(A)) is the same size as A and all zeros.


ZEROS with no arguments is the scalar 0.
EYE

Identity matrix.

EYE(N) is the N-by-N identity matrix.


EYE(M,N) or EYE([M,N]) is an M-by-N matrix with 1's on the diagonal and zeros elsewhere.
EYE(SIZE(A)) is the same size as A.
EYE with no arguments is the scalar 1.
EYE(M,N,CLASSNAME) or EYE([M,N],CLASSNAME) is an M-by-N matrix with 1's of
class CLASSNAME on the diagonal and zeros elsewhere.
LINSPACE

Linearly spaced vector.

LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1
3

and X2.
LINSPACE(X1, X2, N) generates N points between X1 and X2. For N < 2, LINSPACE
returns X2.
FLIPLR

Flip matrix in left/right direction.

FLIPLR(X) returns X with row preserved and columns flipped in the left/right direction.
X= 123
456

becomes

321
654

Basic Array Information:


LENGTH

Length of vector.

LENGTH(X) returns the length of vector X. It is equivalent to MAX(SIZE(X)) for non-empty


arrays and 0 for empty ones.

SIZE

Size of array.

D = SIZE(X), for M-by-N matrix X, returns the two-element row vector D = [M,N] containing
the number of rows and columns in the matrix. For N-D arrays, SIZE(X) returns a 1-by-N
vector of dimension lengths. Trailing singleton dimensions are ignored.
[M,N] = SIZE(X) for matrix X, returns the number of rows and columns in X as separate
output variables.
[M1,M2,M3,...,MN] = SIZE(X) for N>1 returns the sizes of the first N dimensions of the array X.
NUMEL

Number of elements in an array or subscripted array expression.

N = NUMEL(A) returns the number of elements, N, in array A.


N = NUMEL(A, VARARGIN) returns the number of subscripted elements, N, in A(index1,
index2, ..., indexN), where VARARGIN is a cell array whose elements are index1, index2, ...
indexN.
Elementary Math functions:
SIN

Sine of argument in radians.


4

SIN(X) is the sine of the elements of X.


COS

Cosine of argument in radians.

COS(X) is the cosine of the elements of X.


ABS

Absolute value.

ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex
modulus (magnitude) of the elements of X.
ANGLE
Phase angle.
ANGLE(H) returns the phase angles, in radians, of a matrix with complex elements.
REAL

Complex real part.

REAL(X) is the real part of X.


IMAG

Complex imaginary part.

IMAG(X) is the imaginary part of X.


FIX

Round towards zero.

FIX(X) rounds the elements of X to the nearest integers towards zero.


FLOOR

Round towards minus infinity.

FLOOR(X) rounds the elements of X to the nearest integers towards minus infinity.

CEIL

Round towards plus infinity.

CEIL(X) rounds the elements of X to the nearest integers towards infinity.


ROUND

Round towards nearest integer.

ROUND(X) rounds the elements of X to the nearest integers.


SIGN

Signum function.

For each element of X, SIGN(X) returns 1 if the element is greater than zero, 0 if it equals zero
and -1 if it is less than zero. For the nonzero elements of complex X, SIGN(X) = X ./ ABS(X).
Signal Generation:
5

SQUARE

Square wave generation.

SQUARE(T) generates a square wave with period 2*Pi for the elements of time vector T.
SQUARE(T) is like SIN(T), only it creates a square wave with peaks of +1 to -1 instead of
a sine wave.
SQUARE(T,DUTY) generates a square wave with specified duty cycle. The duty cycle, DUTY,
is the percent of the period in which the signal is positive.
SAWTOOTH

Sawtooth and triangle wave generation.

SAWTOOTH(T) generates a sawtooth wave with period 2*pi for the elements of time vector T.
SAWTOOTH(T) is like SIN(T), only it creates a sawtooth wave with peaks of +1 to -1 instead of
a sine wave.
SAWTOOTH(T,WIDTH) generates a modified triangle wave where WIDTH, a scalar parameter
between 0 and 1, determines the fraction between 0 and 2*pi at which the maximum occurs. The
function increases from -1 to 1 on the interval 0 to WIDTH*2*pi, then decreases linearly from 1
back to -1 on the interval WIDTH*2*pi to 2*pi. Thus WIDTH = .5 gives you a triangle wave,
symmetric about time instant pi with peak amplitude of one. SAWTOOTH(T,1) is equivalent to
SAWTOOTH(T).
TRIPULS

Sampled aperiodic triangle generator.

TRIPULS(T) generates samples of a continuous, aperiodic, unity-height triangle at the points


specified in array T, centered about T=0. By default, the triangle is symmetric and has width 1.
TRIPULS(T,W) generates a triangle of width W.
TRIPULS(T,W,S) allows the triangle skew S to be adjusted. The skew parameter must be in the
range -1 < S < +1, where 0 generates a symmetric triangle.
Convolution & Correlation:
CONV

Convolution and polynomial multiplication.

C = CONV(A, B) convolves vectors A and B. The resulting vector is length


LENGTH(A)+LENGTH(B)-1. If A and B are vectors of polynomial coefficients, convolving
them is equivalent to multiplying the two polynomials.
XCORR
Cross-correlation function estimates.
C = XCORR(A,B), where A and B are length M vectors (M>1), returns the length 2*M-1 crosscorrelation sequence C. If A and B are of different length, the shortest one is zero-padded. C will
be a row vector if A is a row vector, and a column vector if A is a column vector.
6

XCORR produces an estimate of the correlation between two random(jointly stationary)


sequences:
C(m) = E[A(n+m)*conj(B(n))] = E[A(n)*conj(B(n-m))]
It is also the deterministic correlation between two deterministic signals.
Plotting of signals:
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. If X is a scalar and Y is a vector,
disconnected line objects are created and plotted as discrete points vertically at X.
PLOT(Y) plots the columns of Y versus their index.
If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).
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. If Y is a matrix then each column is plotted as a separate series.
STEM(X,Y) plots the data sequence Y at the values specified in X.
In all other uses of PLOT, the imaginary part is ignored.
XLABEL

X-axis label.

XLABEL('text') adds text beside the X-axis on the current axis.


XLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the
specified properties of the xlabel.
YLABEL

Y-axis label.

YLABEL('text') adds text beside the Y-axis on the current axis.


YLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the
specified properties of the ylabel.
TITLE

Graph title.

TITLE('text') adds text at the top of the current axis.


TITLE('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the
specified properties of the title.
SUBPLOT

Create axes in tiled positions.


7

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.
Inputting & Outputting data:
INPUT

Prompt for user input.

R = INPUT('How many apples') gives the user the prompt in the text string and then waits for
input from the keyboard.The input can be any MATLAB expression, which is evaluated,
using the variables in the current workspace, and the result returned in R. If the user presses the
return key without entering anything, INPUT returns an empty matrix.
R = INPUT('What is your name','s') gives the prompt in the text string and waits for character
string input. The typed input is not evaluated; the characters are simply returned as a MATLAB
string.
DISP

Display array.

DISP(X) displays the array, without printing the array name. In all other ways it's the same as
leaving the semicolon off an expression except that empty arrays don't display.
If X is a string, the text is displayed.
DFT & IDFT Computation:
FFT

Discrete Fourier transform.

FFT(X) is the discrete Fourier transform (DFT) of vector X. For matrices, the FFT operation is
applied to each column. For N-D arrays, the FFT operation operates on the first non-singleton
dimension.
FFT(X,N) is the N-point FFT, padded with zeros if X has less than N points and truncated if it
has more.
IFFT

Inverse discrete Fourier transform.

IFFT(X) is the inverse discrete Fourier transform of X.


IFFT(X,N) is the N-point inverse transform.
Radix 2 FFT Computation:
DEC2BIN

Convert decimal integer to a binary string.


8

DEC2BIN(D) returns the binary representation of D as a string. D must be a non-negative integer


smaller than 2^52.
DEC2BIN(D,N) produces a binary representation with at least N bits.
BIN2DEC

Convert binary string to decimal integer.

X = BIN2DEC(B) interprets the binary string B and returns in X the equivalent decimal number.
If B is a character array, or a cell array of strings, each row is interpreted as a binary string.
Embedded, significant spaces are removed. Leading spaces are converted to zeros.
LOG2

Base 2 logarithm and dissect floating point number.

Y = LOG2(X) is the base 2 logarithm of the elements of X.


[F,E] = LOG2(X) for each element of the real array X, returns an array F of real numbers, usually
in the range 0.5 <= abs(F) < 1, and an array E of integers, so that X = F .* 2.^E. Any zeros in X
produce F = 0 and E = 0. This corresponds to the ANSI C function frexp() and the IEEE floating
point standard function logb().
Impulse response & Frequency Response:
IMPZ

Impulse response of digital filter

[H,T] = IMPZ(B,A) computes the impulse response of the filter B/A choosing the number of
samples for you, and returns the response in column vector H and a vector of times (or sample
intervals) in T (T = [0 1 2 ...]').
[H,T] = IMPZ(B,A,N) computes N samples of the impulse response. If N is a vector of integers,
the impulse response is computed only at those integer values (0 is the origin).
FREQS

Laplace-transform (s-domain) frequency response.

H = FREQS(B,A,W) returns the complex frequency response vector H of the filter B/A:
H(s) = B(s) = b(1) snb-1 + b(2) snb-2 + . + b(nb)
A(s)
a(1) sna-1 + a(2) sna-2 + . + a(na)
given the numerator and denominator coefficients in vectors B and A.
The frequency response is evaluated at the points specified in vector W (in rad/s). The magnitude
and phase can be graphed by calling FREQS(B,A,W) with no output arguments.
[H,W] = FREQS(B,A) automatically picks a set of 200 frequencies W on which the frequency
response is computed. FREQS(B,A,N) picks N frequencies.
9

FREQZ

Digital filter frequency response.

[H,W] = FREQZ(B,A,N) returns the N-point complex frequency response vector H and the Npoint frequency vector W in radians/sample of the filter:
H(ejw) = B(ejw) =
b(1) + b(2) e-jw + . + b(m+1) e-mjw
jw
A(e )
a(1) + a(2) e-jw + . + a(n+1) e-njw
given numerator and denominator coefficients in vectors B and A. The frequency response is
evaluated at N points equally spaced around the upper half of the unit circle. If N isn't specified,
it defaults to 512.
[H,W] = FREQZ(B,A,N,'whole') uses N points around the whole unit circle.
H = FREQZ(B,A,W) returns the frequency response at frequencies designated in vector W, in
radians/sample (normally between 0 and pi).
Pole Zero Plot:
TF2ZP

Transfer function to zero-pole conversion.

[Z,P,K] = TF2ZP(NUM,DEN) finds the zeros, poles, and gains:


(s-z1)(s-z2)...(s-zn)
H(s) = K
(s-p1)(s-p2)...(s-pn)
from a SIMO transfer function in polynomial form:
NUM(s)
H(s) =
DEN(s)
ZPLANE

Z-plane zero-pole plot.

ZPLANE(Z,P) plots the zeros Z and poles P (in column vectors) with the unit circle for reference.
Each zero is represented with a 'o' and each pole with a 'x' on the plot. Multiple zeros and poles
are indicated by the multiplicity number shown to the upper right of the zero or pole.
ZPLANE(Z,P) where Z and/or P is a matrix, plots the zeros or poles in different columns using
the colors specified by the axes Color Order property.
ZPLANE(B,A) where B and A are row vectors containing transfer function polynomial
coefficients plots the poles and zeros of B(z)/A(z). Note that if B and A are both scalars they will
be interpreted as Z and P.
Window functions:
RECTWIN

Rectangular window.

W = RECTWIN(N) returns the N-point rectangular window.


10

HAMMING

Hamming window.

HAMMING(N) returns the N-point symmetric Hamming window in a column vector.

HAMMING(N,SFLAG) generates the N-point Hamming window using SFLAG window


sampling. SFLAG may be either 'symmetric' or 'periodic'. By default, a symmetric window is
returned.
HANNING

Hanning window.

HANNING(N) returns the N-point symmetric Hanning window in a column vector. Note that the
first and last zero-weighted window samples are not included.
BLACKMAN

Blackman window.

BLACKMAN(N) returns the N-point symmetric Blackman window in a column vector.


BLACKMAN(N,SFLAG) generates the N-point Blackman window using SFLAG window
sampling. SFLAG may be either 'symmetric' or 'periodic'. By default, a symmetric window is
returned.
Butterworth & Chebychev filters:
BUTTORD

Butterworth filter order selection.

[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Butterworth filter that loses no more than Rp dB in the passband and has at least Rs dB of
attenuation in the stopband.Wp and Ws are the passband and stopband edge frequencies,
normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2
Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8]
Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency") to use
with BUTTER to achieve the specifications.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which
case Wp and Ws are in radians/second.
When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD.
BUTTER

Butterworth digital and analog filter design.

[B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the
filter coefficients in length N+1 vectors B (numerator) and A (denominator). The coefficients
are listed in descending powers of z. The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0
11

corresponding to half the sample rate.


If Wn is a two-element vector, Wn = [W1 W2], BUTTER returns an order 2N bandpass filter
with passband W1 < W < W2.
[B,A] = BUTTER(N,Wn,'high') designs a highpass filter.
[B,A] = BUTTER(N,Wn,'low') designs a lowpass filter.
[B,A] = BUTTER(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2].

CHEB1ORD

Chebyshev Type I filter order selection.

[N, Wp] = CHEB1ORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Chebyshev Type I filter that loses no more than Rp dB in the passband and has at least Rs dB of
attenuation in the stopband.
Wp and Ws are the passband and stopband edge frequencies, normalized from 0 to 1 (where 1
corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2
Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8]
Bandstop: Wp = [.1 .8], Ws = [.2 .7]
CHEB1ORD also returns Wp, the Chebyshev natural frequency to use with CHEBY1 to achieve
the specifications.
[N, Wp] = CHEB1ORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which
case Wp and Ws are in radians/second.

CHEBY1

Chebyshev Type I digital and analog filter design.

[B,A] = CHEBY1(N,R,Wp) designs an Nth order lowpass digital Chebyshev filter with R
decibels of peak-to-peak ripple in the passband. CHEBY1 returns the filter coefficients in length
N+1 vectors B (numerator) and A (denominator). The passband-edge frequency Wp must be
1.0 < Wp < 1.0, with 1.0 corresponding to half the sample rate. Use R=0.5 as a starting point, if
2.0 you are unsure about choosing R.
If Wp is a two-element vector, Wp = [W1 W2], CHEBY1 returns an order 2N bandpass filter
with passband W1 < W < W2.
[B,A] = CHEBY1(N,R,Wp,'high') designs a highpass filter.
[B,A] = CHEBY1(N,R,Wp,'low') designs a lowpass filter.
[B,A] = CHEBY1(N,R,Wp,'stop') is a bandstop filter if Wp = [W1 W2].
When used with three left-hand arguments, as in [Z,P,K] = CHEBY1(...), the zeros and poles are
returned in length N column vectors Z and P, and the gain in scalar K.
12

When used with four left-hand arguments, as in [A,B,C,D] = CHEBY1(...), state-space matrices
are returned.
CHEBY1(N,R,Wp,'s'), CHEBY1(N,R,Wp,'high','s') and CHEBY1(N,R,Wp,'stop','s') design
analog Chebyshev Type I filters. In this case, Wp is in [rad/s] and it can be greater than 1.0.

13

You might also like