Escola Federal de Egeharia de Itajubá Departameto de Egeharia Mecâica Pós-Graduação em Egeharia Mecâica MPF04 ANÁLISE DE SINAIS E AQUISÇÃO DE DADOS SINAIS E SISTEMAS Trabalho 02 (MATLAB) Prof. Dr. José Juliao de Lima Jr. e-mail: juliao@iem.efei.br home page:www.iem.efei.br/juliao Setembro de 2001
MPF04 Aálise de Siais e Aquisção de Dados 2/14 Ídice 1.1 Itroductio 3 1.2 Project 1: Basic Sigals 4 1.2.1 Exercise: Basic Sigals - Impulses 5 1.2.2 Exercise: Basic Sigals Siusoids 6 1.2.3 Exercise: Sampled Siusoids 7 1.2.4 Exercise: Basic Sigals-Expoetials 9 1.3 Project 2: Complex-Valued Sigals 10 1.3.1 Exercise: Complex Expoetials 12
Itroductio 3/14 Trabalho 02 SINAIS E SISTEMAS 1.1 Itroductio The basic sigals used ofte i digital sigal processig are the uit impulse sigal δ [, expoetials of the form u[, sie waves, ad their geeralizatio to complex expoetials. The followig projects are directed at the geeratio ad represetatio of these sigals i MATLAB. Sice the oly umerical data type i MATLAB is the M x N matrix, sigal must be represeted as vectors: either M x 1 matrices if colum vectors, or 1 x N matrices if row vectors. I MATLAB all sigals must be fiite i legth. This cotrasts sharply with aalytical problem solvig, where a mathematical formula ca be used to represet a ifiite-legth sigal (e. g., a decayig expoetial, a u[ ). a A Secod issue is the idexig domai associated with a sigal vector. MATLAB assumes by default that a vector is idexed from 1 to N, the vector legth. I cotrast, a sigal vector is ofte the result of samplig a sigal over some domai where the idexig rus from 0 to N-1; or, perhaps, the samplig start at some arbitrary idex that is egative, e. g., at N. The iformatio about the samplig domai caot be attached to the sigal vector cotaiig the sigal values. Istead, the user is forced to keep track of this iformatio separately. Usually, this is ot a problem util it comes time to plot the sigal, i which case the horizotal axis must be labeled properly.
MPF04 Aálise de Siais e Aquisção de Dados 4/14 A fial poit is the use of MATLAB's vector otatio to geerate sigals. A sigificat power of the MATLAB eviromet is its high-level otatio for vector maipulatio; for loops are almost always uecessary. Whe creatig sigals such as a sie wave, it is best to apply the si fuctio to a vector argumet, cosistig of all the time samples. I the followig projects, we treat the commo sigals ecoutered i digital sigal processig: impulses, impulse trais, expoetials, ad siusoids. 1.2 Project 1: Basic Sigals This project cocetrates o the issues ivolved with geeratio of some basic discrete-time sigals i MATLAB. Much of the work ceters o usig iteral MATLAB vector routies for sigal geeratio. I additio, a sample MATLAB fuctio will be implemeted. Hits Plottig discrete-time sigals is doe with the stem fuctio i MATLAB. The followig MATLAB code will create 31 poits of a discrete-time siusoid. = 0:30; % vector of time idices sius = si(/2+1); Notice that the = 0 idex must be referred to as (1), due to MATLAB's idexig scheme; likewise, sius(1) is the first value i the siusoid. Whe plottig the sie wave we would use the stem fuctio, which produces the discrete-time sigal plot commoly see i DSP books (see Figura 1.1): stem(,sius ); The first vector argumet must be give i order to get the correct -axis For compariso, try stem(sius) to see the default labelig.
Project 1: Basic Sigals 5/14 1 sie wave 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8-1 0 5 10 15 20 25 30 Time idex () Figura 1.1 -Pottig a discrete-time sigal with stem. 1.2.1 Exercise: Basic Sigals - Impulses The simplest sigal is the (shifted) uit impulse sigal: 1, = 0 δ[ 0] = (1.1) 0, 0. To create a impulse i MATLAB, we must decide how much of the sigal is of iterest. If the impulse δ [ is goig to be used to drive a causal LTI system, we might wat to see the L poits from = 0 to =L-1. If we choose L = 31, the followig MATLAB code will create a "impulse": L = 31; = 0:(L-1); imp = zeros(l,1); imp(1)=1; Notice that the = 0 idex must be referred to as imp(1), due to MATLAB's idexig scheme. (a). Geerate ad plot the followig sequeces. I each case the horizotal () axis should exted oly over the rage idicated ad should be labeled accordigly. Each sequece should be displayed as a discrete-time sigal usig stem
MPF04 Aálise de Siais e Aquisção de Dados 6/14 x [ = 0.9δ[ 5], 1 20, 1 x [ = 0.8δ[, 15 15, 2 x [ = 1.5δ[ 333], 300 350, 3 x [ = 4.5δ[ + 7], 10 0. 4 (b) The shifted impulse, δ ], ca be used to build a weighted [ 0 impulse trai, with period P ad total legth MP: s[ = M 1 l= 0 Alδ[ l P]. (1.2) The weights are AL; if they are all the same. The impulse trai is periodic with period P. Geerate ad plot a periodic impulse trai whose period is P = 5 ad whose legth is 50. Start the sigal at =0. Try to use oe or two vector operatios rather tha a for loop to set the impulse locatios. How may impulses are cotaied withi the fiitelegth sigal? (c). The followig MATLAB code will produce a repetitive sigal i the vector x. x=[0;1;1;0;0;0]*oes(1,7); x=x(:); size(x) % retur the sigal legth Plot x to visualize its form; the give a mathematical formula similar to equatio (1.2) to describe this sigal. 1.2.2 Exercise: Basic Sigals Siusoids Aother very basic sigal is cosie wave. I geeral, it takes three parameters to describe a real siusoidal sigal completely: amplitude (A), frequecy (Ω0), ad phase (φ). x [ = Acos( Ω0 + φ) (1.3)
Project 1: Basic Sigals 7/14 (a). Geerate ad plot each of the followig sequeces. Use MATLAB's vector capability to do this with oe fuctio call by takig the cosie (or sie) of a vector argumet. I each case, the horizotal () axis should exted oly over the rage idicated ad should be labeled accordigly. Each sequece should be displayed as a sequece usig stem. π x1[ = si( ), 17 0 25, π x2[ = si( ), 17 15 25, π x3[ = si(3π + ), 2 10 10, x4[ = cos( π ), 23 0 50. Give a simpler formula for x3[ that does ot use trigoometric fuctios. Explai why x4[ is a ot periodic sequece. (b). Write a MATLAB fuctio that will geerate a fiite-legth siusoid. The fuctio will eed a total of five iput argumets: three for the parameters ad two more to specify the first ad last idex of the fiite-legth sigal. The fuctio should retur the result a colum vector that cotais the values of the siusoid. Test this fuctio by plottig the results for various choices of the iput parameters. I particular, show how to geerate the sigal 2si( π /11) for 20 20. (c). Modificatio: Rewrite the fuctio i part (b) to retur two argumets: a vector of idices over the rager of, ad the values of the sigal. 1.2.3 Exercise: Sampled Siusoids Ofte a discrete-time sigal is produced by samplig a cotiuous-time sigal such as a costat-frequecy sie wave. The relatioship betwee the cotiuos-time frequecy ad the samplig frequecy is mai poit of
MPF04 Aálise de Siais e Aquisção de Dados 8/14 Nyquist-Shao samplig theorem, which requires that samplig frequecy be at least twice the highest frequecy i the sigal for perfect recostructio. I geeral, a cotiuos-time siusoid is give by the followig mathematical formula: s ( t) = Acos(2πf0t + φ) (1.4) where A is the amplitude f0 is the frequecy i Hertz, ad φ is the iitial phase. If a discrete-time sigal is produced by regular samplig of s(t) at a rate of fs=1/t, we get f 0 s[ = s( t) = Acos(2πf T + φ = A π + φ t= T 0 s ) cos 2 (1.5) f s Compariso with formula (1.4) for a discrete-time siusoid, x[=acos(ω0+φ), shows that the ormalized radia frequecy is ow a scaled versio of f0, Ω0=2π(f0Ts). (a). From formula (1.3) for the cotiuous-time siusoid, write a fuctio that will geerate samples of s(t) to create a fiite-legth discrete-time sigal. This fuctio will require six iputs: three for the sigal parameters, two for the start ad stop times, ad oe for samplig rate (i Hertz). It ca call the previously writte MATLAB fuctio for the discrete-time siusoid. To make the MATLAB fuctio correspod to the cotiuous-time sigal defiitio, make the uits of the start ad stop times secods ot idex umber. Use this fuctio to geerate a sample siusoid with the followig defiitio: Sigal freq= 1200 Hz Samplig freq = 8 KHz Iitial Phase= 45 deg Startig Time= 0 s Amplitude = 50 Edig Time = 7 ms
Project 1: Basic Sigals 9/14 Make two plots of the resultig sigal: oe as a fuctio of time t (i millisecod), ad the other as a fuctio of the sample idex used i t=ts. Determie the legth of the resultig discrete-time sigal ad the umber of periods of the siusoid icluded i the vector. (b). Show by mathematical maipulatio that samplig a cosie at the times t = +3/4 T will result i a discrete-time sigal that appears to be a sie wave whe f0 = 1/T. Use the fuctio from part (a) to geerate a discrete-time sie wave by chagig the start ad stop times for the samplig. 1.2.4 Exercise: Basic Sigals-Expoetials The decayig expoetial is a basic sigal i DSP because it occurs as the solutio to liear costat-coefficiet differece equatios. (a). Study the followig MATLAB fuctio to see how it geerates a discrete-time expoetial sigal. The use the fuctio to plot the expoetial x [ = (0.9) over the rage = 0,1, 2,..., 20. fuctio y = geexp(b,0,l) %GENEXP geerate a expoetial sigal: b^ % usage: y = geexp(b,0,l) % b iput scalar givig ratio betwee terms % 0 startig idex (iteger) % L legth of geerated sigal % y output sigal y(1:l) if(l<= 0) error('genexp: legth ot positive') ed =0+[1:L]'-1; %---vector of idices y=b.^; ed (b). I may derivatios,the expoetial sequece a u[ must be summed over a fiite rage. This sum is kow i closed form: L 1 = 0 a L 1 a = 1 a, for a 1 (1.6)
MPF04 Aálise de Siais e Aquisção de Dados 10/14 Use the fuctio from part (a) to geerate a expoetial ad the sum it up; compare the result to formula (1.6). (c). Oe reaso the expoetial sequece occurs so ofte i DSP is that time shiftig does ot chage the character of the sigal. Show that the fiite-legth expoetial sigal satisfies the shiftig relatio: y[ = ay[ 1] over the rage1 L 1 (1.7) By comparig the vectors y(2 :L) ad a *y(1:l-1). Whe shiftig fiitelegth sigals i MATLAB, we must be careful at the edpoits because there is o automatic zero paddig. (d). Aother way to geerate a expoetial sigal is to use a recursive formula give by a differece equatio. The sigal y[=a u[ is the solutio to the followig differece equatio whe the iput, x[, is a impulse: y[ ay[ 1] = x[ iitial coditio : y[ 1] = 0 (1.8) Sice the differece equatio is assumed to recurse i a causal maer (i. e., for icreasig ), the iitial coditios at =-1 is ecessary. I MATLAB the fuctio filter will implemet a differece equatio. Use filter to geerate the same sigal as i part (a) (i. e., a = 0.9). 1.3 Project 2: Complex-Valued Sigals This project ceters o the issues ivolved with represetig ad geeratig complex-valued sigals. Although i the real world, sigals must have real values, it is ofte extremely useful to geerate, process, ad iterpret real-valued sigal pairs as complex-valued sigals. This is doe by combiig the sigals ito a pair, as the real ad imagiary parts of a complex umber, ad processig this pair with other complex-valued sigals
Project 2: Complex-Valued Sigals 11/14 usig the rules of complex arithmetic. Use of sigal pairs i this way is a importat part of may sigal processig systems, especially those ivolvig modulatio. Complex expoetials are a class of complex sigals that is extremely importat because the complex amplitude (phasor otatio) provides a cocise way to describe siusoidal sigals. Most electrical egieerig studets are familiar with phasors i coectio with ac circuits or power systems, but their use i radar, wave propagatio, ad Fourier aalysis is just as sigificat (although the term phasor is ot always used). Hits I MATLAB, the fuctios real ad imag will extract the real ad imagiary parts of a complex umber. Whe plottig a complex vector, the defaults for plot ad stem ca be cofusig. If z is complex, the plot(z) will plot the imagiary part versus the real part; ad plot(,z) will plot the real part of z versus. However, stem(z) will just plot the real part. If you wat to view simultaeous plots of the real ad imagiary parts, the subplot(211) ad subplot(212) commads prior to each stem commad will force the two plots to be placed o the same scree, oe above the other. See Figura 1.2, which was created usig the followig code: = 0:25; xx = exp(j*/3); %--- complex expoetial subplot (211) stem(,real(xx)) title('real PART'), xlabel('index ()') subplot (212) stem(,imag(xx)) title('imag PART'), xlabel('index ()')
MPF04 Aálise de Siais e Aquisção de Dados 12/14 1 REAL PART 0.5 0-0.5-1 0 5 10 15 20 25 INDEX () 1 IMAG PART 0.5 0-0.5-1 0 5 10 15 20 25 INDEX () Figura 1.2 - Plottig Real ad Imagiary Parts of a Discrete-Time Sigal with subplot 1.3.1 Exercise: Complex Expoetials The real expoetial otatio ca be exteded to complex-valued expoetial sigals that embody the sie ad cosie sigals. These sigals form the basis of the Fourier trasform. (a). I MATLAB a complex sigal is a atural extesio of the otatio i Exercise 1.4. Thus the parameters a ca be take as a complex umber to geerate these sigals. Recall Euler's formula for the complex expoetial (i a form that gives a sigal): x[ = ( z 0 ) = e (l Z + j z ) jθ 0 0 = r e = r (cosθ + jsiθ ) (1.9) where zo=re jθ =r θ. Use this relatioship to geerate a complex expoetial with z0= 0.9 45. Plot the real ad imagiary parts of x[ over the rage 0 20. Notice that the agle of z0 cotrols the frequecy of the siusoids. (b). For the sigal i part (a) make a plot of the imagiary part versus the real part. The result should be a spiral. Experimet with differet agles for θ - a smaller value should produce a better picture of a spiral. (c). Equatio (1.9) is ot geeral eough to produce all complex expoetials. What is missig is a complex costat to scale the
Project 2: Complex-Valued Sigals 13/14 amplitude ad phase of the siusoids. This is the so-called phasor otatio: jφ jθ j( θ+φ) G. z0 = Ae r e = Ar e = Ar [cos( θ + φ) + jsi( θ + φ)], (1.10) where G = A e jφ = A φ is the complex amplitude of the complex expoetial. Geerate ad plot each of the followig sequeces. Covert the siusoids to complex otatio; the create the sigal vector usig exp. If the sigal is purely real, it should be geerated by takig the real part of a complex sigal. I each plot, the horizotal () axis should exted oly over the rage idicated ad should be labeled accordigly. π π x1[ = 3si( ) + j4cos( ), 0 20, 7 7 π x2[ = si( ), 15 25, 17 π π x3[ = 1.1 si( + ), 0 50, 11 4 π x4[ = 0.9 cos( ), 10 20. 11 For each sigal, determie the values of amplitude ad phase costats that have to be used i G; also the agle ad magitude of zo. (d). These same complex expoetials ca be geerated by first-order differece equatios (usig filter): y [ = z0 y[ 1] + x[. (1.11) The filter coefficiet, z0=re jθ, is a complex umber. The ratio betwee successive terms i the sequece is easily see to be z0; but the correct amplitude ad phase must be set by choosig a complex amplitude for the impulse which drives the differece equatio (i. e., x[ = Gδ[). Use filter to create the same sigals as i part (c). Verify by plottig both the
MPF04 Aálise de Siais e Aquisção de Dados 14/14 real ad imagiary parts of y[ ad comparig to the sigals geerated via exp. (e). I the first-order differece equatio (1.11), let yr[ ad yi[ deote the real ad imagiary parts of y[. Write a pair of real-valued differece equatios expressig yr[ ad yi[ i terms of yr[-1], yi[- 1], x[ ad r, cosθ, ad siθ. (f). Write a MATLAB program to implemet this pair of real equatios, ad use this program to geerate the impulse respose of equatio (1.11) for r = 1/2 ad θ = 0, ad θ = π/4. For these two cases, plot the real part of the impulse resposes obtaied. Compare to the real part of the output from the complex recursio (1.11).