scilab7

Upload: dionisio-de-zolt

Post on 08-Aug-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 scilab7

    1/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

    1 Fourier Analysis

    The Fourier Transform is defined by

    F() =1

    2

    f(t)ejtdt ,

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

    f(t) =

    F()ejtd .

    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 f

    dt,t

    = jF()

    F

    tf 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

  • 8/22/2019 scilab7

    2/11

    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 harmonics

    are 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 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 identification.

    Adaptive circuits: You want to listen to a radio station, but its transmission frequencyis 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.

    2 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 infinite number of fourier coefficients. So

    we keep only a finite number of fourier coefficients to get to the computer approximation

    of the Fourier Transform.

    Let us take an example:

    f(t) =1

    1 + t2et

    2

    The plot of the function looks as follows:

    2

  • 8/22/2019 scilab7

    3/11

    3 2 1 0 1 2 3

    0.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. Even

    better, we can make copies of f(t) every 6 seconds to make it a periodic function, g(t). Theplot ofg(t) looks like this

    10 8 6 4 2 0 2 4 6 8 10

    0.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=

    cnejnt

    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 (nt)

    3

  • 8/22/2019 scilab7

    4/11

    We can compute the coefficients as

    a0 =1

    6

    3

    3

    g(t)dt

    an =1

    3

    33

    g(t) cos (nt) dt

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

    follows

    [branch code0:

    4 * 4global count;

    function y=g(t)

    global count;

    count=count+1

    y=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);

    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);

    4

  • 8/22/2019 scilab7

    5/11

    ]

    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 each

    coefficient. That is quite expensive. If you run the above code, you find that the function

    was called 8883 times, or about 450 times per coefficient. 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 coefficients collapses to a sum. We first sample the function

    f(t) at N times over a time period t0, t0 +t, etc to get N samples f0, f1, . . . , fN1. TheDFT is then defined by:

    fn =N1

    k=0

    Fkej2kn/N

    Fk =1

    N

    N1

    n=0

    fnej2kn/N

    The coefficients Fn are closely related to the coefficients an in the exact Fourier series. We

    can go from Fn to a cosine series

    fn = A0 +

    N/21

    k=1Akcos (2nk/N)

    Comparing we can see that

    2kn

    N nt

    or,2

    N t

    which is reasonable, since Nt 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 21

    times. Let us compute the coefficients using the DFT and compare them to the fourier

    5

  • 8/22/2019 scilab7

    6/11

    series values. Note that we have to compute 41 coefficients, 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);

    6

  • 8/22/2019 scilab7

    7/11

    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

    exact

    DFT

    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. 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)

    7

  • 8/22/2019 scilab7

    8/11

    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

    exact

    DFT256

    The errors are as follows:

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

    8

  • 8/22/2019 scilab7

    9/11

    The answer that Scilab gives is shown below

    1. - 4.781E-08 - 1.242E-09

    2. - 0.0011045 2.485E-09

    3. - 0.0026768 - 2.485E-09

    4. - 0.0027972 2.485E-09

    5. - 0.0019422 - 2.486E-09

    6. - 0.0010921 2.487E-09

    7. - 0.0005517 - 2.487E-09

    8. - 0.0002619 2.488E-09

    9. - 0.0001192 - 2.490E-09

    10. - 0.0000525 2.491E-09

    11. - 0.0000225 - 2.492E-09

    12. - 0.0000095 2.494E-09

    13. - 0.0000039 - 2.496E-09

    14. - 0.0000016 2.497E-09

    15. - 0.0000006 - 2.500E-0916. - 0.0000003 2.502E-09

    17. - 3.128E-08 - 2.504E-09

    18. - 0.0000001 2.507E-09

    19. 6.934E-08 - 2.509E-09

    20. - 9.917E-08 2.512E-09

    21. 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, something

    about which you will learn a lot in your ADSP course.

    3 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 N2) 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 3Nlog2N computations. For

    N = 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 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.

    4 Filtering and Data Analysis

    Suppose we have data whose frequencies of interest lie in a range. We can extract that data

    by suitable filtering. For example, consider

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

    9

  • 8/22/2019 scilab7

    10/11

    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 away

    the 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 = Nkto N1 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 + cos2t

    2

    Again filter to reject the cos2t term.

    Inverse transform to get A(t) itself.

    5 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

    d

    2

    ydt2

    +y = cos(3t)

    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 figure out how to implement the derivitive in the DFT. Basically

    a term like

    ej2kn/N ejnt

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

    factor to use.

    10

  • 8/22/2019 scilab7

    11/11

    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 ofN. 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) cost+ 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 AM

    modulation.

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