Signal Processing in MATLAB Signal Processing in MATLAB February 2, - - PowerPoint PPT Presentation

signal processing in matlab signal processing in matlab
SMART_READER_LITE
LIVE PREVIEW

Signal Processing in MATLAB Signal Processing in MATLAB February 2, - - PowerPoint PPT Presentation

Signal Processing in MATLAB Course February 1998 Signal Processing in MATLAB Signal Processing in MATLAB February 2, 1998 Tom Krauss PhD Student in Elec. Engineering On-campus Representative of The MathWorks, Inc. krauss@ecn.purdue.edu


slide-1
SLIDE 1

Signal Processing in MATLAB Course February 1998

Signal Processing in MATLAB Signal Processing in MATLAB February 2, 1998 Tom Krauss PhD Student in Elec. Engineering On-campus Representative of The MathWorks, Inc. krauss@ecn.purdue.edu

slide-2
SLIDE 2

Signal Processing in MATLAB Course February 1998

Outline Outline

  • Built-in MATLAB support for Signal Processing
  • Overview of the Signal Processing Toolbox
  • Some new MATLAB 5 Signal Processing features

How to Start MATLAB How to Start MATLAB

  • Login to UNIX prompt
  • type

matlab

slide-3
SLIDE 3

Signal Processing in MATLAB Course February 1998

Signal Representation Signal Representation

  • Weighted Sum of Sinusoids

x[n] = a1*sin(w1*n+phi1) + a2*sin(w2*n+phi2) + a3*sin(w3*n+phi3), 0 <= n <= N-1

  • Implementation 1 - non-vectorized, ala C or FORTRAN

N = 100; a = [1 1/sqrt(2) 0.5]; w = [1 2 3]*.051*2*pi; phi = [0 0 0]; x = zeros(N,1); for n = 0:N-1 for k = 1:3 x(n+1) = x(n+1) + a(k)*sin(w(k)*n + phi(k)); end end

slide-4
SLIDE 4

Signal Processing in MATLAB Course February 1998

Vectorization Vectorization

  • Implementation 2 - vectorized
  • Plotting and Comparison
  • plot(0:N-1,[x x1])
  • norm(x-x1)

n = 0:N-1; x1 = sin(n’*w + phi(ones(1,N),:))*a’;

10 20 30 40 50 60 70 80 90 100
  • 2
  • 1.5
  • 1
  • 0.5
0.5 1 1.5 2
slide-5
SLIDE 5

Signal Processing in MATLAB Course February 1998

How did we do that? How did we do that?

x[n] = sin(

w1*0 + phi1 w2*0 + phi2 w3*0 + phi3 w1*1 + phi1 w2*1 + phi2 w3*1 + phi3 w1*2 + phi1 w2*2 + phi2 w3*2 + phi3 w1*3 + phi1 w2*3 + phi2 w3*3 + phi3 . . . . . . . . . w1*(N-2)+phi1 w2*(N-2)+phi2 w3*(N-2)+phi3 w1*(N-1)+phi1 w2*(N-1)+phi2 w3*(N-1)+phi3

) * a’

sin(n’*w + phi(ones(1,N),:))*a’

  • dash ’ is the (conjugate) transpose of a matrix
  • Outer-product n’*w is N-by-3 matrix - think of it as a weighted replication
  • f row w with elements of n as weights
  • Replicated row phi into matrix [phi; phi; ... phi] using : indexing notation
  • Sum and sine are element-wise operations
  • Matrix Multiplication is a linear combination of column vectors
slide-6
SLIDE 6

Signal Processing in MATLAB Course February 1998

Functions - sos.m Functions - sos.m

function x = sos(N,a,w,phi) % SOS Weighted sum of sinusoids % Inputs: % N - length of sequence % a - vector of amplitudes % w - vector of frequencies (in radians) % phi - vector of phases % Uses Implementation 1 (non vectorized) for n = 0:N-1 x(n+1) = 0; for k = 1:3 x(n+1) = x(n+1) + a(k)*cos(w(k)*n + phi(k)); end end

  • Create a file entitled sos.m containing the following text:
slide-7
SLIDE 7

Signal Processing in MATLAB Course February 1998

