You are on page 1of 4

EECE 359 MATLAB Exercise 4 1 Introduction

This assignment gives Matlab examples for the material covered in Chapter 4 of the Lecture Notes. Commands indicated by the >> should be typed 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 Continuous Fourier Transform


Chapter 4 of the Lecture Notes discusses the continuous Fourier transform. The problem with trying to approximate these transforms using numerical techniques in Matlab is that, as we discussed in exercise 1, Matlab does not support continuous-time signals. Thus at rst glance it might seem rather difcult to calculate continuous Fourier transforms numerically in Matlab. However, we will examine a technique which will give a good approximation!

2.1

Approximating the Fourier Transform

As we discussed in Matlab Exercise 1, we can approximate a continuous-time signal using a very nely-sampled discrete version (i.e. by using a very small step size). Lets do this for the function cos(t): >> dt = 0.001; >> t = 0:dt:2*2*pi; >> x = cos(t); This creates a discrete-time approximation to cos(t) using a step size of 0.001 seconds, for two periods of the function (its period is 2 ). Have a look at the signal: >> plot(t,x); Now, we will use the Matlab fft command in order to calculate the Fourier series coefcients of the signal, which will give us an approximation to the Fourier transform: >> >> >> >> >> >> >> f = fftshift(fft(x)); N = length(f); n = -(N-1)/2:(N-1)/2; w = 2*pi*n/N/dt; f = f ./ N; stem(w,real(f)); axis([-10 10 -1 1]);

Lets look at these commands one-by-one: f = fftshift(fft(x)): Here we are calculating the Fourier series coefcients. The command fftshift modies the output of fft so that the zero-frequency component is in the middle of the vector f. (When we used fft in Exercise 3, we looked at the spectrum from 0 to 2 . Now, we look at a spectrum symmetric about = 0.) N = length(f): We will need the length of f to do some normalization below. n = -(N-1)/2:(N-1)/2: Vector n contains the indices of vector f, although these arent the actual frequency indices. Those are in w. w = 2*pi*n/N/dt: These are the frequency indices for each of the elements of f. f = f ./ N: We normalize by a factor N so that the output of fft agrees with the coefcients as dened in the Lecture Notes (recall the discussion from Section 2.1 of Matlab Exercise 3). stem(w,real(f)): We plot the real part of the spectrum (since we know from page 106 of the Lecture Notes what the spectrum should be). axis([-10 10 -1 1]): Zoom in on the portion of the spectrum of interest to us. Now that we have understood the code used to get the Fourier transform approximation, lets examine the results. If you compare the plot you obtain with the exact Fourier transform from page 106 of the Lecture Notes, you will notice a close similarity. Specically, we have determined the Fourier series coefcients of this signal, and if we multiply them by 2 we have the same result as the gure.

2.2

Some experiments to try

You should try modifying the Matlab code above to look at: 1. Non-integer periods: what happens if we dont use an integer number of periods of the signal? Try changing the time vector t to (for example) 2.5 periods. How does this affect the Fourier transform output? What about if you use 2.3 periods? What effect are you seeing here? 2. Fourier transform of sin(t): Try calculating the Fourier transform for sin(t) as shown on page 106. (Hint: we expect that the coefcients will be imaginary, so you should modify the code accordingly).

2.3

Non-periodic signals

Now lets look at the aperiodic square wave example from pages 9798 of the Lecture Notes:

>> >> >> >> >>

dt = 0.001; t = 0:dt:10; Nx = length(t); x = [ones(1,1/dt)]; x = [x zeros(1,Nx-length(x))];

Take the Fourier transform as we did before: f = fftshift(fft(x)); N = length(f); n = -(N-1)/2:(N-1)/2; w = 2*pi*n/N/dt; f = f ./ max(f); stem(w,real(f)); axis([-20 20 -1 1]); Compare this with page 98 of the Lecture Notes. What do you see? Now, try changing the length of the time window, i.e.: >> t = 0:dt:20; Re-calculate the Fourier coefcients and re-plot them. Try this for several larger window sizes. You should see the stem plot starting to look like a continuous-time function as you increase the time-domain window (and thus increase the resolution in the frequency domain).

3 The Convolution Property


We have learned in Sections 4.4 and 4.5 of the Lecture Notes that the time-domain and Fourier-domain have a relationship whereby convolution in one domain is equivalent to multiplication in the other domain. In Matlab Exercise 2 we investigated convolution in the time-domain. Now, we will re-examine this topic with the knowledge we have gained from Chapter 4 of the Lecture Notes. First, lets create an approximation of a continuous-time square wave: >> >> >> >> dt = 0.001; t = 0:dt:10; Nx = length(t); x = [ones(1,floor(N/2)) zeros(1,ceil(N/2))];

Now, lets calculate the time-domain convolution of two of these square waves: >> >> >> >> y = conv(x,x); Ny = length(y); ty = 0:dt:dt*(Ny-1); plot(ty,y);

Now that we know what output to expect, lets try using the convolution property (page 118 of the Lecture Notes): >> >> >> >> >> f = fft(x,Ny); g = f.*f; z = ifft(g,Ny); tz = ty; plot(tz,z);

You should obtain the same result as you did before from the convolution method. (The extra parameter to fft and ifft results in the vectors having Ny points, the correct number we need to compare with the time-domain convolution case). To convince yourself further, try doing this with other signals, such as the convolution of cos(t) and a square wave: >> >> >> >> >> dt tx Nx x1 x2 = = = = = 0.001; 0:dt:2*2*pi; length(tx); [ones(1,floor(Nx/2)) zeros(1,ceil(Nx/2))]; cos(tx);

First, the time-domain convolution: >> >> >> >> y = conv(x1,x2); Ny = length(y); ty = 0:dt:dt*(Ny-1); plot(ty,y);

Now, the frequency-domain multiplication: >> >> >> >> >> >> f1 = fft(x1,Ny); f2 = fft(x2,Ny); g = f1.*f2; z = ifft(g); tz = ty; plot(tz,z);

Again, you will obtain the same results.

You might also like