This is a short introduction to the usage of the... f k Z

advertisement
This is a short introduction to the usage of the Fast Fourier Transform in MATLAB.
For a function f on [0, 2π] the number Fk with k an integer, given by the formula
Z 2π
1
Fk =
f (ω)e−ikω dω,
2π 0
(1)
is called the k th Fourier coefficient of f . One can show that under reasonable assumptions
on the function f the Fourier coefficients exist and that
f (ω) =
∞
X
Fk eikω .
(2)
k=−∞
The series in the right hand side is called the Fourier series of the function f .
We will describe how to compute these Fourier coefficients.
The MATLAB function ‘fft’ computes, using N values of the function f on the interval
[0, 2π) (where the values are computed on N equally spaced points), N Fourier coefficients
up to a factor 1/N . Conversely, the function ‘ifft’ computes from N Fourier coefficients
(correctly ordered) the values of the function in N equally spaced points in [0, 2π) up to a
factor N . We will explain how this works.
We approximate the integral in (1) with a sum. Divide the interval [0, 2π] in N equal
pieces and choose for ω the values 2πn
with n = 0, 1, . . . , N − 1. We replace
N
Z 2π
f (ω)e−ikω dω
0
in (1) by its approximation
N
−1
X
f ( 2πn
) exp(−ik 2πn
) 2π
N
N
N
n=0
and obtain
N −1
N −1
1 2π X 2π
1 X 2π
2π
F̂k =
f ( n) exp(−ik N n) =
f ( n) exp(−ik 2π
n).
N
2π N n=0 N
N n=0 N
(3)
Then F̂k is a good approximation for Fk if N is large enough.
Conversely, if the values of Fk are given and we want to approximate f in the points
2π
2, . . . , 2π
(N − 1), then we can use as an approximation of formula (2) (for N
0, N , 2π
N
N
even)
N
fˆ( 2π
n)
N
=
−1
2
X
n).
Fk exp(ik 2π
N
(4)
k=− N
2
From (3) and the periodicity of the exponential function (e2πin = 1 for integers n) it follows
that F̂N −k = F̂−k .
1
If N = 2p for an integer p then a very fast algorithm to compute (3) and (4) exists. For
other values of N a less fast, but still fast, algorithm exists. In MATLAB the function ’fft’
is used for (3) and ’ifft’ for (4). Next we discuss how these functions are to be used. Define
f (ω) = 1 + cos(ω). Then we have
f (ω) = 12 e−iω + 1 + 12 eiω .
Now choose N = 8. The Fourier coefficients F̂0 , F̂1 , F̂2 , F̂3 , F̂−4 , F̂−3 , F̂−2 , F̂−1 are given by:
(F̂0 F̂1 F̂2 F̂3 F̂−4 F̂−3 F̂−2 F̂−1 ) = (1
1
2
0 0 0 0 0 12 ).
Note the order of appearance of the Fourier coefficients here. This is precisely the way
’fft’ produces the Fourier coefficients and how ’ifft’ reads its input. We now compute the
Fourier coefficients of f with MATLAB. First determine the values of f at the eight points
ω = 2πn/8 for n = 0, . . . , 7 and put them in a row vector as follows:
¡
¢7
f = f (2πn/8) n=0 = (f (1) · · · f (8)).
With
F̂ = fft(f )
Matlab computes
F̂ (k + 1) =
N
−1
X
f (n + 1) exp(−ik 2πn
),
N
(5)
n=0
which is N (= 8) times the outcome of (3). So
F̂ = fft(f )/8
gives the Fourier coefficients of the function f . Check with MATLAB that this is really true
for this particular case (Note: in MATLAB components of vectors start their numbering
with 1, this explains the ’+1’ in (5)).
Conversely, if we have
F = (1 12 0 0 0 0 0 12 ),
(6)
then the ‘inverse fast Fourier transform’ fˆ = ifft(F ) gives
N −1
1 X
ˆ
f (n + 1) =
).
f (k + 1) exp(ik 2πn
N
N k=0
Thus we find the outcome of (4) divided by N (= 8). Hence from
fˆ = 8 ifft(F )
we obtain the values of the function f in the points ω = 2πn/8 for n = 0, . . . , 7. So
fˆ = (2 1 +
√
2
2
1 1−
√
2
2
2
0 1−
√
2
2
1 1+
√
2
).
2
Again, the number 8 stands for the length of the vector of intermediate points which has
the same length as the vector of Fourier coefficients.
Assignments
1. Now pick f (x) = sin(x) and g(x) = π + |π − x| on [0, 2π). Take N = 256 and
determine the coefficients of the approximating sequences
127
X
ikx
Fk e
k=−128
,
127
X
Gk eikx .
k=−128
Use the fft. Do the Fk correspond to what you know about the sine function? Do not
forget the factor 1/256.
2. With the Fourier sequence (G0 , . . . , G127 , G−128 , . . . , G−1 ) of the function g we can
construct an approximation of g with ifft:
ĝ = ifft(G) ∗ 256.
Define in MATLAB omega=[0:255]*pi/128 and apply ‘plot(omega,ghat)’. Do you
obtain the desired plot? Note: often MATLAB announces that some imaginary part
has been constructed. Check in this case whether the absolute value of the imaginary
part really is negligible with ‘max(abs(imag(ghat)))’. When the imaginary parts are
not negligible this is often a sign that there is made an error in the computations.
3. Try some other examples until you know exactly how ‘fft’ and ‘ifft’ work.
Something to remember: ‘fft’ gives the Fourier coefficients from the function values
and ‘ifft’ gives the function values from the (properly ordered) Fourier coefficients.
3
Download