About Functions About Functions

  • Help is automatic
  • The help for the function is everything after the ‘function’ line

that starts with % (the comment character), up to the first line that is not a comment.

  • To get help on our function, (or ANY function in MATLAB),

type ‘help sos.m’

  • Input and output arguments

function x = sos(N,a,w,phi)

  • This line defines N, a, w and phi as input arguments, and x as
  • utput argument
  • These arguments are local to the function sos
  • We can have a variable of the same name in the calling

workspace

  • Don’t exist before or after function execution
slide-8
SLIDE 8

Signal Processing in MATLAB Course February 1998

Another Function - sos1.m Another Function - sos1.m

function x1 = sos1(N,a,w,phi) % SOS1 Weighted sum of sinusoids % Inputs: % N - length of sequence % a - vector of amplitudes % w - vector of frequencies (in radians) % phi - vector of phases % Uses Implementation 2 (vectorized) n = 0:N-1; x1 = cos(n’*w + phi(ones(1,N),:))*a’;

slide-9
SLIDE 9

Signal Processing in MATLAB Course February 1998

Timing Comparison Timing Comparison

  • Use tic and toc:

>> tic, for i=1:100, x=sos(N,a,w,phi); end, t=toc t = 2.4385 >> tic, for i=1:100, x1=sos1(N,a,w,phi); end, t1=toc t1 = 0.11287 >> t/t1 ans = 21.605 FACTOR OF 20 SPEED INCREASE BY USING VECTORIZATION

slide-10
SLIDE 10

Signal Processing in MATLAB Course February 1998

Noise Signals Noise Signals

  • There are two functions, rand and randn, which generate

matrices of random numbers

  • x=rand(m,n) creates m-by-n matrix of independent, uniformly

distributed real numbers on interval [0,1]

  • x=randn(m,n) creates m-by-n matrix of independent, Normally

distributed real numbers with mean 0 and variance 1

  • rand and randn have internal states that determine the numbers
  • produced. This state is initialized when you start MATLAB, and

you can reset it at will. Example:

>> rand(1,3) ans = 0.95013 0.23114 0.60684 >> rand('state',0) >> rand(1,3) ans = 0.95013 0.23114 0.60684

slide-11
SLIDE 11

Signal Processing in MATLAB Course February 1998

More on Noise More on Noise

  • Note that the random number generators have been improved in

MATLAB 5. The old random number generators are still present and are activated by the command rand(‘seed’,0). It is recommended that you remove any references to ‘seed’ from all your old MATLAB code so that you can use the improved generators.

  • Example:
  • High frequency noise

h = remez(40,[0 .4 .6 1],[0 0 1 1]); noise = 5*randn(2*N,1); noise = filter(h,1,noise); noise = noise(end-N+1:end); % make it length N More about these functions later! Note use of MATLAB 5 feature “end”

slide-12
SLIDE 12

Signal Processing in MATLAB Course February 1998

Additive Noise Example Additive Noise Example

x = x + noise; plot(0:N-1,x) The signal is completely buried!

10 20 30 40 50 60 70 80 90 100

  • 6
  • 4
  • 2

2 4 6 8 10

slide-13
SLIDE 13

Signal Processing in MATLAB Course February 1998

Attempt to remove noise Attempt to remove noise

  • Smoothing with running

average of 7 points y[n] = 1/7 * (x[n] + x[n-1] + ... + x[n-6])

  • “Exponential Smoothing”

where present sample is forced to be similar to previous sample y[n] = 0.9*y[n-1] + 0.1*x[n]

10 20 30 40 50 60 70 80 90 100
  • 2
  • 1.5
  • 1
  • 0.5
0.5 1 1.5 2 Averaged Noisy Signal 10 20 30 40 50 60 70 80 90 100
  • 0.8
  • 0.6
  • 0.4
  • 0.2
0.2 0.4 0.6 0.8 Exponentially Smoothed Noisy Signal
slide-14
SLIDE 14

Signal Processing in MATLAB Course February 1998

Digital Filtering with Digital Filtering with filter filter

  • Both these smoothing operations and more can be implemented

with filter:

y = filter([1 1 1 1 1 1 1]/7,1,x); % moving average FIR y = filter(.1,[1 -.9],x); % exponential smoothing IIR

