ECEN 242 Wireless Electronics for Communication Spring 212 1-23-12 P. Mathys Problem Set 1 (Solutions are due Mon. 1-3-12) 1 Introduction The goals of this problem set are to use Matlab to generate and analyze waveforms in the time and frequency domains. A function or signal x(t) that is defined for all instants of time in some interval, such as the sinusoid x(t) = A cos(2πft + θ), < t <, is called a continuous-time (CT) function or signal. By sampling such an x(t) at time instants t = nt s, n =..., 2, 1,, 1, 2,..., a discrete-time (DT) function or signal, usually denoted by x n or x[n], is obtained for which the values are only known at integer multiples of the sampling interval T s. We set x n = x[n] = x(nt s ) and say that the signal x(t) has been sampled with sampling frequency F s = 1/T s in samples per second or Hertz. An example of a sine signal, sampled 8 times per period, is shown in the graph below. 1 CT Signal x(t)=sin(2πf t) and DT Signal x n =x(nt s ), f =1 Hz, F s =1/T s =8 Hz x(t), x n =x(nt s ).5.5 1.2.4.6.8 1 1.2 1.4 1.6 1.8 2 t [ms] Intuitively, more detail can be captured if the sampling rate is higher. If the highest frequency in a signal is f m, then it turns out that the process of sampling can be reversed without any loss, provided that F s > 2f m. The frequency 2f m is called the Nyquist rate or frequency. The sampling theorem states that a bandlimited waveform can be reconstructed exactly from its samples at rate F s, provided that F s is at least as large as the Nyquist rate. Digital computers can only work with discrete-time signals. Therefore, all signal processing operations in Matlab have to be performed on DT signals. It is often useful to choose a sampling rate in Matlab that is much higher than the Nyquist rate and to pretend that the corresponding signal is a CT signal. This was done for the CT sinewave signal in the graph above. When the plot command in Matlab is used, then Matlab automatically connects adjacent samples by straight lines, thereby creating the illusion of a CT signal. But 1
fundamentally, all signals in Matlab are DT signals that are stored in vectors and matrices with discrete indexes. To work efficiently in Matlab you need to know a few basic things. All operations can be executed directly at the command prompt, or they can be entered in script and/or function files which are then invoked from the command prompt. If you don t know how to use a command, type help followed by the name of the command at the command prompt. Try help script or help plot to see how this works. To compute something simple like sin(6 ), type sin(pi*6/18) %pi*6/18 converts to radians To generate a row vector x = [59 313] type x = [ 5 9-3 13] When you press enter, Matlab will echo the values that you entered. To suppress this, use a semicolon at the end of your entry as shown below. x = [ 5 9-3 13]; To convert a row vector into a column vector use the transpose operator, e.g., y = [ 5 9-3 13] ; or use the semicolon to start new rows: y = [; 5; 9; -3; 13]; Try multiplying A = x*y size(a) B = y*x size(b) %Print the size of matrix A %Print the size of matrix B One of those two operations is call inner product, the other is call outer product. Both of the multiplication operations are matrix/vector operations. To multiply elements in vectors or matrices (of the same size) individually, use.* instead of * (similarly, use./ or.^ for elementwise division and multiplication, respectively). Here is an example: pow3 = (3*ones(1,1)).^[:9] This computes the powers of 3 from 3 to 3 9. The expression 3*ones(1,1) produces a row vector of length 1, filled with 3 s. The expression [:9] produces a row vector of length 1 with consecutive integers from to 9. Here are some examples which show how to read various subsets from pow3. Note that indexing in Matlab always starts with 1 (and not with ). 2
pow3(2) %Read 2 nd element pow3([2,5,8]) %Read 2 nd, 5 th, and 8 th element pow3([4:7 9]) %Read elements 4,5,6,7 and 9 pow3([end:-1:1]) %Read whole array backwards Matrices work similarly as vectors, except that elements need to be addressed using more than one index. 2 Waveforms in Matlab The Matlab commands shown below can be used to generate (a DT approximation to) a sinusoidal signal of the form x(t) = A cos(2πf t + θ), with amplitude A, frequency f in Hertz, and phase θ. To be able to plot (a close replica of) x(t), as well as hear the effect of sampling, especially for f in the vicinity of one half of the sampling rate, we use two sampling rates, F s1 = 1/T s1 and F s2 F s1 in Matlab. The CT waveform x(t) is produced using the much higher sampling rate F s2 and the DT waveform x[n T s1 ] is produced using the actual sampling rate F s1. Waveform Example Code #1: Fs1 = 8; Fs2 = 2*Fs1; A = 1; f = 125; theta = 45; tlen1 = 1; tlen2 = 5/f; %Sampling rate for sound, Ts1=1/Fs1 %Sampling rate for display %Amplitude %Frequency in Hz %Phase in degrees %Duration in sec for sound %Duration in sec for display tt1 = [:floor(tlen1*fs1)-1]/fs1; %Time axis for sound tt2 = [:floor(tlen2*fs2)-1]/fs2; %Time axis for display xt1 = A*cos(2*pi*f*tt1+(pi/18)*theta); %x[n*ts1] for sound xt2 = A*cos(2*pi*f*tt2+(pi/18)*theta); %x(t) for display sound(xt1,fs1) %Play x[n*ts1] through sound card Continued on next page. 3
ixd = find(tt1<=tlen2); %Find indexes of tt1 where t<=tlen2 subplot(211) plot(1*tt2,*xt2, -k,1*tt2,xt2, -b,1*tt1(ixd),xt1(ixd), or ) grid %Plot x(t) and sound samples xlabel( t [ms] ), ylabel( x(t), x[n*t_{s1}] ) str = Waveform x(t)=a*cos(2*{\pi}*f_*t+{\theta} and x[n*t_{s1}] ; str = [str, f_= int2str(f) Hz, \theta= int2str(theta) deg ]; str = [str, F_{s1}= int2str(fs1) Hz ]; title(str) figure(gcf) The resulting graph which shows x(t) (xt2 in Matlab) as a CT waveform (blue line) and x[n T s1 ] (xt1 in Matlab) as DT samples (red circles) is shown below. 1 Waveform x(t)=a*cos(2*π*f *t+θ and x[n*t s1 ], f =125 Hz, θ=45 deg, F s1 =8 Hz.5 x(t), x[n*t s1 ].5 1.5 1 1.5 2 2.5 3 3.5 4 t [ms] By leaving F s1 fixed and varying f and letting it get close to F s1 /2, the effect of sampling a CT waveform can be studied. 3 Fourier Series and FFT in Matlab The Fourier Series (FS) (and Fourier transforms in general) is a tool that is very frequently used in engineering and in communications and signal processing in particular, to move back and forth between time and frequency representations of signals. Here is the definition of the complex-valued form of the FS. Definition: The Fourier Series (FS) of a periodic continuous time signal x(t) with period T is defined as X k = 1 T T x(t) e j2πkt/t dt, k =, ±1, ±2,..., where the integration is taken over any interval of length T. The FS coefficients X k correspond to frequency components at f k = k/t. Frequency f 1 = 1/T is called the fundamental 4
frequency, f 2 = 2/T is called the 2 nd harmonic, f 3 = 3/T is called the 3 rd harmonic, etc. The FS coefficients X k are complex-valued in general and are often displayed in the form of two graphs, a magnitude plot that shows X k versus k (or versus frequency k/t ), and a phase plot that shows X k versus k (or versus frequency k/t ). If the time domain function x(t) is real, then X k and X k are related by X k = X k, where the denotes complex conjugation. The periodic time function x(t) is obtained from the frequency domain coefficients X k using the following theorem. Theorem: Inverse FS. A periodic CT signal x(t) can be recovered uniquely from its FS coefficients X k (provided that they exist) by where T is the period of x(t). x(t) = k= X k e j2πkt/t, Example: Periodic rectangular waveform x(t) with amplitude 1, 5% duty cycle, and period T defined by { 1, mt T x(t) = /4 t < mt + T /4, m integer,, otherwise. The FS coefficients X k are computed as X k = 1 T T x(t) e j2πkt/t dt = 1 T T /4 T /4 e j2πkt/t dt = sin(πk/2) πk, k =, ±1, ±2,.... The graphs below show X k and X k versus k for 5 k 5, corresponding to frequencies 5/T f 5/T. Note that in this particular case the X k are all real and thus X k only shows whether X k is positive or negative for a specific k. 5
.5.4 X k.3.2.1 5 4 3 2 1 1 2 3 4 5 2 15 X k [deg] 1 5 5 4 3 2 1 1 2 3 4 5 k To synthesize x(t) from X k the formula x(t) = k= X k e j2πkt/t, is used. However, in practice only a limited number of X k may be available, e.g., in the range K k K for some finite integer K. In that case x K (t) = K k= K X k e j2πkt/t. The two graphs below show x K (t) for K = 5 and K = 5. The shape of x(t) that these graphs try to approximate is clearly visible, but it is also evident that there are some nonnegligible discrepancies, especially in places where the original x(t) has sharp transitions. 6
1.5 Synthesis of x K (t) from FS Coefficients X k for k up to K=5 1 x K (t), y k (t).5.5 1 1.8.6.4.2.2.4.6.8 1 t [ms] 1.5 Synthesis of x K (t) from FS Coefficients X k for k up to K=5 1 x K (t), y k (t).5.5 1 1.8.6.4.2.2.4.6.8 1 t [ms] The Matlab commands that were used to produce these graphs are shown below. Waveform Example Code #2: Fs = 8; T = 1e-3; tlen = 2*T; K = 5; X = 1/2; kk = [1:K]; Xk = sin(pi/2*kk)./(pi*kk); %Sampling rate %Period %Time duration %Maximum k %dc component %Positive k indexes %FS coefficients Xk tt = [:floor(tlen*fs)-1]/fs; %Time axis tt = tt-tlen/2; %Time axis centered at t= Ayt = zeros(k+1,length(tt)); %Array for component waveforms yk(t) Ayt(1,:) = X*ones(size(tt)); %dc component for k=1:k Ayt(k+1,:) = 2*Xk(k)*cos(2*pi*k/T*tt); %k-th component waveform yk(t) end Continued on next page. 7
xkt = sum(ayt); %Synthesized xk(t) subplot(211) plot(1*tt,ayt ) %Plot component waveforms yk(t) line(1*tt,xkt, Color,[1 1], LineWidth,2) %Add xkt to plot grid xlabel( t [ms] ), ylabel( x_k(t), y_k(t) ) str1 = Synthesis of x_k(t) from FS Coefficients X_k ; str1 = [str1 for k up to K= int2str(k)]; title(str1) figure(gcf) In this particular case, where X k = X k, x(t) was computed as x(t) = K k= K K X k e j2πkt/t ( = X + X k e j2πkt/t + e ) K j2πkt/t = X + 2 X k cos(2πkt/t ). k=1 The waveforms y k (t) that are stored in the array Ayt in the Matlab script are y k (t) = 2 X k cos(2πkt/t ). Computing the FS coefficients X k analytically is not practical for waveforms that were either measured in the lab or generated by circuit simulation, e.g., using LTspice. Most of the time such signals are only available in sampled form at times t = nt s for n =, 1, 2,..., N 1 (total of N samples. In this case it is reasonable to approximate the computation of X k as X k = 1 x(t) e j2πkt/t dt 1 N 1 x(nt s ) e j2πknts/(nts) T s T T NT s = 1 N n= N 1 x(nt s ) e j2πkn/n, where the period T has been set equal to N T s, the total length of the signal assumed to be available. Apart from the factor of 1/N, this is of the same form as the discrete Fourier transform (DFT) which is defined as follows. Definition: The discrete Fourier transform (DFT) of a DT signal x n, n =, 1,..., N 1, that is periodic with period N, is defined as X k = N 1 n= n= x n e j2πkn/n, k =, 1,..., N 1. The term FFT (fast Fourier transform) refers to a fast algorithm for computing the DFT for composite N and, very often, for the case when N is a power of 2. k=1 8
Theorem: Inverse DFT/FFT. A periodic DT signal x n with period N can be recovered uniquely from the DFT coefficients X k, k =, 1,... N 1 (mod N), by x n = 1 N N 1 k= X k e j2πkn/n, n =, 1,..., N 1. The term inverse FFT refers to a fast algorithm for computing the inverse DFT when N is composite, most often when N is a power of 2. Matlab has the commands fft and ifft built-in, and thus it is quite easy to obtain an approximation to the FS coefficients X k by computing the DFT coefficients X k and setting X k 1 N X k. The following Matlab commands compute and display X k for a cosine waveform with frequency f. FS/FFT Example Code #1: Fs = 8; f = 1; tlen =.1; tt = [:floor(tlen*fs)-1]/fs; xt = cos(2*pi*f*tt); N = length(xt); Xk = 1/N*fft(xt); kk = [:N-1]; ff = Fs/N*kk; %Sampling rate %Frequency of waveform x(t) %Time duration %Time axis %Waveform x(t) %Total number of samples %Xk approximated using FFT %Indexes k of Xk %Frequency axis subplot(211) stem(ff,abs(xk),.-b ) grid ylabel( X_k ) str = FS Coefficient Approximation using FFT ; str = [str, f_= int2str(f) Hz, F_s= int2str(fs) Hz ]; title(str) figure(gcf) The resulting graph which shows X k is shown below. 9
.5 FS Coefficient Approximation using FFT, f =1 Hz, F s =8 Hz.4 X k.3.2.1 1 2 3 4 5 6 7 8 Note that, instead of showing positive and negative frequencies (at f and at f ) the graph shows a spectral line at f and at F s f. This comes from the fact that the DFT is periodic in both the time and frequency domains with period N. Therefore, 1 is the same as N 2, 2 is same as N 2, etc. 4 Problems 1) (a) Look at the Waveform Example Code #1 in the section Waveforms in Matlab. Look up the Matlab commands that you are not familiar with (using help command at the command prompt). Work on the code until you understand what it is doing and how it is doing it. Recreate the waveform given with the example code. Describe the main elements of the code in your solution. (b) Use the code from (a) to generate CT and DT sinusoids with frequencies f = 3, 35, 4, 45, 5 Hz with a sampling frequency F s1 = 8 Hz. Listen to the x1t signal and look at the plots for each f. Describe what you would expect to hear if you increase f and what you actually hear. How would you explain the phenomenon that you observe? (c) Generate the following complex-valued waveform xt in Matlab: Fs = 441; f = 1; tlen = 1; j = sqrt(-1); tt = [:floor(tlen*fs)-1]/fs; xt = exp(j*2*pi*f*tt); %Sampling rate %Signal frequency %Time duration %Imaginary unit %Time axis %Complex exponential Listen to and plot (using suitable scaling of the time axis) real(xt), imag(xt), and abs(xt). What is your interpretation of the three signals? 2) (a) Study the Matlab code given in Waveform Example Code #2. Recreate the graphs that were produced using this code in the section on Fourier Series and FFT in Matlab. Describe the main elements of the Matlab code. 1
(b) The following FS coefficients are given: X k = 2 π ( 1) k, k =, ±1, ±2,.... 1 4k2 Synthesize x(t) using T = 1 ms for K k K when K = 5 and K = 5. Include labeled graphs in your solution. What is the most likely original wavefom x(t)? What happens if you set X = when you synthesize x(t)? (c) Repeat (b) for the following FS coefficients: X k = j e j2πkτ/t 1 2πk, k =, ±1, ±2,.... Use T = 1 ms and τ = T /3 and τ = T /5. How does the value of τ affect the values of the spectral components X k? Are any of the X k zero? If so, how are the indexes of these X k related to the value of τ? Note that you will have to modify the Waveform Example Code #2 because in this case it is not true that X k = X k. 3) (a) Look at the FS/FFT Example Code #1 in the section on Fourier Series and FFT in Matlab. Recreate the example figure and describe the main elements of the code. What happens if you change f from 1 to 15 Hz? Why do you think this happens? (b) In the FS/FFT Example Code #1 change the statement for xt to xt = sign(cos(2*pi*f*tt)); %Waveform x(t) and run the script file again. Plot the waveform x(t) and describe it. Then look at the FS coefficients X k. Are they what they should be for the waveform that you saw? Try setting F s = 8 Hz (instead of 8 Hz). Are the X k now closer to what you expected? What conclusion can you draw for the selection of F s and N? (c) Generate approximately.1 sec of the waveform you found in 2b with a sampling rate of F s = 8 Hz. Then use the FFT approximation for FS coefficients from the section on Fourier Series and FFT in Matlab to compute and display X k. Compare with the X k given in 2b. c 212, P. Mathys. Last revised: 1-23-12, PM. 11