Sei sulla pagina 1di 11

Scilab Week 7

October 5, 2009
In this assignment, the focus will be on Fourier Transforms to solve various problems Study the spectra of data Solve differential equations Filter data to extract interesting information

Fourier Analysis
1 2

The Fourier Transform is dened by F ( ) = f (t )e j t dt ,

work in frequency domain, solve our problem there, and invert to get the solution

f (t ) =

F ( )e j t d

Note that you can put the factor of 1/2 in front of either integral. Also note that the sign of the exponent is opposite for the two integrals. You have studied in Maths and in Networks that F ( ) is unique given f (t ) The Fourier transform is a linear transformation. i.e., the transform of a linear combination of f (t ) and g(t ) is the same linear combination of F ( ) and G( ). The transform of a convolution is the product of the transforms, i.e., if

h(t ) =

f ( )g(t )d

then H ( ) = F ( )G( ) The transforms of a derivitive and indenite integral are simpler in the Transform domain: F
t

df ,t dt f d , t

= =

j F ( ) F ( ) j

Many problems are solved using Fourier transforms. In electrical engineering, the following are some of the types of ways we use Fourier transforms: 1

Harmonic content of nonlinear circuits driven by a phasor source. For instance if a diode switches on and off based on the sign of the voltage across it, what harmonics are induced? Fourier spectrum of AM and FM modulated signals. Frequency response of a circuit. This has been done last time using Laplace transfroms, but it can be done in the Fourier domain as well. Studying the transfer function of a system to know if it is stable under feedback. This is a crucial part of control systems theory and analog circuit theory. The spectrum of noise. Central to communications theory and control systems theory. Filter design. We want to isolate a desired part of the spectrum. How to do it in fourier domain, and how to do it in time domain itself. System identication: An earthquake has occurred and you have the trace. How to analyse it, knowing the type of trace an earthquake can give to identify where and when it occurred and what type and how strong it was. This often uses fourier methods as part of the process of system identication. Adaptive circuits: You want to listen to a radio station, but its transmission frequency is not constant (or your receivers tuner is not stable). How to lock the receiver to the transmitted signal? These are known as phase locked loops. Also analysed and designed using fourier methods, since what we are tracking is a frequency signal. We will take some sample problems here to look at.

Digital Fourier Transforms

Now, computers are not good at performing continuous integrals over unbounded domains, and so these equations are not useful as they are. However, we know that the Fourier Transform is just the limit of the Fourier Series. So we can solve the Fourier Series problem that is nearest to the exact problem and get an approximate solution. But even that is too complicated, since the general function has an innite number of fourier coefcients. So we keep only a nite number of fourier coefcients to get to the computer approximation of the Fourier Transform. Let us take an example: 1 t 2 e f (t ) = 1 + t2 The plot of the function looks as follows:

1.0 0.9 0.8 0.7 0.6 f 0.5 0.4 0.3 0.2 0.1 0.0 3 2 1 0 t 1 2 3

Since the plot goes nearly to zero, we can safely ignore the rest of the function. Even better, we can make copies of f (t ) every 6 seconds to make it a periodic function, g(t ). The plot of g(t ) looks like this

1.0 0.9 0.8 0.7 0.6 g(t) 0.5 0.4 0.3 0.2 0.1 0.0 10

0 t

10

Then a Fourier series will do the job. We write

g(t ) =

n=

cn e jn t

where = 2 /6. Since the function is even and real, the fourier series is also even and real, and so it collapses into a cosine series:

g(t ) =

n=0

an cos (n t )
3

We can compute the coefcients as a0 an = = 1 6 1 3


3

g(t )dt
3 3 3

g(t ) cos (n t ) dt

Using the intg function that is built in, we can compute the integrals. The code is as follows [branch code0:
4

*4 global count; function y=g(t) global count; count=count+1 y=exp(-t.^2)./(1+t.^2); endfunction function y=gn(t) global count; count=count+1; y=exp(-t.^2).*cos(n*omega*t)./(1+t.^2); endfunction omega=2*%pi/6; count=0; a=zeros(21,1); a(1)=intg(0,3,g)/3; for n=1:20 a(n+1)=intg(0,3,gn)/1.5; end bar(0:20,a); xtitle("Fourier coefficients","n","a(n)"); mprintf("Number of times function called: %d\n",count);

] The coefcients look as follows:


Fourier coefficients 0.25

0.20

0.15

a(n)

0.10

0.05

0.00

0.05 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 n

Now, while this works, we are actually doing numerical integration to compute each coefcient. That is quite expensive. If you run the above code, you nd that the function was called 8883 times, or about 450 times per coefcient. In practical situations we often take fourier transforms of very large data sets (could be millions of elements). We need a faster method. This faster method is called the Discrete Fourier Transform or the DFT. You will learn the mathematics in your ADSP course. The main thing about it is that the integral to compute the coefcients collapses to a sum. We rst sample the function f (t ) at N times over a time period t0 , t0 + t , etc to get N samples f0 , f1 , . . . , fN 1 . The DFT is then dened by:
N 1

fn Fk

= =

k=0 N 1

Fk e j2 kn/N
n=0

1 N

fn e j2 kn/N

The coefcients Fn are closely related to the coefcients an in the exact Fourier series. We can go from Fn to a cosine series
N /21

fn = A0 + Comparing we can see that

k=1

Ak cos (2 nk/N )

2 or,

kn n t N

2 t N which is reasonable, since N t should correspond to a period, i.e., 2 / . The DFT is a much better algorithm than the integration method above, since only N evaluations are done. To get 21 coefcients, we would only evaluate the function 21 times. Let us compute the coefcients using the DFT and compare them to the fourier 5

series values. Note that we have to compute 41 coefcients, since each cosine is equivalent to two complex exponentials.
6a

* 4 + N=41; t=linspace(-3,3,N+1);t=t(1:N); This peculiar piece of code is to get 41 samples between 0 and 6, not counting both t = 0 and t = 6, since they are the same point. So I get 42 points including the end points and discard the 42nd point.

6b

* 4 + f=fftshift(g(t)); F=dft(f,-1)/N; A=zeros(21,1); A(1)=F(1); A(2:21)=F(2:21)+F(41:-1:22); clf bar(1:21,[a real(A)]); legends(["exact";"DFT"],2:3);

The result is plotted below

0.40

0.35 exact DFT

0.30

0.25

0.20

0.15

0.10

0.05

0.00

0.05 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

It is obvious that the coefcients are not the same. In fact, they should not be. After all, the DFT is computing the integral using 21 uniformly spaced samples of f (t ). Only if the integral can be exactly calculated from those samples, will the coefcients agree. Which means that the more samples we take, the more accurate will be the approximation. So let us take (say) 256 samples.
7

* 4 + N=256; t=linspace(-3,3,N+1);t=t(1:N); ff=fftshift(g(t)); FF=dft(ff,-1)/N; AA=zeros(21,1); AA(1)=FF(1); AA(2:21)=FF(2:21)+FF(N:-1:N-19); clf bar(1:21,[a real(AA)]); legends(["exact";"DFT-256"],2:3)

Now we get the following:

0.40

0.35 exact DFT256

0.30

0.25

0.20

0.15

0.10

0.05

0.00

0.05 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

The errors are as follows:


8

* 4 + [(1:21) real(A)-a real(AA)-a]

The answer that Scilab gives is shown below 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 4.781E-08 0.0011045 0.0026768 0.0027972 0.0019422 0.0010921 0.0005517 0.0002619 0.0001192 0.0000525 0.0000225 0.0000095 0.0000039 0.0000016 0.0000006 0.0000003 3.128E-08 0.0000001 6.934E-08 - 9.917E-08 0.0000001 - 1.242E-09 2.485E-09 - 2.485E-09 2.485E-09 - 2.486E-09 2.487E-09 - 2.487E-09 2.488E-09 - 2.490E-09 2.491E-09 - 2.492E-09 2.494E-09 - 2.496E-09 2.497E-09 - 2.500E-09 2.502E-09 - 2.504E-09 2.507E-09 - 2.509E-09 2.512E-09 - 2.515E-09