FILTER One-dimensional digital filter. Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the standard difference equation: a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)

  • a(2)*y(n-1) - ... - a(na+1)*y(n-na)
slide-15
SLIDE 15

Signal Processing in MATLAB Course February 1998

Transforms: FFT Transforms: FFT

A = 1; W = exp(-j*pi*.05); M = 40; z = A* W.^(-(0:M-1)); zplane([],z.’) Computes Z-transform at evenly-spaced points around the unit circle

  • 1
  • 0.5
0.5 1
  • 1
  • 0.5
0.5 1 Real part
slide-16
SLIDE 16

Signal Processing in MATLAB Course February 1998

Transforms: CZT Transforms: CZT

A = .8 *exp( j*pi/6); W = .995*exp(-j*pi*.05); M = 91; z = A* W.^(-(0:M-1)); zplane([],z.’) Domain is a spiral or “chirp” in the Z-plane

  • 1.5
  • 1
  • 0.5
0.5 1 1.5
  • 1
  • 0.5
0.5 1 Real part
slide-17
SLIDE 17

Signal Processing in MATLAB Course February 1998

FFT vs. CZT FFT vs. CZT

fft(x,M) Uses prime factor algorithm which is fastest when transform length is a power of 2. czt(x,M,W,A) Uses next greatest power–of–2 FFT for fast computation. Timing Example >> x=randn(2027,1); % a prime length sequence >> tic, fft(x); toc elapsed_time = 0.18934 >> tic, czt(x); toc elapsed_time = 0.11305 Note: both fft and czt work column-wise on matrices - very useful

slide-18
SLIDE 18

Signal Processing in MATLAB Course February 1998

Zoom Transform Application of Chirp Z- Zoom Transform Application of Chirp Z- transform transform

f1 = .4; f2 = .7; [b,a] = ellip(5,.1,40,[f1 f2]); M = 1000; A = exp(j*pi*f1); W = exp(-j*pi*(f2-f1)/(M-1)); H = czt(b,M,W,A)./czt(a,M,W,A); f = linspace(f1,f2,M); plot(f,abs(H))

0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.988 0.99 0.992 0.994 0.996 0.998 1 1.002 Frequency

Efficiently computes frequency response in passband only

slide-19
SLIDE 19

Signal Processing in MATLAB Course February 1998

Signal Processing Toolbox Overview - Signal Processing Toolbox Overview - FIR Filter Design FIR Filter Design

  • FIR example: h = remez(40,[0 .4 .6 1],[0 0 1 1]);

freqz(h)

0.2 0.4 0.6 0.8 1

  • 2000
  • 1500
  • 1000
  • 500

500 Normalized frequency (Nyquist == 1) 0.2 0.4 0.6 0.8 1

  • 150
  • 100
  • 50

50 Normalized frequency (Nyquist == 1)

High-pass Finite Impulse Response equiripple filter

slide-20
SLIDE 20

Signal Processing in MATLAB Course February 1998

Signal Processing Toolbox Overview - Signal Processing Toolbox Overview - IIR Filter Design IIR Filter Design

  • IIR example: [b,a] = ellip(5,.1,40,[.4 .7]);

freqz(b,a)

0.2 0.4 0.6 0.8 1

  • 400
  • 200

200 400 Normalized frequency (Nyquist == 1) 0.2 0.4 0.6 0.8 1

  • 300
  • 200
  • 100

Normalized frequency (Nyquist == 1)

Band-pass Infinite Impulse Response equiripple (elliptic) filter

slide-21
SLIDE 21

Signal Processing in MATLAB Course February 1998

Signal Processing Toolbox Overview - Signal Processing Toolbox Overview - Other Filter Design Techniques Other Filter Design Techniques

  • FIR
  • Parks-McClellan (minimax) remez
  • Least Squares firls
  • Windowed method fir1, fir2
  • Constrained Least Squares fircls, fircls1
  • Complex, nonlinear phase cremez
  • IIR
  • Butterworth, Chebyshev Type I and II, Elliptic

butter, cheby1, cheby2, ellip

  • Piecewise linear magnitude approx. yulewalk
  • Arbitrary Mag. & Phase invfreqz
  • Generalized Butterworth (lowpass only) maxflat
