Lab Manual EKT230/4 Signals & Systems LABORATORY 5 FOURIER REPRESENTATION 1.0 OBJECTIVES: 1.1 To determine the DTFS coefficients of the signal. 1.2 Building a square wave from DTFS coefficients and partial-sum approximation. 1.3 Use the fft, fftshift and ifft MATLAB commands. 1.4 Do Fourier Series problems without integration or indeterminate. 1.5 To study the relation in the time domain between a continuous time signal and discrete time signal. 1.6 To study relation between the continuous time Fourier transform (CTFT) and DTFT In frequency domain. 2.0 THE DTFS ( DISCRETE-TIME FOURIER SERIES ) Commands covered to evaluate DTFS are, fft ifft The DTFS representation of a periodic signal x[n] with fundamental period N and fundamental frequency Ω0 = 2N is given by x[n] N 1 X [k ]e jk 0 n ______(equation 2.1(a )) K 0 X [k ] Where, 1 N N 1 x[n]e jk 0 n ______(equation2.1(b)) n 0 are the DTFS coefficients of the signal x [n]. We say that x [n] and x [k] are a DTFS pair and denote this relationship as; DTFS ; Ω0 x [n] X [k] = frequency domain representation for x[n] Any periodic discrete-time signal maybe describe in terms of equation 2.1 (a) Given a length-N vector x representing one period of an N periodic signal x [n], the command >> x = fft (x) / N 46 Lab Manual EKT230/4 Signals & Systems Produces a length-N vector x containing the DTFS Coefficient X[K]. MATLAB assumes that the summation in both equations above run from 0 to N-1, so the first elements of x and X correspond to x [0] and X [0], respectively, while the last elements correspond to x [N-1] and X [N-1]. Note that division by N is necessary because fft evaluates the sum in equation 2.1(b) without dividing by N. Similarly, given DTFS coefficient in a vector X, the command >> x = ifft(x)*N Produces a vector x that represents one period of the time domain waveform. Note that ifft must multiplied by N to evaluate equation 2.1(a) Example 2.1.1 Determine the DTFS coefficients of the signal using MATLAB. x [n] = 1 + Sin ( x [n] n 3 ) 12 8 DTFS ; 2π/24 X [K] The signal has period 24, so we define one period and evaluate the DTFS coefficients using the following commands. >> x = ones (1,24) + sin ( [0:23]*pi/12 + 3*pi/8 ); >> X1= fft (x)/24 >> X1 = ? ( Note that MATLAB uses i to denote the square roots of -1), your answer will be X [K] = 1 0.4619-j0.1913 0.4619+j0.1913 0 K=0 K=1 K = 23 otherwise on 0 ≤ K ≤ 23 Using ifft, we may reconstruct the time-domain signal and evaluate the first form values of the reconstructed signal with the commands. >> xrecon = ifft (X1)*24; >> xrecon (1:4); ans = 1.9239 1.9914 - 0.0000i 1.9914 1.9239 – 0.0000i Note that the reconstructed signal has an imaginary component, even through the original signal was purely real. 47 Lab Manual EKT230/4 3.0 Signals & Systems THE FOURIER SERIES (FS) The FS coefficients are known as frequency-domain representation of x(t) because each FS coefficient is associated with a complex sinusoid of a different frequency. Continuous-time periodic signals are represented by the FS. x[k ]e x[n] jk 0 n ______(equation6.2(a )) K T X [k ] 1 x[t ]e jk0t dt ______(equation6.2(b)) T0 FS ; x(t) ω0 X[k] x(t) and X[k] are an FS pair; The infinite series in equation 6.2(a) is not guaranteed to converge for all possible signals ^ x[n] x[k ]e jk0 n ______ t K A periodic signal x(t) has a FS if it satisfies the Dirichlet condition given by; 1. x (t) is absolutely integrable over any period; that is a T x[t ] dt for any a 0 2. x (t) has a finite number of maxima and minima over any period. 3. x (t) has only a finite number of discontinuities over any period. Example 3.0.1 Building a square wave from DTFS coefficients The contribution of each term to the square wave maybe illustrated by defining the partial-sum approximation to x[n]in equation below: x[t ] B[k ] cos(k t ) K xˆ J [t ] 0 __________ (equation 6.3(c)) B[k ] cos(k t ) K 0 _________ xJhat (equation 6.3(d)) 48 Lab Manual EKT230/4 Signals & Systems where J ≤ N . This approximation contains the first 2J+1 terms centered on k = 2 0 in Equation 6.3(d). Assume a square wave has period N = 50 and M = 12. Evaluate one period of the Jth term in equation 6.3(d) and the 2J+1 term x̂ J[n] for J=1,3,5,23 and 25. approximation by using the following commands: >> clear >> k=1:24; >> n=-24:25; >> B(1)=25/50; >> B(2:25)=2*sin(k*pi*25/50)./(50*sin(k*pi/50)); >> B(26)=sin(25*pi*25/50)/(50*sin(25*pi/50)); >> %coefficient for k=N/2 >> xJhat(1,:)=B(1)*cos(n*0*pi/25); >> %term in sum for k=0 >> %accumulate partial sums >> for k=2:26 xJhat(k,:)=xJhat(k-1,:)+B(k)*cos(n*(k-1)*pi/25); end >> stem(n,xJhat(6,:)) Example 3.0.2 R x(t) + + - C - RC Circuit This example exploits linearity and the FS representation of the square wave in order to determine the output of an LTI System. If x(t ) X [k ]e jk0t , k y (t ) Then the output is H ( jkw ) X [k ]e k 0 jk0t , Where H(j) is the frequency response of the system. Thus, FS ; ω0 49 Lab Manual EKT230/4 Signals & Systems y(t) Y[k] = H(jk0) X[k] The frequency response of the RC Circuit was determined to be H ( j ) 1 RC j 1 RC X [k ] & 2Sin (k 2 (T0 T )) k 2 Now, substituting for H(jk0) with RC = 0.01, 0 = 2 and using T0 T = ¼ , givens, sin( k / 2) 100 Y [k ] k j 2k 100 The Y[k] go to zero in proportion to 1/k2 as k increases, so a reasonably accurate representation of y(t) maybe determined with the use of a modest number of terms in the FS. We plot the magnitude and phase spectra of X[k] and Y[k], and we determine y(t) using the approximation y (t ) 100 Y [k ]e jk0t k 100 From the above explanation, the approximation involves the exponential FS Coefficient with indices -J≤ k ≤ J. Let using 20 samples per period, we obtain TS T . Note that the total number of sample in one period is then 20 (20 J max) Jmax. Assuming Jmax =200 and T=1, we may compute the partial-sum Y[K]. >> clc >> clear >> % Set KV into 2 portion and at K = 101 Y = 1/2 k = 0 >> KV = [-100:100]; KV1 = [-100:-1]; KV2 = [1:100] >> Y(101) = 0.5; >> % Compute Y[K] and set RC = 0.01 or 1/RC = 100 >> RC = 0.01; >> Y([1:100 102:201])=(1/RC)*Sin([KV1 KV2].*Pi/2)/((j*2*pi[KV1 KV2]+1/RC).* [KV1 KV2]*Pi); >> % Set, Jmax and sampling time delta. >> Jmax=200; T=1; delta=T/(20*Jmax); 50 Lab Manual EKT230/4 Signals & Systems >> % Set time t for the process >> t=[-(10*Jmax-1):10*Jmax]*delta; y=zeros(1,length(t)); >> % initial y >> % Create loop to find y(t) for K=-100 to 100 for k=1: Jmax+1 y=y+Y(k)*exp(j*t*KV(k)*2*pi); end >> % Plot figure for the response >> figure (1) >> Plot (t,abs(y)), xlabel(‘t),ylabel(‘y(t)’) >> axis tight 4.0 RELATING THE DTFS TO THE DTFT Equation 7.1 states that the DTFS coefficient of a finite-duration signal correspond to samples of the DTFT, divided by the number of DTFS coefficients, N. X [k] = 1 X ( e j ) N k 0 … ( equation 7.1) As discussed in the previous LAB, the MATLAB routine ‘fft’. Calculates N times the DTFS coefficients, hence ‘fft’ directly evaluates samples of the DTFT of a finite duration signal. The zero padding process involves appending zeros to the finite-duration signal before computing the DTFS and the results in a denser sampling of the underlying DTFT. Zero padding is easy accomplished command ‘fft’ by adding and argument that specifies the number of coefficient to compute. If ‘x’ is a length-M vector representing a finite duration time signal and ‘n’ is greater than ‘M’, then the command x = fft(x,n) evaluates ‘n’ samples of the DTFT of x by first padding x with training zeros to length ‘n’. If n is less than M, then fft(x,n) first truncates x to length n. The frequency values corresponding to the samples in x are represented by a vector n point long command the first element zero and the remaining entries 51 Lab Manual EKT230/4 Signals & Systems spaced at intervals 2 . For example, the command w = [0:( n-1 )]*2*pi/n n generates the appropriate vector of frequencies. Note that this describes the DTFT for 0 ≤ 2. It is sometimes more convenient to view the DTFT over a period centered on zero. That is - < ≤ . The command y = fftshift (x) swaps the left and right values of x in order to put the zero-frequency value in the center. The vector of frequency value corresponding t . The values in y maybe generated by using : = [-n/2 : (n/2-1)]*2*pi/n Example 4.0.1 Sampling the DTFT of a Cosine Pulse: Consider the signal 3 8 cos x [n] = n 0 , 0 n 31 , otherwise Using MATLAB to evaluate X (ej)at intervals in frequency of (a) 2 32 (b) 2 60 and (c) 2 120 Solution 4.0.1; For case (a) we use a 32 point DTFS computed from the 32 nonzero values of the signal. For the cases (b) and (c), we zero pad to the length 60 & 120 respectively. To sample the DTFT at the specified interval, we evaluate and display the results on -π n π . Using the following commands and observe the result. >> n = [0:31]; >> x = cos (3*pi*n/8): >> X32 = abs(fftshift (fft (x))); % Magnitude for 32 points DTFS >> X60 = abs(fftshift (fft (x,60))); % Magnitude for 60 points DTFS >> X120 = abs(fftshift (fft (x,120))); % Magnitude for 120 points DTFS >> w32 = [-16:15]*2*pi/32; w60 = [-30:29]*2*pi/60; >> w120 = [-60:59]*2*pi/120; >> stem (w32,X32); stem (w60,X60); stem (w120,X120); 52 Lab Manual EKT230/4 5.0 Signals & Systems COMPUTATIONAL APPLICATIONS OF THE DTFS As previously noted, MATLAB’s fft command maybe used to evaluate the DTFS and thus is used for approximating the FT. In particular, the fft is used to generate the DTFS approximation. Example 5.0.1 DTFS Approximation Of Sinusoids: Use the DTFS to approximate the FT of the periodic signal; x(t) = cos (2(0.4)t) + ½ cos (2(0.45)t, Assume that the frequency band of interest is -10π 10π and the desired sampling interval is ∆ω = 20/M. Evaluate the DTFS approximation for resolutions of (a) ωr = rad/s 2 and (b) ωr = 100 rad/s Solution 5.0.1: First note that the FT of x(t) is given by X(jω) = πδ( + 0.8) + πδ( - 0.8) + δ( + 0.9) + δ( + 0.9) 2 2 The maximum frequency of interest is -10 rad/s, and this is much large than the highest frequency in X(jω), so aliasing is not a concern and we choose ωs = 2 ωa. This gives Ts = 0.1s. The number of samples, M is determined by substituting ωs into Equation M i.e M s r 20 r To obtain the resolution specified in case (a), we require that M 40 samples, while in case (b) we need M 2000 samples, we shall choose M = 40 for case (a) and M = 2000 for case (b). We substitute ∆ω = 20 .Equation N s , with M equality to obtain N = M, and thus no. zero padding is required N s M 20 @ s = 20 , N=μ The signal is weighted sum of complex sinusoids, so the underlying FT is a weighted sum of shifted window frequency responses and is given by 53 Lab Manual EKT230/4 Signals & Systems yδ(jω) = ½ W δ( j ( + 0.8)) + ½ W δ( j ( - 0.8)) + ¼ W δ( j ( + 0.9)) + ¼ W δ( j ( - 0.9)) Use the following command to observe the approximating the FT. >> ta = 0:0.1:3.9; % Time samples for case (a) >> xa = cos (0.8*pi*ta) + 0.5* cos (0.9*pi*ta); >> ya = abs (fft (xa)/40); >> ydela = abs (fft (xa,8192)/40); % Evaluate 1/M Y- delta (jomega) for case (a) >> fa = [0:19]*5/20; >> fdela = [0:4095]*5/4096; >> plot (fdela , ydela (1:4096)); >> hold on >> stem (fa , ya (1:20)); >> xlabel (‘Frequency (Hz)’); ylabel (‘Amplitude’) >> tb = 0:0.1:199.9; % Time samples for case (b) >> xb = cos (0.8*pi*tb) + 0.5* cos (0.9*pi*tb); >> yb = abs (fft (xb)/2000); >> ydelb = abs (fft (xb,16000)/2000); % Evaluate 1/M Y- delta (jomega) for case (b) >> fb = [0:999]*5/1000; >> fdelb = [0:7999]*5/8000; >> plot (fdelb (560:800), ydelb (560:800)); >> hold on >> stem (fb (71:100), yb (71:100)); >> xlabel (‘Frequency (Hz)’); ylabel (‘Amplitude’) Note that here we evaluated 1/M yδ(jω) by using fft, and zero padding with a large number of zeros relative to the length of x[n]. Recall that zero padding decreases the spacing between samples of DTFT that are obtained by the DTFT. Hence, by padding with a large number of zeros, we capture sufficient detail such that plot provides a smooth approximation to the underlying DTFT. If plot is used to display the DTFS coefficient continued zero padding, then a much coarser approximation to the underlying DTFT is obtained. For case (b), using plot and stem, the coefficient is obtained via the following commands: >> plot (fb(71:100), yb(71:100)) >> hold on >> stem(fb(71:100), yb(71:100)) 54 Lab Manual EKT230/4 Signals & Systems TASKS OF THE DAY Q 5.1 (a) Use the defining equation for the DTFS coefficients to evaluate the DTFS representation of the following signals: (a) x[ n] cos( 6n ) 17 3 (b) x[n] 2 sin( 14n 10n ) cos( ) 1 19 19 (b) Repeat by using MATLAB’s fft command Q 5.2 Using MATLAB, solve the following problem Let the partial-sum approximation to the FS in equation x[t ] B[k ] cos( k 0 t ) , be given by K 0 J xˆ[t ] B[k ] cos( k 0 t ) K 0 This approximation involves the exponential FS coefficient with indices –J ≤ k ≤ J. Consider a square wave with T = 1 and To/T = ¼. Depict one period of the Jth term in this sum, and find x J[t] for J = 19, 29, and 59. coefficient of the Square Wave is X [k] = 2 sin( k 2 T0 T ) k 2 Q 5.3 Use the MATLAB command fft to derive both the DTFT, X(ej), and DTFS, X[k],of x[n], assuming a period N>31. Evaluate and plot X(ej) and NX[k] for N = 32, 60 and 120. x [n] = cos (5/8 n) , 0 n 31 0 , otherwise Q 5.4 Use the MATLAB command fft to approximate the FT of the signal x(t) = e-t/10 u(t) (cos (10t) + cos (12t)) Assume that the frequency band of interest is -20 < < 20 and the desired sampling interval is ∆ω = /20 rad/s. Compare the approximation with the underlying FT for resolutions of ωr = 2 rad/s. 55 Lab Manual EKT230/4 Signals & Systems Q 5.5 Using zero padding and the MATLAB commands fft and fftshift to sample and plot Y(ej) at 512 points - < . Consider the signal x[n] = cos ( 7π/16 n) + cos ( 9π/16 n ) Evaluate the effect of computing the DTFT, using only the 2M + 1 values x[n], n≤ M. Hint : x[n] N 1 X [k ]e jk0n K 0 DTFT X (e j ) 2 X [k ] ( k) k 56