You are on page 1of 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 dened by
F() =
1
2

f (t)e
jt
dt ,
work in frequency domain, solve our problem there, and invert to get the solution
f (t) =

F()e
jt
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 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
then
H() = F()G()
The transforms of a derivitive and indenite integral are simpler in the Transform
domain:
F

d f
dt
, t

= jF()
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
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 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 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.
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 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:
f (t) =
1
1+t
2
e
t
2
The plot of the function looks as follows:
2
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). The
plot of g(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=
c
n
e
jnt
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
a
n
cos(nt)
3
We can compute the coefcients as
a
0
=
1
6

3
3
g(t)dt
a
n
=
1
3

3
3
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 * 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);
4
]
The coefcients 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
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 t
0
, t
0
+t, etc to get N samples f
0
, f
1
, . . . , f
N1
. The
DFT is then dened by:
f
n
=
N1

k=0
F
k
e
j2kn/N
F
k
=
1
N
N1

n=0
f
n
e
j2kn/N
The coefcients F
n
are closely related to the coefcients a
n
in the exact Fourier series. We
can go from F
n
to a cosine series
f
n
= A
0
+
N/21

k=1
A
k
cos(2nk/N)
Comparing we can see that
2
kn
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 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 42
nd
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
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 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)
7
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
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-09
16. - 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 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 3Nlog
2
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.
4 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)cost +B(t)cos2t
9
where A(t) and B(t) are signals which vary very slowly. Then, we can lter out the cos2t
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 frequen-
cies 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 frequen-
cies and the negative low frequencies, i.e., keep terms corresponding to n = 0 to k
and n = Nk to N1 while throwing away the rest.
Get back the ltered f (t) by inverse fourier transforming.
Multiply all the terms of the signal with cost.
Fourier transform the signal again. Now we are taking the fourier transform of
A(t)cos
2
t = A(t)

1+cos2t
2

Again lter 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+cos
2
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) = sin
3
(t +1)
3. Solve the differential equation
d
2
y
dt
2
+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 gure out how to implement the derivitive in the DFT. Basically
a term like
e
j2kn/N
e
jnt
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 10
14
) 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) = J
0
(0.01t)cost +cos2t
where J
0
(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 cost. Plot the function extracted along with J
0
(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

You might also like