Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October...

11

Click here to load reader

Transcript of Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October...

Page 1: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

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

1 Fourier AnalysisThe Fourier Transform is defined by

F(ω) =1

∫∞

−∞

f (t)e− jωtdt ,

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

f (t) =∫

−∞

F(ω)e jωtdω .

Note that you can put the factor of 1/2π in front of either integral. Also note that the signof 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 com-bination 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τ

thenH(ω) = F(ω)G(ω)

• The transforms of a derivitive and indefinite integral are simpler in the Transformdomain:

F(

d fdt

, t→ ω

)= jωF(ω)

F(∫ t

f dτ, t→ ω

)=

F(ω)jω

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

1

Page 2: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

• Harmonic content of nonlinear circuits driven by a phasor source. For instance if adiode switches on and off based on the sign of the voltage across it, what harmonicsare induced?

• Fourier spectrum of AM and FM modulated signals.

• Frequency response of a circuit. This has been done last time using Laplace trans-froms, 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 the-ory.

• Filter design. We want to isolate a desired part of the spectrum. How to do it infourier domain, and how to do it in time domain itself.

• System identification: An earthquake has occurred and you have the trace. Howto analyse it, knowing the type of trace an earthquake can give to identify whereand when it occurred and what type and how strong it was. This often uses fouriermethods as part of the process of system identification.

• Adaptive circuits: You want to listen to a radio station, but its transmission frequencyis not constant (or your receiver’s tuner is not stable). How to “lock” the receiver tothe transmitted signal? These are known as phase locked loops. Also analysed anddesigned using fourier methods, since what we are tracking is a frequency signal.

We will take some sample problems here to look at.

2 Digital Fourier TransformsNow, 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 FourierTransform is just the limit of the Fourier Series. So we can solve the Fourier Series problemthat is nearest to the exact problem and get an approximate solution. But even that is toocomplicated, since the general function has an infinite number of fourier coefficients. Sowe keep only a finite number of fourier coefficients to get to the computer approximationof the Fourier Transform.

Let us take an example:

f (t) =1

1+ t2 e−t2

The plot of the function looks as follows:

2

Page 3: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

−3 −2 −1 0 1 2 30.0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

t

f

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

−10 −8 −6 −4 −2 0 2 4 6 8 100.0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

t

g(t)

Then a Fourier series will do the job. We write

g(t) =∞

∑n=−∞

cne jnωt

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

g(t) =∞

∑n=0

an cos(nωt)

3

Page 4: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

We can compute the coefficients as

a0 =16

∫ 3

−3g(t)dt

an =13

∫ 3

−3g(t)cos(nωt)dt

Using the intg function that is built in, we can compute the integrals. The code is asfollows

[branch code0:4 〈* 4〉≡

global count;function y=g(t)

global count;count=count+1y=exp(-t.^2)./(1+t.^2);

endfunctionfunction y=gn(t)

global count;count=count+1;y=exp(-t.^2).*cos(n*omega*t)./(1+t.^2);

endfunctionomega=2*%pi/6;count=0;a=zeros(21,1);a(1)=intg(0,3,g)/3;for n=1:20a(n+1)=intg(0,3,gn)/1.5;

endbar(0:20,a);xtitle("Fourier coefficients","n","a(n)");mprintf("Number of times function called: %d\n",count);

4

Page 5: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

]The coefficients look as follows:

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

0.00

0.05

0.10

0.15

0.20

0.25

Fourier coefficients

n

a(n)

Now, while this works, we are actually doing numerical integration to compute eachcoefficient. That is quite expensive. If you run the above code, you find that the functionwas called 8883 times, or about 450 times per coefficient. In practical situations we oftentake fourier transforms of very large data sets (could be millions of elements). We need afaster 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 thatthe integral to compute the coefficients collapses to a sum. We first sample the functionf (t) at N times over a time period t0, t0 + ∆t, etc to get N samples f0, f1, . . . , fN−1. TheDFT is then defined by:

fn =N−1

∑k=0

Fke j2πkn/N

Fk =1N

N−1

∑n=0

fne− j2πkn/N

The coefficients Fn are closely related to the coefficients an in the exact Fourier series. Wecan go from Fn to a cosine series

fn = A0 +N/2−1

∑k=1

Ak cos(2πnk/N)

Comparing we can see that

2πknN≡ nωt

or,2π

N≡ ω∆t

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 coefficients, we would only evaluate the function 21times. Let us compute the coefficients using the DFT and compare them to the fourier

5

Page 6: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

series values. Note that we have to compute 41 coefficients, since each cosine is equivalentto 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 botht = 0 and t = 6, since they are the same point. So I get 42 points including the end pointsand 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);clfbar(1:21,[a real(A)]);legends(["exact";"DFT"],2:3);

6

Page 7: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

The result is plotted below

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

0.00

0.05

0.10

0.15

0.20

0.25

0.30

0.35

0.40

exactDFT

