EECE359: Matlab Exercise 3 1 EECE 359 MATLAB Exercise 3 1 Introduction This assignment gives Matlab examples for the material covered in Chapter 3 of the Lecture Notes. Commands indicated by the >> should be typed in the Matlab Command Window. The Matlab built-in help command provides a lot of useful information. For example, to find out how to use the Matlab fft command, just type help fft in the Matlab Command Window. This exercise builds on the previous Matlab exercises, so please have a look at them if you have not already done so. 2 The Fast Fourier Transform (FFT) Matlab has built-in support for discrete Fourier transforms. Specifically, Matlab implements the Fast Fourier Transform (FFT) as the command fft, and the inverse FFT as the command ifft. Before trying the exercises below, consult the Matlab help, i.e. try running help fft and help ifft. 2.1 Fourier-series coefficients of a square wave In this section, we will examine the Fourier series coefficients of the periodic square wave of page 84 of the Lecture Notes. In particular, we take N1 = 2 and N = 7, and define xs [n] on the interval 0 ≤ n ≤ 6. Enter the following code to create this signals >> ns = [0 1 2 3 4 5 6]; >> xs = [1 1 1 0 0 1 1]; Now, let’s plot the signal in the time-domain to verify it is correct: >> stem(ns,xs); We can also plot several periods of the signal easily, using Matlab’s repmat function: >> xr = repmat(xs,1,5); >> stem(xr); Now we are ready to look at the Fourier series coefficients of xs [n] by using the fft command: >> Xf = fft(xs); >> stem(real(Xf)); EECE359: Matlab Exercise 3 2 Note: we needed to use real in the last line above because sometimes Matlab has slight numerical issues with the FFT and returns complex values even when the imaginary parts are 0. You can verify this yourself by examining the contents of the vector Xf. Verify that the response you see is correct, by calculating the expected Fourier series coefficients using the equations on page 84 of the Lecture Notes. You should have found when doing the calculation above that your Fourier series coefficients were correct up to a scaling factor of 1/N . Why is this? (Hint: look at the definition of the coefficients in help fft and compare it with that found on page 84. What is the difference?) 2.2 More square-wave investigations We will change a few of the parameters of the square wave from Section 2.1 and see how they affect the Fourier series coefficients. 2.2.1 Changing the square-wave duty cycle Try changing the duty cycle of the wave, and look at the resulting Fourier series: >> >> >> >> ns = [0 1 2 3 4 5 6 7 8]; xs = [1 1 1 0 0 0 0 1 1]; Xf = fft(xs); stem(real(Xf)); How does this affect the Fourier series coefficients? 2.2.2 Square waves with even N In our previous experiments have used square waves with odd N . Try creating a wave with even N : >> >> >> >> ns = [0 1 2 3 4 5 6 7 8 9]; xs = [1 1 1 0 0 0 0 0 1 1]; Xf = fft(xs); stem(real(Xf)); What can you say about the Fourier series coefficients of square waves with odd N versus even N ? 2.3 The IFFT We will now try taking the ifft of frequency-domain signals to make sure we end up with the original signal again: EECE359: Matlab Exercise 3 >> >> >> >> >> 3 ns = [0 1 2 3 4 5 6 7 8 9]; xs = [1 1 1 0 0 0 0 0 1 1]; Xf = fft(xs); xb = ifft(Xf); stem(ns,xb) Is the signal that has been passed through the FFT and IFFT (xb) the same as the original signal (xs)? 3 Filtering using FFT and IFFT 3.1 Lowpass filtering a square wave We will now use the FFT and IFFT to do simple filtering (see Section 3.4 of the Lecture Notes for details). First, we will create the same square-wave signal as before: >> ns = [0 1 2 3 4 5 6 7 8 9]; >> xs = [1 1 1 0 0 0 0 0 1 1]; Now, let’s create an ideal lowpass filter (see page 90 of the Lecture Notes) with ωc = π/2. We define it on the interval 0 ≤ ω ≤2π: >> H = [1 1 1 0 0 0 0 0 1 1]; Now, filtering is a simple matter of converting signal xs to the frequency domain, and multiplying it with the filter H: >> Xf = fft(xs); >> Yf = Xf.*H; >> y = ifft(Yf); Finally, let’s plot the original signal xs (in blue) and the filtered signal y (in red): >> stem(ns,xs,’b’); >> hold on; >> stem(ns,y,’r’); You can see that the square wave has lost it’s shape. Can you explain why this happens? (Hint: consider the impact of the different frequency components that make up the square wave, and look at the plots on page 77 of the Lecture Notes. Pay particular attention to the impact of the sinusoids of different frequencies.) EECE359: Matlab Exercise 3 3.2 3.2.1 4 Further filtering investigations Changing the cutoff frequency We will now study what happens we change the cutoff frequency ωc of the lowpass filter. Let’s create a new filter with a higher cutoff frequency, and re-filter xs: >> >> >> >> >> >> H = [1 1 1 1 0 0 0 1 1 1]; Yf = Xf.*H; y = ifft(Yf); stem(ns,xs,’b’); hold on; stem(ns,y,’r’); How has this affected the output signal y?: 3.2.2 Changing the square-wave duty cycle Now let’s try changing the duty cycle of the original signal xs: >> >> >> >> >> >> >> >> xs = [1 1 1 1 0 0 0 1 1 1]; H = [1 1 1 0 0 0 0 0 1 1]; Xf = fft(xs); Yf = Xf.*H; y = ifft(Yf); stem(ns,xs,’b’); hold on; stem(ns,y,’r’); Does this tell you anything about how the duty cycle of a square wave affects its frequency content?