You are on page 1of 23

An Introduction to Matlab for DSP

Brady Laska

SYSC 4405
Carleton University

September 13, 2007

SYSC 4405

An Introduction to Matlab for DSP

Overview

1 2 3 4 5

Matlab background Basic Matlab DSP functions Coding for speed Demos

SYSC 4405

An Introduction to Matlab for DSP

octave. student editions $ + toolboxes $ Use GNU Octave → Compatible syntax → Free download (Windows.org → Octave-forge add-on contains most functions from signal processing toolbox → All in-class examples will run in both Matlab and Octave (possibly with modification) SYSC 4405 An Introduction to Matlab for DSP . Linux) from http://www. Mac.Accessing Matlab Labs on campus Purchase it → commercial editions $$$.

number-crunch. easy to transform data. export. powerful plotting/graphics Implement/prototype/test algorithms → vast library of built-in functions. curve fitting Visualize and explore data → interactive. modelling SYSC 4405 An Introduction to Matlab for DSP .Uses of Matlab in DSP Analyze data → import. integration with Simulink → easy to map algebra of DSP algorithms to Matlab syntax Simulation. available add-on toolboxes.

Matlab is A programming language An interactive numerical computation environment An interactive development environment A programming library and API A graphics system (for plotting. GUI creation) SYSC 4405 An Introduction to Matlab for DSP .

char. structure. switch-case Datatypes Standard datatypes are scalar. string. cell array Arrays (vector/matrix) are 1-based and automatically re-size SYSC 4405 An Introduction to Matlab for DSP . boolean. break Branching: if-elseif-else.Matlab the programming language Typical programming constructs Looping: for. while. vector and matrix of double → also: integer.

operator overloading SYSC 4405 An Introduction to Matlab for DSP .’ are optional Interfaces with other languages (C. FORTRAN.Matlab the programming language Other language aspects Case sensitive Dynamically typed Interpreted (mostly) Whitespace and terminating ‘. Java) Object Oriented → classes.

m SYSC 4405 An Introduction to Matlab for DSP .Navigating The Matlab prompt supports common Linux and Windows shell commands pwd cd newdirectory ls/dir !command current directory path change directory lists files in current directory executes command in the system shell example: >>!grep fft *.

Housekeeping When you’re lost who. history list variables and sizes prints usage and documentation scans documentation for key and prints matches Cleaning up clear x clc.clf clear variable x.whos help commandname lookfor key Tab completion. or use clear all clears the console and current figure respectively SYSC 4405 An Introduction to Matlab for DSP .