It is obvious that the coefficients 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 theintegral can be exactly calculated from those samples, will the coefficients agree. Whichmeans that the more samples we take, the more accurate will be the approximation. So letus 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);clfbar(1:21,[a real(AA)]);legends(["exact";"DFT-256"],2:3)

7

Page 8: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

Now we get the following:

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

0.00

0.05

0.10

0.15

0.20

0.25

0.30

0.35

0.40

exactDFT−256

The errors are as follows:

8 〈* 4〉+≡[(1:21)’ real(A)-a real(AA)-a]

8

Page 9: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

The answer that Scilab gives is shown below

1. - 4.781E-08 - 1.242E-092. - 0.0011045 2.485E-093. - 0.0026768 - 2.485E-094. - 0.0027972 2.485E-095. - 0.0019422 - 2.486E-096. - 0.0010921 2.487E-097. - 0.0005517 - 2.487E-098. - 0.0002619 2.488E-099. - 0.0001192 - 2.490E-0910. - 0.0000525 2.491E-0911. - 0.0000225 - 2.492E-0912. - 0.0000095 2.494E-0913. - 0.0000039 - 2.496E-0914. - 0.0000016 2.497E-0915. - 0.0000006 - 2.500E-0916. - 0.0000003 2.502E-0917. - 3.128E-08 - 2.504E-0918. - 0.0000001 2.507E-0919. 6.934E-08 - 2.509E-0920. - 9.917E-08 2.512E-0921. 0.0000001 - 2.515E-09

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

3 The Fast Fourier TransformWell that was good. We could compute a decently accurate Fourier series using a fewsamples. No need for integration. But to do a good job we needed a lot of samples, and thecomputation of the sum requires a large number (roughly N2) multiplications of complexnumbers. That is still very expensive. Instead of 8000 function evaluations we are nowdoing 64000 complex multiplications!

There is a better way, through an algorithm known as the “Fast Fourier Transform” orFFT. It computes exactly the same DFT but uses only about 3N log2 N computations. ForN = 256, this works out to be about 3000 complex multiplications. As N increases, thesavings become huge. As far as Scilab is concerned, the only changes required to use theFFT 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).

That’s it. Nothing else changes, but the speed up is huge, and the memory utilization isfar less. If you did the dft calculation with a few thousand samples, you would run out ofmemory. No such problems with fft.

4 Filtering and Data AnalysisSuppose we have data whose frequencies of interest lie in a range. We can extract that databy suitable filtering. For example, consider

f (t) = A(t)cos t +B(t)cos2t

9

Page 10: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

where A(t) and B(t) are signals which vary very slowly. Then, we can filter out the cos2tterm 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 frequen-cies you want to reject.

• Zero the entries of F(ω) for those frequencies you want to reject. This operation iscalled filtering, and in this case, we are low pass filtering since we are throwing awaythe high frequencies.

The main thing to keep in mind is that you must keep both the positive low frequen-cies and the negative low frequencies, i.e., keep terms corresponding to n = 0 to kand n = N− k to N−1 while throwing away the rest.

• Get back the filtered 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)(

1+ cos2t2

)• Again filter to reject the cos2t term.

• Inverse transform to get A(t) itself.

5 The Assignment1. 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 forthe error to become negligible? Do you understand this result?

2. Compute the sine fourier series and extract the corresponding terms from the DFTfor the function. How many terms were needed for the result to become exact?

f (t) = sin3 (t +1)

3. Solve the differential equation

d2ydt2 + y = cos(3t)

Note that this is not an initial value problem - we would use Laplace Transforms forthat.

You will have to Fourier transform the equation, solve it in frequency domain, andthen invert back to get the function in real time. To do that, use y=fft(Y,1).

Note: You have to figure out how to implement the derivitive in the DFT. Basicallya term like

e j2πkn/N ≡ e jnωt

and the derivitive will pull out jnω . Here your ω is unity, which gives you thefactor to use.

10

Page 11: Scilab Week 7 - Indian Institute of Technology Madrashsr/ec205/scilab7.pdf · Scilab Week 7 October 5, 2009 In this assignment, the focus will be on Fourier Transforms to solve various

Note: Another problem you will face is that of a zero in the denominator. Handle itby adding a tiny number (say 10−14) to eliminate the singularity.

Note: You will have to play around with the factors of N. fft often has the factor ofN 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 directdifferentiation. How does the error scale with N? Do you understand this?

4. Given a signalf (t) = J0(0.01t)cos t + cos2t

where J0(x) is the the Bessel function of zeroth order. You can compute it in Scilabby besselj(0,x). Create a Scilab function to generate the function f (t), Sampleit from t = 0 to t = 500 with 8 samples per 2π . Filter as explained above to extractthe coefficient of cos t. Plot the function extracted along with J0(0.01t) itself andcompare to see if the extraction worked. This is nothing but an example of AMmodulation.

There are many uses of the Fourier transform, and you should get very familiar with it.

11