MATLAB and The File System
MATLAB and The File System
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
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 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:
Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization,
and numerical integration
Functions for integrating MATLAB based algorithms with external applications and languages,
such as C, C++, Fortran, Java, COM, and Microsoft Excel
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.
Zeros array.
zeros.
Identity matrix.
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
FLIPLR(X) returns X with row preserved and columns flipped in the left/right direction.
X= 123
456
becomes
321
654
Length of vector.
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
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
FLOOR(X) rounds the elements of X to the nearest integers towards minus infinity.
CEIL
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(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(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
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
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.
Y-axis label.
Graph title.
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
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
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
[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
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
[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
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.
HAMMING
Hamming window.
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.
[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
[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
CHEB1ORD
[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
[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