These errors we get for small N are due to the fact that the integral cannot be properly evaluated given only a few points. The technical name for this problem is aliasing, something about which you will learn a lot in your ADSP course.

The Fast Fourier Transform

Well that was good. We could compute a decently accurate Fourier series using a few samples. No need for integration. But to do a good job we needed a lot of samples, and the computation of the sum requires a large number (roughly N 2 ) multiplications of complex numbers. That is still very expensive. Instead of 8000 function evaluations we are now doing 64000 complex multiplications! There is a better way, through an algorithm known as the Fast Fourier Transform or FFT. It computes exactly the same DFT but uses only about 3N log2 N computations. For N = 256, this works out to be about 3000 complex multiplications. As N increases, the savings become huge. As far as Scilab is concerned, the only changes required to use the FFT are: N must be a power of 2. Instead of a command like F=dft(f,-1) we instead have a command like F=fft(f,-1). Thats it. Nothing else changes, but the speed up is huge, and the memory utilization is far less. If you did the dft calculation with a few thousand samples, you would run out of memory. No such problems with fft.

Filtering and Data Analysis

Suppose we have data whose frequencies of interest lie in a range. We can extract that data by suitable ltering. For example, consider f (t ) = A(t ) cos t + B(t ) cos 2t 9

where A(t ) and B(t ) are signals which vary very slowly. Then, we can lter out the cos 2t term and analyse only A(t ). The steps are as follows: Obtain the fourier transform of f (t ). Plot the magnitude spectrum and identify the frequencies you want and the frequencies you want to reject. Zero the entries of F ( ) for those frequencies you want to reject. This operation is called ltering, and in this case, we are low pass ltering since we are throwing away the high frequencies. The main thing to keep in mind is that you must keep both the positive low frequencies and the negative low frequencies, i.e., keep terms corresponding to n = 0 to k and n = N k to N 1 while throwing away the rest. Get back the ltered f (t ) by inverse fourier transforming. Multiply all the terms of the signal with cos t . Fourier transform the signal again. Now we are taking the fourier transform of A(t ) cos2 t = A(t ) Again lter to reject the cos 2t term. Inverse transform to get A(t ) itself. 1 + cos 2t 2

The Assignment
1. Compute the exact fourier series and the DFT for the following periodic function f (t ) = 1 1 + cos2 t

How exact are the solutions if you use 16 terms. How many terms were needed for the error to become negligible? Do you understand this result? 2. Compute the sine fourier series and extract the corresponding terms from the DFT for the function. How many terms were needed for the result to become exact? f (t ) = sin3 (t + 1) 3. Solve the differential equation d2y + y = cos(3t ) dt 2 Note that this is not an initial value problem - we would use Laplace Transforms for that. You will have to Fourier transform the equation, solve it in frequency domain, and then invert back to get the function in real time. To do that, use y=fft(Y,1). Note: You have to gure out how to implement the derivitive in the DFT. Basically a term like e j2 kn/N e jn t and the derivitive will pull out jn . Here your is unity, which gives you the factor to use. 10

Note: Another problem you will face is that of a zero in the denominator. Handle it by adding a tiny number (say 1014 ) to eliminate the singularity. Note: You will have to play around with the factors of N . fft often has the factor of N built in. Look at the fourier transform and decide if the answer is reasonble. How many samples are required for an accurate answer? Verify the answer by direct differentiation. How does the error scale with N ? Do you understand this? 4. Given a signal f (t ) = J0 (0.01t ) cos t + cos 2t where J0 (x) is the the Bessel function of zeroth order. You can compute it in Scilab by besselj(0,x). Create a Scilab function to generate the function f (t ), Sample it from t = 0 to t = 500 with 8 samples per 2 . Filter as explained above to extract the coefcient of cos t . Plot the function extracted along with J0 (0.01t ) itself and compare to see if the extraction worked. This is nothing but an example of AM modulation. There are many uses of the Fourier transform, and you should get very familiar with it.

11

Potrebbero piacerti anche