tan.exp.pow. SYSC 4405 An Introduction to Matlab for DSP .log.sinc. exponential). Arithmetic operators Matrix operators perform the linear-algebra-defined matrix operation (matrix multiplication..log10. Array operators work element-by-element and are indicated by adding a period before the operator.sqrt.log2.Mathamathical operations Functions All functions that are frequently used in DSP are included and named as you’d expect: sin. . .cos.

j both equal −1 (watch when using index variables and complex numbers in the same function) → example: creating a complex number >>x = 1 + 2j Functions for manipulating complex numbers: real.cart2pol.angle.Complex numbers Built-in complex number suppport √ Keywords i.conj.abs.pol2cart SYSC 4405 An Introduction to Matlab for DSP .imag.

3]. mean. SYSC 4405 An Introduction to Matlab for DSP .2. Column vectors: >>y = [1. Default for signals. fft. that take a vector input will evaluate each column of a matrix as a separate signal. Default for range operations such as x = 1:10. sum.Vectors Unlike other programming languages.2. Matlab has two distinct types of 1-dimensional arrays (vectors). etc.3]. Row vectors: >>x = [1. Functions such as plot.

N) zeros(M. aka white noise matrix of uniform random numbers on [0. SYSC 4405 An Introduction to Matlab for DSP .N) diag(x) matrix of ones matrix of zeros N × N identity matrix matrix of zero-mean unit variance Gaussian random numbers.Common matrix creation commands ones(M.N) eye(N) randn(M.N) rand(M. 1] matrix with x along the diagonal Note that ones(1E6) will attempt to create a 106 × 106 matrix. not a 106 × 1 vector.

Subsets defined by logical matrix or index set. Elements in a matrix can be accessed using subscripts or linear indices.Matrix Indexing Most functions can operate on either scalar. vector. Functions sub2ind and ind2sub are used to convert back and forth. clever indexing allows functions to be applied to a select subset of your data. SYSC 4405 An Introduction to Matlab for DSP . or matrix.

<.∼=.|| accept and return scalars and are used for control statements.|. SYSC 4405 An Introduction to Matlab for DSP .xor.| accept and return matrices while &&. Logical vectors can be collapsed to scalars for control statements using any and all. Logical matrices can be combined using Boolean algebra and logical operators: ==.Indexing methods Logical matrix A matrix of logical ones and zeros (or boolean datatype in new versions) same size as vector/matrix. Note that &.&.>.

Indexing methods Index set A matrix of linear indices in the range 1:prod(size(A)). unique. Sets created using find(Boolean statement ). setdiff. setxor. SYSC 4405 An Introduction to Matlab for DSP . Index sets can be combined using set operations: union. Expression is evaluated at the indices in the set. intersect.

A.fliplr returns the size of the matrix convert any matrix or vector to a column vector conjugate and non-conjugate transpose. reshapes a matrix. Generally use conjugate transpose in DSP.Matrix reshaping size(A) A(:) A’.’ reshape repmat flipud. traverses column-wise useful for adding/multiplying a vector to each row/column of a matrix flips the vector/matrix SYSC 4405 An Introduction to Matlab for DSP .

roots poly zplane residuez fdatool fvtool find the zeros of a polynomial construct a polynomial from a set of roots plot poles and zeros on the complex plane z-transform partial fraction expansion filter design and analysis tool filter visualization tool SYSC 4405 An Introduction to Matlab for DSP .Working with transfer functions Matlab has many functions for analyzing and constructing filters and transfer functions.

Other useful DSP functions filter(B.A. blackman kaiser. hamming.x) fftfilt(B.x) conv buffer windows xcorr FIR and IIR filtering FIR filtering using the FFT discrete convolution (polynomial multiplication) divide a signal into (possibly overlapping) frames hanning. bartlett auto and cross-correlation SYSC 4405 An Introduction to Matlab for DSP .

and more amenable to parallelization.Vectorizing Definition Vectorization: replacing loops with calls to vector functions. more readable. Vectoring can still make your code faster. Matlab now has JIT acceleration so code using loops with built-in functions can be as fast as vectorizing. Code says what you want to do. Vectorization makes extensive use of index sets and logical matrices. not how to do it. Matlab used to be entirely interpreted and loops were very slow. SYSC 4405 An Introduction to Matlab for DSP .

profile report and tic. SYSC 4405 An Introduction to Matlab for DSP . write your function in C or FORTRAN with Mex interfaces.Coding for speed As usual. toc to time code execution.zeros to intialize vectors/matrices. Mex functions If you really need speed. Profile Use profile on. Pre-allocate Use ones. Very important. especially for big matrices. don’t sacrifice readability and clarity for speed.

SYSC 4405 An Introduction to Matlab for DSP . modulate it using a carrier sinusoid. Example Noise removal Identify signal components. remove noise to recover signal.Demos Example AM Modulation Create a baseband signal.

Roth and A. Online at: http: //www.cs. S.HTM.rit.brown. SYSC 4405 An Introduction to Matlab for DSP .edu/courses/ csci1430/MatlabDemo.References P. Venkataraman.html. Online at: http://www. “Matlab: A Fast Paced Introduction”. Balan.edu/∼pnveme/Matlab Course/DEFAULT. “Introduction to Matlab (Demo)”.