slide-22
SLIDE 22

Signal Processing in MATLAB Course February 1998

Other M-files in the Signal Proc. Toolbox Other M-files in the Signal Proc. Toolbox

  • Spectral Analysis - estimate Power Spectral Density
  • Welch’s method (overlapped modified periodograms) psd
  • Maximum entropy method (AR modeling) pmem
  • MUSIC (eigenanalysis based) pmusic
  • Multitaper (discrete prolate spheroidal sequences) pmtm
  • Parametric Modeling - find AR or ARMA model for signal
  • AR model via autocorrelation technique lpc
  • ARMA prony
  • iterative ARMA stmcb
slide-23
SLIDE 23

Signal Processing in MATLAB Course February 1998

and More M-files in the Signal Proc. and More M-files in the Signal Proc. Toolbox Toolbox

  • Graphic User Interface (GUI) sptool
  • Other
  • Correlation functions xcorr, xcov
  • Hilbert transform hilbert
  • Spectrogram specgram
  • Resampling resample, upfirdn
  • Alternate Filtering schemes filtfilt, fftfilt
slide-24
SLIDE 24

Signal Processing in MATLAB Course February 1998

upfirdn - Efficient Multirate FIR Filter bank upfirdn - Efficient Multirate FIR Filter bank Implementation Implementation

  • Syntax:

y = upfirdn(x,h,p,q)

  • x and h can be arrays to implement BANKS of filters

P Q FIR H x[n] y[n]

slide-25
SLIDE 25

Signal Processing in MATLAB Course February 1998

load mtlb f = (200:5:1000); % frequencies in Hz w = hamming(256); h = exp(-j*(0:255)’*f*2*pi/Fs); % DFT filter bank s = upfirdn(mtlb,h,1,100); % compute DFT every 100 samples t = (0:100:length(mtlb)+256)/Fs; imagesc(t,f,abs(s.')), axis xy

upfirdn Example: Compute spectrogram upfirdn Example: Compute spectrogram

  • ver range of frequencies
  • ver range of frequencies

time (seconds) Magnitude of Spectrogram 0.1 0.2 0.3 0.4 0.5 200 300 400 500 600 700 800 900 1000

slide-26
SLIDE 26

Signal Processing in MATLAB Course February 1998

New in MATLAB 5 New in MATLAB 5

  • Multidimensional Arrays
  • fftn - N-dimensional FFT
  • convn - N-dimensional convolution (filtering)
  • Non-double data containers - very useful for image and video

work

  • uint8, int8
  • Filter works on array inputs - filters each column
slide-27
SLIDE 27

Signal Processing in MATLAB Course February 1998

Other Signal Processing Relevant Other Signal Processing Relevant Toolboxes Toolboxes

  • Image Processing images
  • Higher Order Spectral Analysis (HOSA) hosa
  • Wavelets wavelet
  • Statistics stats
  • System Identification ident
  • Communications comm
  • Optimization optim
  • Symbolic Math symbolic
  • Control Systems control
  • Neural Networks nnet
  • Fuzzy Logic fuzzy

For a list of available functions, type help signal, help wavelet, etc.

slide-28
SLIDE 28

Signal Processing in MATLAB Course February 1998

Some Symbolic Basics in MATLAB 5 Some Symbolic Basics in MATLAB 5

  • Define some symbolic variables using ‘syms’ command

» syms x a T w » int(exp(a*x),0,T) ans = 1/a*exp(T*a)-1/a » solve(x^3+a) ans = [ (-a)^(1/3)] [ -1/2*(-a)^(1/3)-1/2*i*3^(1/2)*(-a)^(1/3)] [ -1/2*(-a)^(1/3)+1/2*i*3^(1/2)*(-a)^(1/3)] » int(exp(sqrt(-1)*w*x),-T/2,T/2) ans =

  • i/a*exp(1/2*i*T*w)+i/a*exp(-1/2*i*T*w)

» y = simplify(ans) y = 2*sin(1/2*T*w)/w »limit(y,w,0) ans = T

INTEGRATE SOLVE EQUATION(S) FOURIER TRANSFORM OF SQUARE PULSE SIMPLIFY EXPRESSIONS TAKE LIMITS