lecture on matlab with simulations - oakland universityganesan/old/courses/sys595 f06/arun... ·...
TRANSCRIPT
9/27/2006 School of Engineering and Computer Science
1
Lecture on Matlab with Simulations
Dr.Subra GanesanProfessor in School of Engineering and Computer ScienceOakland University
9/27/2006 School of Engineering and Computer Science
2
What is Matlab
Matlab is a matrix laboratory.In particular Matlab is based on Matrices and vector algebra.we want to pass a vector or any element, it is stored in the form of a Matrix.
ex 1 0 00 0 0 0 0 1
9/27/2006 School of Engineering and Computer Science
3
9/27/2006 School of Engineering and Computer Science
4
What are its uses
There are two main uses1.To process an signal
--time domain and frequency domain analysis
spectral analysis
filtering
2.To process an imagegeometric operations
neighborhood and block operations
linear filtering
Fourier Transform, FFT, Discrete Cosine Transform
image enhancement
binary image operations
9/27/2006 School of Engineering and Computer Science
5
M - files
We all know about matlab M-file-file is the place where we write matlab programs.M-files are the macros of Matlab commamd that are stored as ordinary textfiles ,with an extension of ‘m’ i.e.,filename.mM-files can either be a function with I/p and o/p variables or list of commands.
9/27/2006 School of Engineering and Computer Science
6
All the loops which are used in C can be used here too, but with some modification.
Suppose a program requires an input to be given at the output, then a statement can be made as
T=input(‘input value of T;’)Comparisions are also made as in C.
9/27/2006 School of Engineering and Computer Science
7
Fourier Analysis
9/27/2006 School of Engineering and Computer Science
8
Signal Processing
9/27/2006 School of Engineering and Computer Science
9
Signal processing in Matlab ,though not very difficult, requires knowledge of programming.The programming is Very similar to ‘C’ Programming, but differs slightly in the orientation (style of programming)
9/27/2006 School of Engineering and Computer Science
10
Mathematically we know 1+2=3.This we write in matlab program as a=1+2
Now since a is defined ,we define b as b=2*a;
If we want to compute the value of a number which is too big to fitin screen ,then it is written in matlab as 1+2+3+4+5+6+7+8+…
…9+10;=55
9/27/2006 School of Engineering and Computer Science
11
Pre defined functionsMATLAB has many Pre defined functions.But if you know the method ,you are free to use it.i = sqrt(-1)=jPi=3.1416There are many other predefined functions
Abs abs(y) y=2*(1+4j)Angle(y)If a=3cos(a)Sin(a)Exp(a) in specific exp(y) which can be verified by eulers
formulae e^2cos(Img value)+je^2sin(imgValue)
9/27/2006 School of Engineering and Computer Science
12
We know that Matlab is a software which stores every thing in form of matrices. But how do we define matrices? To define matrix which is in slide 1 just type [100;000;100]
But can you store any variable in form of matrix?
9/27/2006 School of Engineering and Computer Science
13
Define VectorsVectors are also easy to define in matlab.So how do we defineSuppose’ V ‘ is a vector of any 4 elements ,say 1,2,3,and 4
Then V = [1 2 3 4]
Suppose suddenly we think of including the fifth element in ‘V’ to be 5
then type V(5) = 5
In matlab subscript starts from ‘1’ not ‘0’,as in ‘C’language
9/27/2006 School of Engineering and Computer Science
14
The are two main methods for creating a VectorFirst Method:V = [1 2 3 4 5]
Second method:t = -1:.1:1 V = sint(t)
9/27/2006 School of Engineering and Computer Science
15
Dot operator
When do we use ‘Dot’ Operator?
We use it only when we multiply a Vector with a scalar
9/27/2006 School of Engineering and Computer Science
16
Simple Program
a = [1 2 3;4 5 6;7 8 9]b = 2c = a .* b % same as dc1 = a * b % same as dd = conv2(a,b) % each element * 2e = power(a,b) % same as gf = a ^b % Multiplicationg = a .^b % power of each
element
9/27/2006 School of Engineering and Computer Science
17
g =
1 4 916 25 3649 64 81
9/27/2006 School of Engineering and Computer Science
18
Matlab is case sensitive.so ‘a’ and’A’aredifferent namesComments are preeced by ‘%’If we want help on cos function type help cosCommand who and whos gives the list ofvariables created in work spaceLength(x) and size(x)----explainFormat short e,format long e and format blank
9/27/2006 School of Engineering and Computer Science
19
9/27/2006 School of Engineering and Computer Science
20
Fourier AnalysisIf I talk about fourier analysis,it can be fourier series and/orfourier transform.Due to some basic reasons fourier transform only could gain familiarity in Matlab than series.Reason
Discrete Fourier Transform
Fast Fourier Transform
Fourier Transform
9/27/2006 School of Engineering and Computer Science
21
Now we will see what is the algorithm of DFTThere are two main Algorithm of DFT1.Decimation in time Expand2.Decimation in frequency Expand
It is clear from the discussion that due to more involvement of mathematics DFT is slow. So we do have an other algorithm called FFT.What is now FFT.FFT is expansion of Fast fourier transform and uses the algorithm of DFT.The main difference between them is that FFT can break N samples into N/2 and N/2 and thus it is fast
Algorithm of FFT
9/27/2006 School of Engineering and Computer Science
22
Why FFT?If you look at the equation for the Discrete Fourier Transform you will see
that it is complicated to work out as it involves many additions and multiplications involving complex numbers. Even a simple eight sample signal would require 49 complex multiplications and 56 complex additions to work out the DFT. At this level it is still manageable, however a realistic signal could have 1024 samples which requires over 20,000,000 complex multiplications and additions. As you can see the number of calculations required soon mounts up to unmanageable proportions. The Fast Fourier Transform is a simply a method of laying out the computation, which is much faster for large values of N, where N is the number of samples in the sequence. It is an ingenious way of achieving rather than the DFT's clumsy P2 timing. The idea behind the FFT is the divide and conquer approach, to break up the original N point sample into two (N / 2) sequences. This is because a series of smaller problems is easier to solve than one large one. The DFT requires (N-1)2 complex multiplications and N(N-1)complex additions as opposed to the FFT's approach of breaking it down into a series of 2 point samples which only require 1 multiplication and 2 additions and the recombination of the points which is minimal. For example Data contains hundreds of thousands of samples and would take months to evaluate the DFT. Therefore we use the FFT.
9/27/2006 School of Engineering and Computer Science
23
But thanks to Matlab.Matlab eases every difficulty of Mathematics calculation with a syntax
Now we just have to typep = fft(x) where x is any vector.
Similarly to find inverse Fast fourier transform type,O= ifft(x)where x is a vector
9/27/2006 School of Engineering and Computer Science
24
About DFT
9/27/2006 School of Engineering and Computer Science
25
Discrete fourier transform: X=dft(x)The firstelementrefersto X(0)Similarly inverse of it:x=idft(X)For more efficient but less obvious program,DFT can be
computed using FFT.To compute x[n],X=fft(x).we can also use X=fft(x,N)
The longer the length of x,the finer the grid for the FFT.Due to wrap round effect only first N/2 Points have meaning.The inverse :x=ifft(X);
9/27/2006 School of Engineering and Computer Science
26
Continuous time system analysis
Consider a feed back system as shown
G(s)
H(s)
9/27/2006 School of Engineering and Computer Science
27
Actually Transfer function = output /inputIf suppose H(s) is a Transfer function and H(s) = 2 * s + 3/s^3 + 4 * s^2 + 5
We write in matlab as num = [2 3]den = [1 4 0 5]
Transfer function may also be defined in terms of its pole,Zeros and gain
H(s) = K(s-z1)(s-z2)….(s-zm)/(s-p1)(s-p2)….(s-pn),type
[z p k]=tt2zp(n,d)
Zp2tf [n,d]=zp2tf(z p k)
9/27/2006 School of Engineering and Computer Science
28
The overall transfer function of individual systems in parallel, series or feedback can be found using MATLAB. Consider block diagram reduction of the different configurations shown. Storethe transfer function G in numGand denG, and the transfer function H in numH and denH.
•To reduce the general feedback system to a single transfer function, Gcl(s) = G(s)/(1+G(s)H(s)) type[numcl,dencl] = feedback(numG,denG,numH,denH);For a unity feedback system, let numH = 1 and denH = 1 before applying the above algorithm.Alternately, use the command[numcl,dencl] = cloop(numG,denG,-1);
9/27/2006 School of Engineering and Computer Science
29
To reduce the series system to a single transfer function, Gs(s) = G(s)H(s) type[nums,dens] = series(numG,denG,numH,denH);To reduce the parallel system to a single transfer function, Gp(s) = G(s) + H(s) type
[nump,denp] = parallel(numG,denG,numH,denH);
9/27/2006 School of Engineering and Computer Science
30
9/27/2006 School of Engineering and Computer Science
31
The analytical function to find time response of a system requires taking inverse transform of Y(s).Matlab aids this process by computing Partial fraction expansion ofY(s),[r p k]=residue(n d)A numerical method to find the response of a stem is available in Matlab.Depending on I/p applied ,response can be got from ,Step or impulse (n d)
9/27/2006 School of Engineering and Computer Science
32
For the response to an arbitrary input, use the command lsim. Create a vector t which contains the time values in seconds at which you want MATLAB to calculate the response. Typically, this is done by entering
t = a:b:c;where a is the starting time, b is the time step and c is the end time. For smooth plots, choose b so that there are at least 300 elements in t (increase as necessary). Define the input x as a function of time, for example, a ramp is defined as x = t. Then plot the response by typinglsim(num,den,x,t);
9/27/2006 School of Engineering and Computer Science
33
num = 2; den = [1 2];t = 0:3/300:3; % for a time constant of 1/2y = step(num,den,t);plot(t,y)
9/27/2006 School of Engineering and Computer Science
34
Frequency response
Let ‘w’ be the frequency defined.W=a:b:c----explain termsSo frequency response is got from ,H=freqs(n d w) gives H(w) for each ‘w’
To draw a bode plot of a transfer function which has been stored in vectors n and d,
bode(n d)
9/27/2006 School of Engineering and Computer Science
35
To define the plot,firstdefine vector ‘w’ which contains the frequencies at which the bode plot will be calculated. since ‘w’ has to be defined logscale ,logspace command has to be usedEg Make a bode plot ranging frequencies 10^-1 to 10^2,define ‘w’ by,
w=logspace(-1,2)To get the magnitude and phase information ofBode Plot,[m p]=bode(n d w)To plot magnitude in decibals,convert magnitude using
magdb=20*log(mag) [base 10]
9/27/2006 School of Engineering and Computer Science
36
Analog Filter Design
MATLAB contains commands to design various types of filters.some of them are Butterworth and Type1 Chebyshev.The commands butterap and cheb1ab are used to design Butterworth and Type1 Chebyshev filters respectively with a cut off frequency of 1 rad/sec.
Eg For a n pole butterworth filter ,type[z p k]=butter(n)
Eg For n pole Eg For n pole chebyshevchebyshev filter ,typefilter ,type[z p k]=cheb1ab(n rp)[z p k]=cheb1ab(n rp)
To find the numerator and denominator polynomials of the resultiTo find the numerator and denominator polynomials of the resulting filter ng filter ,type,type[b a]=zp2tf(z p k)[b a]=zp2tf(z p k)
9/27/2006 School of Engineering and Computer Science
37
We can also do frequency transformations from low pass to low passLowpass to high passLowpass to bandstopLowpass to bandpass
Eg To map to a lowpass filter with a cut off frequency ‘w’and nu and de are stored in b1 and a1 resp.type
[b1 a1]=lp2lp(b a ‘w’) similar to lp2hp
To map to bandpass filter with band width To map to bandpass filter with band width ‘‘bwbw’’ centered at frequency centered at frequency ‘‘ww’’,type,type[b1 a1]=lp2bp(b a w bw) similarly to lp2bs[b1 a1]=lp2bp(b a w bw) similarly to lp2bs
9/27/2006 School of Engineering and Computer Science
38
Control Design
Consider a feedback loop as shown in where G(s)H(s) = KP(s) and K is a gain and P(s) contains the poles and zeros of the controller and of the plant. The root locus is a plot of the roots of the closed loop transfer function as the gain is varied. Suppose that the numerator and denominator coefficients of P(s) are stored in the vectors num and den. Then the following command computesand plots the root locus:
rlocus(num,den)
K = 0:100;r = rlocus(num,den,K);plot(r,'.')
9/27/2006 School of Engineering and Computer Science
39
Discrete time system analysisConvolutionconv and deconv
explain
9/27/2006 School of Engineering and Computer Science
40
Transfer Function Representation
For a DTTF(Discrete time transfer function) ,the co effecients are stored in decending powers of ‘z’or ascending powers of’z^-1.Eg H(z)=2z^2+3z+4/z^2+5z+6
=2+3z^-1+4z^-2/1+5z^-1+6z^-2Then vectors are n=[2 3 4]
d=[1 5 6]
9/27/2006 School of Engineering and Computer Science
41
Digital filter designThe analog prototype method of designing IIR filters can be done by first designing an analog filter with the desired characteristics. Store the numerator and denominator of the analog filter, H(s), in the vectors num and den, and let T be the sampling period. Then the numerator and denominator of the digital filter Hd(z) is found from the following command
[numd,dend] = bilinear(num,den,1/T)To design a digital lowpass filter based on the analog ButterworTo design a digital lowpass filter based on the analog Butterworth filter, th filter, use the commands:use the commands:
[num,den] = [num,den] = butter(n,Omegacbutter(n,Omegac))where n is the number of poles and Omegac is the normalized digiwhere n is the number of poles and Omegac is the normalized digital tal cutoff frequency, cutoff frequency, WcWc = = wcT/pwcT/p..To design a highpass filter with cutoff frequency Omegac, use thTo design a highpass filter with cutoff frequency Omegac, use the e commandscommands
[num,den] = [num,den] = butter(n,Omegac,'highbutter(n,Omegac,'high')')
9/27/2006 School of Engineering and Computer Science
42
To design a bandpass filter with pass band from Omega1 to To design a bandpass filter with pass band from Omega1 to Omega2, define Omega = [Omega1,Omega2] and use the Omega2, define Omega = [Omega1,Omega2] and use the commandcommand
[num,den] = butter(n,Omega)[num,den] = butter(n,Omega)
Similarly we design bandstop Similarly we design bandstop filter.Replacefilter.Replace high with stop.high with stop.The design for an nth order Type I Chebyshev filter is accomplisThe design for an nth order Type I Chebyshev filter is accomplished hed using the same methods as for butter except that "butter" is repusing the same methods as for butter except that "butter" is replaced by laced by "cheby1":"cheby1":
[num,den] = cheby1(n,Omegac); % for a lowpass filter[num,den] = cheby1(n,Omegac); % for a lowpass filter[num,den] = cheby1(n,Omegac,'high'); % for a highpass filter[num,den] = cheby1(n,Omegac,'high'); % for a highpass filter
If Omega has two elements,If Omega has two elements,[num,den] = cheby1(n,Omega); % for a bandpass[num,den] = cheby1(n,Omega); % for a bandpass[num,den] = cheby1(n,Omega,'stop'); % for a bandstop[num,den] = cheby1(n,Omega,'stop'); % for a bandstop
9/27/2006 School of Engineering and Computer Science
43
The Signal Processing Toolbox also provides commands for computing the FIR filter directly. To obtain an FIR filter with length N and cutoff frequency Omegac (normalized by p) use the command
hd = fir1(N-1,Omegac)The vector hd contains the impulse response of the FIR where hd(1) is the value of hd[0].A length N highpass filter with normalized cutoff frequency Omegac is designed by using the command
hd = fir1(N-1,Omegac,'high')
9/27/2006 School of Engineering and Computer Science
44
Write design
9/27/2006 School of Engineering and Computer Science
45
9/27/2006 School of Engineering and Computer Science
46
9/27/2006 School of Engineering and Computer Science
47
9/27/2006 School of Engineering and Computer Science
48
9/27/2006 School of Engineering and Computer Science
49
9/27/2006 School of Engineering and Computer Science
50
9/27/2006 School of Engineering and Computer Science
51
9/27/2006 School of Engineering and Computer Science
52
9/27/2006 School of Engineering and Computer Science
53
9/27/2006 School of Engineering and Computer Science
54
9/27/2006 School of Engineering and Computer Science
55
9/27/2006 School of Engineering and Computer Science
56
9/27/2006 School of Engineering and Computer Science
57
9/27/2006 School of Engineering and Computer Science
58
9/27/2006 School of Engineering and Computer Science
59
9/27/2006 School of Engineering and Computer Science
60
9/27/2006 School of Engineering and Computer Science
61
9/27/2006 School of Engineering and Computer Science
62
9/27/2006 School of Engineering and Computer Science
63
9/27/2006 School of Engineering and Computer Science
64
9/27/2006 School of Engineering and Computer Science
65
9/27/2006 School of Engineering and Computer Science
66
9/27/2006 School of Engineering and Computer Science
67
9/27/2006 School of Engineering and Computer Science
68
9/27/2006 School of Engineering and Computer Science
69
9/27/2006 School of Engineering and Computer Science
70
9/27/2006 School of Engineering and Computer Science
71
9/27/2006 School of Engineering and Computer Science
72
9/27/2006 School of Engineering and Computer Science
73
9/27/2006 School of Engineering and Computer Science
74
9/27/2006 School of Engineering and Computer Science
75
9/27/2006 School of Engineering and Computer Science
76
Design
9/27/2006 School of Engineering and Computer Science
77
Type Order Pass Band Stop Band Transition Phase aka
Butterworth High Flat Flat SlowNearly Linear
Chebyshev Medium Low Rippled FlatMedium Steep
Medium Linear
Chebyshev Type 1
Inverse Chebyshev Medium High Flat Rippled
Medium Slow
Medium Linear
Chebyshev Type 2
Elliptic Low Rippled Rippled Fast Least Linear CauerBessel Very High Flat Flat Very Slow Very Linear
Filter Types (Low Pass analog forms)
9/27/2006 School of Engineering and Computer Science
78
Core Low-Pass Filter Design
Specs(freq response, ...) Pick a filter
formSolve for
parameters
Replace ω2
with -s2
Take Left-Hand Half-Plane
ωs
| Η(ω) |
ωp
vs
vp
ωs
| Η(ω) |
ωp
vs
vp
( )cNA ωω ,11
+
flatness, steepness, ...
closed form, matlab,
analytic freq response
analytic transfer fn
Factor
poles and zeros
analytic transfer fn -polynomial form
Given:
Find:Filter Design
4345
23
2
++++
ssss
Core Low-Pass Filter Design
9/27/2006 School of Engineering and Computer Science
79
Break into simple
components
Analog Filter Design
Transform to LP Specs
Superimpose simple
components
For each simple component
General Specs(freq response, ...)
Simple Components
ω
| Η(ω) |
ω
| Η(ω) |ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
Transform out of LP form
transfer function as ratio of polynomials
analytic transfer fn -polynomial form
ss 1
=′
Given:
Find:Find Second
Order Sections
Filter Design(Series of SOS’s)
Specs(freq response, ...)
ωs
| Η(ω) |
ωp
vs
vp
ωs
| Η(ω) |
ωp
vs
vp
Core Low-Pass Filter Design
Specs(freq response, ...) Pick a filter
formSolve for
parameters
Replace ω 2
with -s2
Take Left-Hand Half-Plane
ωs
| Η(ω) |
ωp
vs
vp
ωs
| Η(ω) |
ωp
vs
vp
( )cNA ωω ,11
+
flatness, steepness, ...
closed form, matlab,
analytic freq response
analytic transfer fn
Factor
poles and zeros
analytic transfer fn -polynomial form
Given:
Find:Filter Design
4345
23
2
++++
ssss
Core Low-Pass Filter Design
9/27/2006 School of Engineering and Computer Science
80
Break into simple
components
IIR Filter Design
Transform to LP Specs
Superimpose simple
components
For each simple component
General Specs(freq response, ...)
Simple Components
ω
| Η(ω) |
ω
| Η(ω) |ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
ω
| Η(ω) |
Transform out of LP form
transfer function as ratio of polynomials
analytic transfer fn in s
polynomial form
ss 1
=′
Given:
Find:Filter Design
Specs(freq response, ...)
ωs
| Η(ω) |
ωp
vs
vp
ωs
| Η(ω) |
ωp
vs
vp
Core Low-Pass Filter Design
Specs(freq response, ...) Pick a filter
formSolve for
parameters
Replace ω 2
with -s2
Take Left-Hand Half-Plane
ωs
| Η(ω) |
ωp
vs
vp
ωs
| Η(ω) |
ωp
vs
vp
( )cNA ωω ,11
+
flatness, steepness, ...
closed form, matlab,
analytic freq response
analytic transfer fn
Factor
poles and zeros
analytic transfer fn -polynomial form
Given:
Find:Filter Design
4345
23
2
++++
ssss
Core Low-Pass Filter DesignPre-Warp
Specs
( ) Ω′⇒2tan ω
Bilinear Transform
1
1
11
−
−
+−
⇒zzs
analytic transfer fn in z
polynomial form
9/27/2006 School of Engineering and Computer Science
81
Pick Odd Order N
FIR Filter Design
N Point Sample of Frequency Response
Weight
(Hamming, ...)
General Specs(freq response, ...)
OR ω
| Η(ω) |
ω
| Η(ω) |
Given:
Find:Filter Design
fΔ
Impulse Response
Inverse Fourier
Transform
Inverse Fourier
Transform
N Point Sub-Sample
(-M to M)
Shift (M = (N-1)/2)
ω
??ω
( )kd
( )kd ΚΚ ,3,2,1,0,1,2,3, −−−=k
MMk ΚΚ ,3,2,1,0,1,2,3,, −−−−=
( )kd MMk ΚΚ ,3,2,1,0,1,2,3,, −−−−=
( )nh′ Nk Κ,3,2,1,0=
( )nh
based on
δbased on
9/27/2006 School of Engineering and Computer Science
82
Write F
9/27/2006 School of Engineering and Computer Science
83
Most of the commands for the continuous time state space representation also work for the discretetime state space. For example, ss2tf, tf2ss, and ss2ss for discrete time are used exactly the same way as for the continuous time case . There is a discrete time version of
the command lsim, which is used as follows:[y,x] = dlsim(A,B,C,D,u,n);
where the output is stored in y, the states are stored in x,the input is stored in u and the time index
is stored in n.
9/27/2006 School of Engineering and Computer Science
84
PLOTTINGCommands areplotxlabelylabeltitlegridaxisstemSubplotExplain every command
9/27/2006 School of Engineering and Computer Science
85
Waveform generation:Periodic and Aperiodic
We can also generate the waveforms through matlab .Its Very Simple and easy.The examples are will be demonstrated both for periodic and aperiodic wave generation.Eg x=[-pi pi]
sin(x)
9/27/2006 School of Engineering and Computer Science
86
VCOvco
Voltage controlled oscillator
Syntax
y = vco(x,fc,fs) vco
Voltage controlled oscillator
Syntax
y = vco(x,fc,fs)y = vco(x,[Fmin Fmax],fs)
Description
y = vco(x,fc,fs) creates a signal that oscillates at a frequency determined by the real input vector or array x with sampling frequency fs. fc is the carrier or reference frequency; when x is 0, y is an fc Hz cosine with amplitude 1 sampled at fs Hz. x ranges from -1 to 1, where x = -1 corresponds to 0 frequency output, x = 0 corresponds to fc, and x = 1 corresponds to 2*fc. Output y is the same size as x.
y = vco(x,[Fmin Fmax],fs) scales the frequency modulation range so that ±1 values of x yield oscillations of Fmin Hz and Fmax Hz respectively. For best results, Fmin and Fmax should be in the range 0 to fs/2.
y = vco(x,[Fmin Fmax],fs)
9/27/2006 School of Engineering and Computer Science
87
Description
y = vco(x,fc,fs) creates a signal that oscillates at a frequencydetermined by the real input vector or array x with sampling frequency fs. fc is the carrier or reference frequency; when x is 0, y is an fc Hz cosine with amplitude 1 sampled at fs Hz. x ranges from -1 to 1, where x = -1 corresponds to 0 frequency output, x = 0 corresponds to fc, and x = 1 corresponds to 2*fc. Output y is the same size as x.
y = vco(x,[Fmin Fmax],fs) scales the frequency modulation range so that ±1 values of x yield oscillations of Fmin Hz and Fmax Hz respectively. For best results, Fmin and Fmax should be in the range 0 to fs/2.
9/27/2006 School of Engineering and Computer Science
88
By default, fs is 1 and fc is fs/4.
If x is a matrix, vco produces a matrix whose columns oscillate according to the columns of x.
Examples
Generate two seconds of a signal sampled at 10,000 samples/second whose instantaneous frequency is a triangle function of time:
fs = 10000;t = 0:1/fs:2;x = vco(sawtooth(2*pi*t,0.75),[0.1 0.4]*fs,fs);
Plot the spectrogram of the generated signal: spectrogram(x,kaiser(256,5),220,512,fs,'yaxis')
9/27/2006 School of Engineering and Computer Science
89
9/27/2006 School of Engineering and Computer Science
90
What Is the Signal Processing Toolbox?
The Signal Processing Toolbox is a collection of tools built on the MATLAB numeric computing environment. The toolbox supports a wide range of signal processing operations, from waveform generation to filter design and implementation, parametric modeling, and spectral analysis. The toolbox provides two categories of tools:
Command line functions in the following categories: Analog and digital filter analysis Digital filter implementation FIR and IIR digital filter design Analog filter design Filter discretization Spectral Windows Transforms Cepstral analysis Statistical signal processing and spectral analysis Parametric modeling Linear Prediction Waveform generation
A suite of interactive graphical user interfaces for Filter design and analysis Window design and analysis Signal plotting and analysisSpectral analysis Filtering signals
9/27/2006 School of Engineering and Computer Science
91
Filtering and FFTs
Two of the most important functions for signal processing are not in the Signal Processing Toolbox at all, but are built-in MATLAB functions: filter applies a digital filter to a data sequence. fft calculates the discrete Fourier transform of a sequence.
The operations these functions perform are the main computational workhorses of classical signal processing. Both are described here. The Signal Processing Toolbox uses many other standard MATLAB functions and language features, including polynomial root finding, complex arithmetic, matrix inversion and manipulation, and graphics tools.
9/27/2006 School of Engineering and Computer Science
92
Signals and Systems
The basic entities that toolbox functions work with are signals and systems. The functions emphasize digital, or discrete, signals and filters, as opposed to analog, or continuous, signals. The principal filter type the toolbox supports is the linear, time-invariant digital filter with a single input and a single output. You can represent linear time-invariant systems using one of several models (such as transfer function, state-space, zero-pole-gain, and second-order section) and convert between representations.
9/27/2006 School of Engineering and Computer Science
93
Key Areas: Filter Design and Spectral Analysis
In addition to its core functions, the toolbox provides rich, customizable support for the key areas of filter design and spectral analysis. It is easy to implement a design technique that suits your application, design digital filters directly, or create analog prototypes and discretize them. Toolbox functions also estimate power spectral density and cross spectral density, using either parametric or nonparametric techniques. Filter Design and Implementation and Statistical Signal Processing, respectively detail toolbox functions for filter design and spectral analysis.
Some filter design and spectral analysis functions included in the toolbox are Computation and graphical display of frequency response System identification Generating signals Discrete cosine, chirp-z, and Hilbert transforms Lattice filters Resampling Time-frequency analysis Basic communication systems simulation
9/27/2006 School of Engineering and Computer Science
94
Interactive Tools
The power of the Signal Processing Toolbox is greatly enhanced by its easy-to-use interactive tools. SPTool provides a rich graphical environment for signal viewing, filter design, and spectral analysis. The Filter Design and Analysis Tool (FDATool) provides a more comprehensive collection of features for addressing the problem of filter design. The FDATool also offers seamless access to the additional filter design methods and quantization features of the Filter Design Toolbox when that product is installed. The Window Design and Analysis Tool (WinTool) provides an environment for designing and comparing spectral windows.
9/27/2006 School of Engineering and Computer Science
95
The pulstran Function
The pulstran function generates pulse trains from either continuous or sampled prototype pulses. The following example generates a pulse train consisting of the sum of multiple delayed interpolations of a Gaussian pulse. The pulse train is defined to have a sample rate of 50 kHz, a pulse train length of 10 ms, and a pulse repetition rate of 1 kHz; D specifies the delay to each pulse repetition in column 1 and an optional attenuation for each repetition in column 2. The pulse train is constructed by passing the name of the gauspuls function to pulstran, along with additional parameters that specify a 10 kHz Gaussian pulse with 50% bandwidth:
T = 0:1/50E3:10E-3;D = [0:1/1E3:10E-3;0.8.^(0:10)]';Y = pulstran(T,D,'gauspuls',10E3,0.5);plot(T,Y)
9/27/2006 School of Engineering and Computer Science
96
The Sinc Function
The sinc function computes the mathematical sinc function for an input vector or matrix x. The sinc function is the continuous inverse Fourier transform of the rectangular pulse of width and height 1.
The sinc function has a value of 1 where x is zero, and a value of
for all other elements of x.
To plot the sinc function for a linearly spaced vector with values ranging from -5 to 5, use the following commands:
x = linspace(-5,5);y = sinc(x);plot(x,y)
9/27/2006 School of Engineering and Computer Science
97
The Dirichlet Function
The toolbox function diric computes the Dirichlet function, sometimes called the periodic sinc or aliased sinc function, for an input vector or matrix x. The Dirichlet function is
where n is a user-specified positive integer. For n odd, the Dirichlet function has a period of ; for n even, its period is . The magnitude of this function is (1/n) times the magnitude of the discrete-time Fourier transform of the n-point rectangular window.
To plot the Dirichlet function over the range 0 to 4 for n = 7 and n = 8, use
x = linspace(0,4*pi,300);plot(x,diric(x,7)); axis tight;plot(x,diric(x,8)); axis tight;
9/27/2006 School of Engineering and Computer Science
98
FIR Filter Design
Digital filters with finite-duration impulse response (all-zero, or FIR filters) have both advantages and disadvantages compared to infinite-duration impulse response (IIR) filters.
FIR filters have the following primary advantages: They can haveexactly linear phase. They are always stable. The design methodsare generally linear. They can be realized efficiently in hardware. The filter startup transients have finite duration.
The primary disadvantage of FIR filters is that they often require a much higher filter order than IIR filters to achieve a given level of performance. Correspondingly, the delay of these filters is often much greater than for an equal performance IIR filter.
9/27/2006 School of Engineering and Computer Science
99
Fir FILTER DESIGN:DESIGN BY WINDOWING
The MATLAB function fir1() designs conventional lowpass, highpass, bandpass, and bandstop linear-phase FIR filters based on the windowing method. The command b = fir1(N,Wn) returns in vector b the impulse response of a lowpass filter of order N. The cut-off frequency Wn must be between 0 and 1 with 1 corresponding to the half sampling rate. The command
b = fir1(N,Wn,'high') returns the impulse response of a highpass filter of order N with normalized cutoff frequency Wn.
Similarly, b = fir1(N,Wn,'stop') with Wn a two-element vector designating the stopband designs a bandstop filter. Without explicit specification, the Hamming window is employed in the design. Other windowing functions can be used by specifying the windowing function as an extra argument of the function. For example, Blackman window can be used instead by the command b = fir1(N, Wn, blackman(N)).
9/27/2006 School of Engineering and Computer Science
100
Parks-McClellan FIR filter design
The MATLAB command b = remez(N,F,A) returns the impulse response of the length N+1 linear phase FIR filter of order N designed by Parks-McClellan algorithm. F is a vector of frequency band edges in ascending order between 0 and 1 with 1 corresponding to the half sampling rate. A is a real vector of the same size as F which specifies the desired amplitude of the frequency response of the points (F(k),A(k)) and (F(k+1),A(k+1)) for odd k. For odd k, the bands between F(k+1) and F(k+2) is considered as transition bands.
9/27/2006 School of Engineering and Computer Science
101
Example
9/27/2006 School of Engineering and Computer Science
102
BRIEFLY
9/27/2006 School of Engineering and Computer Science
103
Filter design of FIR filters can be mainly achieved by two ways.1.Window Shapes2.Design By Iterative Optimization
9/27/2006 School of Engineering and Computer Science
104
9/27/2006 School of Engineering and Computer Science
105
9/27/2006 School of Engineering and Computer Science
106
9/27/2006 School of Engineering and Computer Science
107
9/27/2006 School of Engineering and Computer Science
108
9/27/2006 School of Engineering and Computer Science
109
9/27/2006 School of Engineering and Computer Science
110
9/27/2006 School of Engineering and Computer Science
111
9/27/2006 School of Engineering and Computer Science
112
9/27/2006 School of Engineering and Computer Science
113
9/27/2006 School of Engineering and Computer Science
114
9/27/2006 School of Engineering and Computer Science
115
9/27/2006 School of Engineering and Computer Science
116
Advantages of FIR filters
FIR filters are simple to design.They are guaranteed to be bounded
input-bounded output (BIBO) stable.By designing the filter taps to be symmetrical
about the center tap position, aFIR filter can be guaranteed to have linear
phase. This is a desirable propertyfor many applications such as musicand video processing.
9/27/2006 School of Engineering and Computer Science
117
More advantages
FIR filters also have a low sensitivityto filter coefficient quantization errors.(This is an important property to havewhen implementing a filter on a DSPprocessor or on an integrated circuit).
9/27/2006 School of Engineering and Computer Science
118
IIR
9/27/2006 School of Engineering and Computer Science
119
Another type of digital filter is Infinite Impulse reesponse(IIR) filter.As we defined the impulse response of IIR filter is of infinite duration.The general differential equation of IIR digital filter is given by
9/27/2006 School of Engineering and Computer Science
120
Where ak=k-th feedback tap.summation =k=1 to N-1 where N is the number of feedback taps in IIR filter.
The right summation is from K=0 to K=M where M is number of feedforward taps respectively.
9/27/2006 School of Engineering and Computer Science
121
Unlike FIR filters,the o/p of IIR filter depends on both previous M inputs lso previous N outputs.This is responsible for infinite impulse response
9/27/2006 School of Engineering and Computer Science
122
ExamplePrive that IIR filter has infinite impulse response.
A)Consider two tap IIR filter with following Differential equation
9/27/2006 School of Engineering and Computer Science
123
Here the input to filter is delta(n).The o/p at different time is shown as
9/27/2006 School of Engineering and Computer Science
124
Advantages of IIR filters
IIR filters are useful for high-speeddesigns because they typically require alower number of multiplies compared toFIR filters.
IIR filters can also be designed to have a frequency response that is a discrete version of the frequency response of an analog filter.
9/27/2006 School of Engineering and Computer Science
125
So,the use of FIR filters are advisable .
9/27/2006 School of Engineering and Computer Science
126
Dis advantages of IIR filtersUnfortunately, IIR filters do not have linear phase and they can be unstable if not designed properly. IIR filters also are very sensitive to filter coefficient quantization errors that occur due to using a finite number of bits to representthe filter coefficients.
One way to reduce this sensitivity is to use a cascaded design.
( the IIR filter is implemented as a series of lower-orderIIR filters as opposed to one high-order Section).
9/27/2006 School of Engineering and Computer Science
127
FDA tool
9/27/2006 School of Engineering and Computer Science
128
Opening FDATool
To open the Filter Design and Analysis Tool (FDATool), type fdatool
The Filter Design and Analysis Tool opens with the Design Filterpanel displayed.
Note If you are viewing this online, click in the figure below to jump to a description of the procedure for that area of the figure.
FDATool contains toolbar buttons for controlling sessions and displaying a full view analysis, various types of filter analysis, and what's this help. FDATool includes buttons or fields for saving filters and for specifying the filter method, the filter order, the filter options, and the frequency and magnitude specifications. A Design Filter button creates your new filter. The sidebar on the left side has buttons to display the Pole-Zero Editor, the Import Filter panel, and the Design Filter panel.
9/27/2006 School of Engineering and Computer Science
129
Choosing a Response Type
You can choose from several response types: Lowpass Raised cosine Highpass Bandpass Bandstop Differentiator Multiband Hilbert transformer Arbitrary magnitude
Additional response types are available if you have the Filter Design Toolbox installed.
To design a bandpass filter, select the radio button next to Bandpass in the Response Type region of the GUI.
9/27/2006 School of Engineering and Computer Science
130
Choosing a Filter Design Method
You can use the default filter design method for the response type that you've selected, or you can select a filter design method from the available FIR and IIR methods listed in the GUI.
To select the Remez algorithm to compute FIR filter coefficients, select the FIR radio button and choose Equiripple from the list of methods.
9/27/2006 School of Engineering and Computer Science
131
Bandpass Filter Frequency Specifications
For a bandpass filter, you can set Units of frequency: Hz kHz MHz Normalized (0 to 1) Sampling frequency Passband frequencies Stopband frequencies
You specify the passband with two frequencies. The first frequency determines the lower edge of the passband, and the second frequency determines the upper edge of the passband.
Similarly, you specify the stopband with two frequencies. The first frequency determines the upper edge of the first stopband, and the second frequency determines the lower edge of the second stopband.
For this example: Keep the units in Hz (default). Set the sampling frequency (Fs) to 2000 Hz. Set the end of the first stopband (Fstop1) to 200 Hz. Set the beginning of the passband (Fpass1) to 300 Hz. Set the end of the passband (Fpass2) to 700 Hz. Set the beginning of the second stopband (Fstop2) to 800 Hz.
9/27/2006 School of Engineering and Computer Science
132
Bandpass Filter Magnitude Specifications
For a bandpass filter, you can specify the following magnitude response characteristics: Units for the magnitude response (dB or linear) Passband ripple Stopband attenuation
For this example: Keep Units in dB (default). Set the passband ripple (Apass) to 0.1 dB. Set the stopband attenuation for both stopbands (Astop1, Astop2) to 75 dB.
9/27/2006 School of Engineering and Computer Science
133
Computing the Filter Coefficients
Now that you've specified the filter design, click the Design Filter button to compute the filter coefficients.
Notice that the Design Filter button is disabled once you've computed the coefficients for your filter design. This button isenabled again once you make any changes to the filter specifications.
Note You can undo actions, one at a time, by selecting Undo from the Edit menu or by clicking the Undo toolbar button. To redo previously undone actions, select Redo from the Edit menu or click the Redo toolbar button.
9/27/2006 School of Engineering and Computer Science
134
SP Tool
9/27/2006 School of Engineering and Computer Science
135
SPTool: An Interactive Signal Processing Environment
SPTool is an interactive GUI for digital signal processing that can be used to Analyze signals Design filters Analyze (view) filtersFilter signals Analyze signal spectra
You can accomplish these tasks using four GUIs that you access from within SPTool: The Signal Browser is for analyzing signals.You can also play portions of signals using your computer's audio hardware. The Filter Designer is for designing or editing FIR and IIR digital filters. Most of the Signal Processing Toolbox filter design methods available at the command line are also available in the Filter Designer. Additionally, you can design a filter by using the Pole/Zero Editor to graphically place poles and zeros on the z-plane. The Filter Visualization Tool is for analyzing filter characteristics. See Filter Visualization Tool. The Spectrum Viewer is for spectral analysis. You can use the Signal Processing Toolbox spectral estimation methods to estimate the power spectral density of a signal. See Spectrum Viewer.
9/27/2006 School of Engineering and Computer Science
136
Opening SPTool
To open SPTool, type sptool
9/27/2006 School of Engineering and Computer Science
137
Signal Browser
You can use the Signal Browser to display and analyze signals listed in the Signals list box in SPTool.
Using the Signal Browser you can: Analyze and compare vector or array (matrix) signals. Zoom in on portions of signal data. Measure a variety of characteristics of signal data. Compare multiple signals. Play portions of signal data on audio hardware. Print signal plots.
9/27/2006 School of Engineering and Computer Science
138
Filter Designer
The Filter Designer provides an interactive graphical environment for the design of digital IIR and FIR filters based on specifications that you enter on a magnitude or pole-zero plot.
Note You can also use the Filter Design and Analysis Tool (FDATool) described in FDATool: A Filter Design and Analysis GUIfor filter design and analysis.
Filter Types:
You can design filters of the following types using the Filter Designer: Bandpass Lowpass Bandstop Highpass
FIR Filter Methods
You can use the following filter methods to design FIR filters: Equiripple Least squares Window
9/27/2006 School of Engineering and Computer Science
139
FIR Filter Methods
You can use the following filter methods to design FIR filters: Equiripple Least squares Window
IIR Filter Methods
9/27/2006 School of Engineering and Computer Science
140
You can use the following filter methods to design IIR filters: Butterworth Chebyshev Type I Chebyshev Type II Elliptic
Pole/Zero Editor
You can use the Pole/Zero Editor to design arbitrary FIR and IIRfilters by placing and moving poles and zeros on the complex z-plane.
Spectral Overlay Feature
You can also superimpose spectra on a filter's magnitude response to see if the filtering requirements are met.
9/27/2006 School of Engineering and Computer Science
141
Filter Visualization Tool
You can use the Filter Visualization Tool (fvtool) to analyze the following response characteristics of selected filters: Magnitude response Phase response Impulse response Step response Group delay Phase delay Pole and zero locations Detailed filter information
FVTool also provides features for Overlaying filter responses Zooming Measuring filter responses Modifying display parameters such as frequency ranges or magnitude units
If you start FVTool by clicking the SPTool Filter View button, that FVTool is linked to SPTool. Any changes made in SPTool to the filter are immediately reflected in FVTool. The FVTool title bar includes "SPTool" to indicate the link.
If you start an FVTool by clicking the New button or by selecting File-->New from within FVTool, that FVTool is a stand-alone version and is not linked to SPTool.
Note Every time you click the Filter View button a new, linked FVTool starts. This allows you to view multiple analyses simultaneously.
9/27/2006 School of Engineering and Computer Science
142
What is State space?
It is a technique in which a nonlinear system can be solved.Actually state space equations will be of form……….