You are on page 1of 4

T-61.

3015 DSP 2013

187/190

MATLAB

T-61.3015 Digital Signal Processing and Filtering


Some commands in Matlab, spring 2013. Corrections and comments to t613015@cis.hut.fi, thank
you!
See also Matlab exercises found in Noppa and list of course material in http://www.cis.hut.fi/
Opinnot/T-61.3015/Matlab/

Some commands and demos in Matlab


Command

Description

General commands and notations


quit
terminates Matlab session
help
lists all function directories available
help function
gives help on function, e.g. input arguments
doc function
gives help and examples on function
type function
shows the code of function, which are not build-in
pause
waits until the user presses any key
pause(s)
waits s seconds
more
pauses scrolling
who,whos
lists all variables
diary
copies user commands into a file
ver
lists all toolboxes (versions) available
! OS-command
calls command in operating system
%
starts the comment till the end of line
;
does not print anything on screen
disp
prints matrices or text nicely on screen
size
gives dimensions of the matrix (vector)
length
gives the length of the vector
fliplr, flipud
flips the order of items in an array, x[n] x[n]
xstart : intercreates a vector starting from xstart with interval interval and ending
val : xstop
at least in xstop
linspace
creates a linearly spaced vector
File I/O
load
opens a MAT binary file containing variables
load -ascii
opens a formatted ASCII file
save
saves the variables into a MAT binary file
textread
reads a formatted text file into Matlab workspace
dlmread
reads a formatted file of numbers into Matlab workspace
fopen
opens a file
fclose
closes a file
fprintf
writes into a file
fprintf(1,...)
writes into a file handle 1 = on screen
sprintf(1,...)
writes into screen or code
Elementary functions
+
*
scalar / matrix product
/

power
continued on next page

T-61.3015 DSP 2013

188/190

MATLAB

continued from previous page


Command
Description

.
.
mod
rem
cos
sin
atan
atan2
sqrt
exp
log
log2
log10
abs
angle
real
imag
roots
sum
mean
std
var
Plotting
plot
stem
clf
cla
shg
close all
subplot
grid on
title
xlabel
ylabel
legend
axis
print
saveas
semilogx
semilogy
loglog
get
set
findobj
Audio
wavread
soundsc, sound

conjugate transpose
tranpose
itemwise operation, e.g. x.*y, sum(x.^
2)
modulus after division
remainder after division

arcus tangent in quadrants

absolute value of a complex number, e.g. |H(ej )|


angle of a complex number, e.g. H(ej )
real part of a complex number
imaginary part of a complex number
calculates roots of a polynomial
sums elements column-wise, sum(x,2) row-wise
computes mean column-wise, mean(x,2) row-wise
standard deviation
variance
plots continuous signals
plots sequences
clears the current figure
clears the current axis
the active window is brought on top
closes all windows
creates several axis in a window
inserts a grid on figure
title for a figure
title for x-axis
title for y-axis
creates a legend for a figure
zoom the axis, axis([xmin xmax ymin ymax]), or axis equal
exports a figure into a file or prints it to a printer
exports a figure into a file
plot with logarithmic x axis
plot with logarithmic y axis
plot with logarithmic xy axis
gets values of an object, e.g. p = plot(..); get(p)
sets values of an object, e.g. p = plot(..); set(p,LineWidth,2)
finds objects with certain properties
reads a WAV file into Matlab
plays a vector as sound (sc = scaled)
continued on next page

T-61.3015 DSP 2013

189/190

MATLAB

continued from previous page


Command
Description
wavwrite
writes a WAV file from Matlab
DSP functions, see Signal Processing Toolbox
fft
DFT, fast Fourier transform
ifft
IDFT, inverse Fourier transform
spectrogram
spectrogram
dftmtx
computes a matrix W for DFT
unwrap
eliminates jumps in phase angles
conv
linear convolution of two sequences
conv
polynomial multiplication
filter
filters signal x with a given filter
impz
impulse response for a discrete-time finite-dimensional system
freqz
draws frequence response, magnitude and phase response
zplane
plots a pole-zero-diagram
residuez
partial-fraction expansion of z-trasform H(z)
tf2zp
converts transfer function to corresponding zeros and poles
tf2sos
converts transfer function to corresponding set of second-order systems
buttord,
filter design: estimates the minimum order for fulfilling specifications
cheb1ord, ellipord
butter, cheby1,
filter design: computes filter coefficients
ellip
fir1
one of FIR filter design procedures, window method
fir2
frequency sampling method for FIR filter design
firpm, firpmord
one of FIR filter design procedures, Parks-McClellan
firls
FIR filter design by least-squares
T-61.3015 functions and scripts, see http://www.cis.hut.fi/Opinnot/T-61.3015/Matlab/
setfontsize
modify font sizes of figures
a2dR
quantization of bits
delta
[n]
mu
[n]
tf2latex
prints transfer function on screen
imp2latex
prints impulse response on screen
phasor.m
phasor example, [L0165][T5]
demoFIRwindowDB.m example on window functions
Control loops
for .. end
if .. end
switch .., case
.., otherwise,
end

Note that when using Octave (with SourceForge packages http://octave.sourceforge.net), spectrogram specgram, firpmord remezord, firpm remez.

T-61.3015 DSP 2013

190/190

MATLAB

Read an audio file, plot and listen to it.


0.5

[x, fT, nbits] = wavread(vowel_o.wav);


M = length(x);
t = [0 : M-1]/fT;
plot(t, x); grid on;
title(My vowel /o/); xlabel(time (s));
axis([0.1 0.14 -0.8 0.8]);
soundsc(y, fT);
Compute a FFT X(ej ) from the whole sequence and plot
the spectrum.
xF = fft(x);
M = length(xF);
w = fT * [0 : M-1]/M;
plot(w, 20*log10(abs(xF))); grid on;
title(Spectrum); xlabel(freq. (Hz));
set(gca, XLim, [0 fT/2]);

0.5
0.12

0.13

0.14

300

200

100

0
0

2000

4000

0
20
Magnitude (dB)

Design an elliptic IIR lowpass filter with cut-off 0.3 and plot
the frequency response |H(ej )| and a pole zero diagram.

40
60
80
100

1000

2000

3000
Frequency (Hz)

4000

5000

1000

2000

3000
Frequency (Hz)

4000

5000

Phase (degrees)

Wp = 0.3; Ws = 0.36;
Rp = 0.5; Rs = 40;
[N, Wn] = ellipord(Wp, Ws, Rp, Rs);
[B, A] = ellip(N, Rp, Rs, Wn);
freqz(B, A);
zplane(B, A);

100

200

300

400

Wp = 3300 / (16000/2);
N = 15;
[B, A] = fir1(N, Wp, hamming(N+1));
[H, w] = freqz(B, A, 1024, 16000);
plot(w, abs(H));

0.8
0.6
0.4

Imaginary Part

Design a FIR filter, cut-off at 3300 Hz and fT = 16 kHz, with


Hamming window of order 15, plot the frequency response
|H(ej )|.

0.2
0
0.2
0.4
0.6
0.8
1
1

0.5

0
Real Part

0.5

You might also like