Fourier Series Matlab
Fourier Series Matlab
We usually think about processes around us as functions of time. However, it is often useful
to think about them as functions of frequencies. We naturally do this without giving it a
second thought. For example, when we listen to someone’s speech, we distinguish one person
from another by the pitch, i.e. dominating frequencies, of the voice. Similarly, our eyes do a
similar time-to-frequency transformation, as we can distinguish different light colors, and the
colors themselves are directly connected to the frequency of light. When we tune a radio, we
select a particular subset of frequencies to listen in the time varying signal of electromagnetic
waves. Even when we talk about salary, it is often enough to know that it will come every
two weeks or a month, i.e. we are concerned with the period and, thus, frequency of the
wages.
The transformations from the time domain to the frequency domain and back are called the
forward1 Fourier and inverse Fourier transforms respectively. The transformation is general
and can broadly be applied to any variable, not just a time variable. For example, we can do
the transformation from spatial coordinates to the spatial coordinates’ frequencies, which is
the base of the JPEG image compression algorithm.
The Fourier transform provides the basis for many filtering and compression algorithms. It is
also an indispensable tool for analyzing noisy data. Many differential equations can be solved
much easier in the periodic oscillation basis. Additionally, the calculation of convolution
integrals of two functions is very fast and simple when it is done in the frequency domain.
It is natural to think that a periodic function, such as the example shown in fig. 1.1, can be
constructed as the sum of other periodic functions. In Fourier series, we do it in the basis of
1
The word ‘forward’ is often omitted.
1
sines and cosines, which are clearly periodic functions.
0.5
y(t)
-0.5
-1
-20 -10 0 10 20
t
Figure 1.1: Example of a periodic function with the period of 10.
A more mathematically solid definition of a function that can be transformed is: Rany periodic
T
single value function y(t) with a finite number of discontinuities and for which 0 |f (t)|dt is
finite can be presented as
Fourier series
∞
a0 X
y(t) = + (an cos(nω1 t) + bn sin(nω1 t)) (1.1)
2 n=1
where T is the period, i.e. y(t) = y(t + T ), and ω1 = 2π/T is the fundamental angular
frequency, constant coefficients an and bn can be calculated according to the following
formula
2 T cos(nω1 t)
Z
an
= y(t)dt (1.2)
bn T 0 sin(nω1 t)
At a discontinuity, the series approaches the midpoint, i.e.
y(t − δ) + y(t + δ)
y(t) = lim (1.3)
δ→0 2
2
Note that for any integer n, sin(n2π/T t) and cos(n2π/T t) have the period of T .
The calculation of an and bn according to eq. (1.2) is called forward Fourier transformation
and construction of the y(t) via series of eq. (1.1) is called inverse Fourier transform.
The validity of the transformation can be shown by the use of the following relationship
2 T
Z
sin(nw1 t) cos(mw1 t)dt = 0, for any integer n and m (1.4)
T 0
2 T
Z
sin(nw1 t) sin(mw1 t)dt = δnm , (1.5)
T 0
(
2 T for n = m = 0
Z
2,
cos(nw1 t) cos(mw1 t)dt = (1.6)
T 0 δnm , otherwise
12 T 1 T
Z Z
1
a0 = cos(0w1 t)y(t)dt = y(t)dt = y(t) (1.7)
2 2T 0 T 0
Thus, a0 /2 has a special meaning: it is the mean of the function over its period, i.e. the
base line, the DC offset, or the bias.
Also, an coefficients belong to cosines, thus they are responsible for the symmetrical part
of the function (after offset removal). Consequently, bn coefficients are in charge of the
asymmetrical behavior.
Since each an or bn coefficient corresponds to the oscillatory functions with the frequency
nω1 , the set of a and b coefficients is often called spectrum when it is shown as the dependence
on frequency.
Let’s find the Fourier series representation of the following periodic function
Since the function is symmetrical, we can be sure that all bn = 0. The an coefficients are
found by applying eq. (1.2):
a0 = π,
an = 0, for even n
an = − πn2 , for odd n
4
Their values are shown in fig. 1.2. As we can see a0 = π is twice the mean of the |t| on the
(−π, π) interval.
3
a n coefficients
4
-1
-2
0 5 10 15 20
n
Figure 1.2: The first 20 an coefficients of the |t| Fourier transform.
We can notice from fig. 1.3 that the an coefficients decrease very quickly as n grows, implying
that the contribution of the higher n terms vanishes very quickly. Therefore, we can get quite
a good approximation of |t| with a truncated Fourier series.
This observation provides a basis for information compression. It is enough to know only the
first 11 coefficients (by the way, half of them are zero) of the Fourier transform to reconstruct
our function with minimal deviations from its true values at all possible times. If we need
better precision, we can increase the number of the Fourier series coefficients.
Let’s find the Fourier series for the step function defined as
(
0, −π < x < 0,
1, 0 < x < π
4
Fourier series with 1 terms Fourier series with 3 terms Fourier series with 11 terms
3.5 3.5 3.5
y(t)=|t| y(t)=|t| y(t)=|t|
3 Fourier series 3 Fourier series 3 Fourier series
2 2 2
y
y
1.5 1.5 1.5
1 1 1
0 0 0
-4 -2 0 2 4 -4 -2 0 2 4 -4 -2 0 2 4
t t t
Figure 1.3: Approximation of the |t| function by truncating the Fourier series at maximum
n = 1 (left), n = 3 (middle), n = 11 (right).
This function is asymmetric (with respect to its mean value 1/2), thus all an = 0 except the
a0 = 1. The bn coefficients are
(
bn = 0, n is even
bn = πn , n is odd
2
Recall that
exp(iωt) = cos(ωt) + i sin(ωt)
It can be shown that we can rewrite eqs. (1.1) and (1.2) in the much more compact and
symmetric notation without the eye-catching 1/2 for the zero’s coefficient:
5
b n coefficients
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20
n
Figure 1.4: The first 20 bn coefficients of the step function transform.
∞
X
y(t) = cn exp(inω1 t) (1.8)
n=−∞
Z T
1
cn = y(t) exp(−iω1 nt)dt (1.9)
T 0
The c0 has the special meaning: the average of the function over the period or bias or
offset.
an = cn + c−n (1.10)
bn = i(cn − c−n ) (1.11)
You might ask yourself: what are those ‘negative’ frequencies components for which c−n is
6
Fourier series with 1 terms Fourier series with 10 terms Fourier series with 100 terms
1.2 1.2 1.2
y(t)=|t| y(t)=|t| y(t)=|t|
1 Fourier series 1 Fourier series 1 Fourier series
y
0.4 0.4 0.4
0 0 0
Figure 1.5: Approximation of the step function by truncating the Fourier series at maximum
n = 1 (left), n = 10 (middle), n = 100 (right).
in charge? This does not look physical. It should not worry us too much, since cos(−ωt) =
cos(ωt) and sin(−ωt) = − sin(ωt), i.e. it is just a flip of the sign of the corresponding sine
coefficient. We do this to make the forward and inverse transforms look alike.
Z ∞
1
y(t) = √ cω exp(iωt)dω (1.12)
2π −∞
Z ∞
1
cω = √ y(t) exp(−iωt)dt (1.13)
2π −∞
R∞
The above requires that −∞
y(t)dt exists, and it is finite.
√
Note that the choice of the normalization 1/ 2π coefficient is somewhat arbitrary. In some
literature, the forward transform has the overall coefficient of 1 and the inverse transform
has coefficient of 1/2π. The opposite is also used. Physicists like the symmetric form shown
above in eqs. (1.12) and (1.13).
2
Here, we do the opposite to the rectangle integration method discussed in section 9.2.
7
1.2 Discrete Fourier transform (DFT)
Our interest in the above material is somewhat academic only. In real life, we cannot compute
the infinite series, since it takes an infinite amount of time. Similarly, we cannot compute the
exact integrals required for the forward Fourier transform, as doing so requires knowledge of
the y(t) at every point of time, which necessitates that we need an infinite amount of data
to do the calculation. You might say that we have quite good methods to approximate the
value of integrals covered in chapter 9, but then we automatically limit ourself to a finite set
of points in time where we do the measurement of the y(t) function. In this case, we do not
need the infinite sum, it is enough to have only N coefficients of the Fourier transform to
reconstruct N points of y(t).
y(t) y5 y6
y4 yN-1
y0 yN y y2 y3
=
yN
1
yi yN+1
0
t
t1 t2 t3 t4 t5 t6 t ti t N-1 t N t N+1
t0
Assuming that y(t) has a period T , we took N equidistant points such that spacing between
them is ∆t = T /N (see fig. 1.6). The periodicity condition requires
We use the following notation tk = ∆t × k and yk = y(tk ) and define (see for example [1])
N −1
1 X 2π(k − 1)n
yk = cn exp(i ), where k = 1, 2, 3, · · · , N (1.15)
N n=0 N
N
X 2π(k − 1)n
cn = yk exp(−i ), where n = 0, 1, 2, · · · , N − 1 (1.16)
k=1
N
8
Notice that the above equations do not have time in them at all! Strictly speaking, the
DFT uniquely connects one periodic set of points with another; the rest is in the eye of
the beholder. The notion of the spacing is required when we need to decide what is the
corresponding frequency of the particular cn : f1 × n, where f1 = T /N is the spacing (∆f )
in the spectrum between two nearby c coefficients (see fig. 1.7). The other meaning of f1 is
the resolution bandwidth (RBW), i.e. by construction, we cannot resolve any two frequencies
with a spacing smaller than the RBW. The fs = 1/∆t is called the sampling frequency or
the acquisition frequency. The Nyquist frequency fN q = fs /2 = f1 N/2 has a very important
meaning which we will discuss later in section 16.1.
Y
Y(f) Y Y2 Y3 YN-2 N-1
1
YN-3
Y0
f
0 1
f f
f0 f1 f2 f3 f N-3 f N-2 f N-1
f Nq =(N/2)f
1
Figure 1.7: Sample spectrum: Fourier transform coefficient vs. frequency. Yk is the same as
ck
Note the canonical placement of the normalization coefficient 1/N in eq. (1.15) instead of
eq. (1.16). With this definition, c0 is not the average value of the function anymore; it is N
times larger3 . Unfortunately, pretty much every numerical library implements the DFT in
this particular way, and MATLAB is not an exception.
There are several properties of the cn coefficient; the proof of which is left as exercise for the
reader. The c coefficients are periodic
c−n = cN −n (1.17)
A careful reader would notice that cn and c−n coefficients are responsible for the same
absolute frequency f1 × n. Therefore, the spectrum is often plotted from -N/2 × f1 to
N/2 × f1 . It also has an additional benefit: if all yk have no imaginary part, then
9
i.e. they have the same absolute value |c−n | = |cn | = |cN −n |. This in turn means that for
such real y(t) the absolute values of the spectrum are symmetric either with respect to 0
or to the N/2 coefficient. Consequently, the highest frequency of the spectrum is the
Nyquist frequency (fN q ) and not the (N − 1) × f1 , which is ≈ fs for the large N .
10
1.5 DFT example
Let’s consider a very simple example which will help us to put together the above material.
We will sample and calculate the DFT for the following function
where D = −0.1 is the displacement, offset, or bias of the function with respect to zero;
Aone = 0.7 is the amplitude of the cosine with frequency fone = 10 Hz; and Atwo = 0.2 is the
amplitude of the π/4 shifted cosine with frequency ftwo = 30 Hz. For the reasons which we
explain later in the chapter 16, we chose the sampling frequency fs = 4ftwo . We run the
following code to prepare time samples yk for the time data set governed by eq. (1.21) and
to calculate the corresponding DFT components Yn =fft(y)
%% time parameters
t_start=0;
T = 5/f_one; % should be longer than the slowest component period
t_end = t_start + T;
% sampling frequency should be more than twice faster than the fastest
component
f s = f_two*4;
_
dt = 1/f_s; % spacing between sample points times
N=T/dt; % total number of sample points
Let’s first plot the time domain samples yk = y(tk ) and the underlying eq. (1.21)
11
1
y(t)
y(t k )
0.5
) k
y(t) and y(t
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5
Time, S
Figure 1.8: 60 time domain samples and the underlying eq. (1.21).
The result is shown in fig. 1.8. We can see 5 periods of the y(t), though the function no
longer resembles sine or cosine anymore due to combination of two cosines. Note that the
y(t) is shown as the guide to the eye only. The DFT algorithm has no access to it other than
the 60 sampled points.
Let’s now draw the |Yn | calculated by fft(y)
12
0.4 0.4
0.3 0.3
|Y(f n )| / N
|Y n | / N
0.2 0.2
0.1 0.1
0 0
0 20 40 60 -100 -50 0 50 100
Index n f n , Hz
Figure 1.9: The normalized DFT coefficients for the time samples shown in fig. 1.8. The left
panel depicts coefficient values versus their index (starting from 0). The right panel shows
the spectrum, i.e. coefficient values versus their corresponding frequency.
The result is shown in the left panel of fig. 1.9. Note that we normalized the result of the DFT
by number of points N = 60. This allows us to see the true nature of the Fourier transform
coefficients: recall (see section 1.2) that the Y0 coefficient normalized by N corresponds to
the mean or displacement of the function, which is −0.1 as we set it above for eq. (1.21). We
might wonder why there are 4 more non zero coefficients if we have only two cosines with
two distinct frequencies. This is due to reflection property of the DFT, i.e. Y−n and YN −n
corresponds to the same frequency. This is better shown if we plot the spectrum, i.e. Yn
versus the corresponding frequency. This is done with
13
plot( freq, abs(Y_normalized), 'bo' );
fontSize=FontSizeSet; set(gca,'FontSize', fontSize );
xlabel('f_n, Hz');
ylabel('|Y(f_n)| / N');
the Yn index transformation to the frequency is done with the helper function
function spectrum_freq=fourier_frequencies(SampleRate, N)
%% return column vector of positive and negative frequencies for DFT
% SampleRate − acquisition rate in Hz
% N − number of data points
% assignment of frequency,
% recall that spectrum_freq(1) is zero frequency, i.e. DC component
spectrum_freq=(((1:N)−1)*f1).'; % column vector
Now we have the canonical spectrum shown in the right panel of fig. 1.9. Notice that the
spectrum of the absolute values of Yn is fully symmetric (i.e. mirrored around f = 0, as
it predicted by eq. (1.18). Now, we see that the spectrum has only two strong frequency
components at 10 Hz and 30 Hz. This is in complete accordance with eq. (1.21) and the
values of fone = 10 Hz and ftwo = 30 Hz. Now, let’s examine the components’ values:
Y (10) Hz = 0.35, which is exactly half of the Aone = 0.70. A similar story is seen for the
other frequency component Y (30 Hz) = 0.1. This is due to eqs. (1.10) and (1.11) and the
fact that y(t) has no imaginary part. Note that Yn themselves can be complex even in this
case.
14
1.6 Self-Study
Problem 1: Have a look at the particular realization of the N point forward DFT with the
omitted normalization coefficient:
N
X
Cn = yk exp(−i2π(k − 1)n/N )
k=1
Analytically prove that the forward discrete Fourier transform is periodic, i.e. cn+N = cn .
Note: recall that exp(±i2π) = 1.
Does this also prove that c−n = cN −n ?
Problem 2: Use the proof for the previous problem’s relationships and show that the fol-
lowing relationship holds for any sample set which has only real values (i.e. no complex
part)
cn = c∗N −n
Where ∗
depicts the complex conjugation.
Problem 3: Load the data from the file 'data_for_dft.dat'4 provided at the book’s web
page. It contains a table with y vs t data points (the first column holds the time, the second
holds y). These data points are taken with the same sampling rate.
(a) What is the sampling rate?
(b) Calculate forward DFT of the data (use MATLAB built-ins) and find which 2 frequency
components of the spectrum (measured in Hz not rad−1 ) are the largest. Note: I refer
to the real frequency of the sin or cos component, i.e. only positive frequencies.
(c) What is the largest frequency (in Hz) in this data set which we can scientifically discuss?
(d) What is the lowest frequency (in Hz) in this data set which we can scientifically discuss?
4
The file is available at http://physics.wm.edu/programming_with_MATLAB_book/ch_dft/data/data_
for_dft.dat
15
Bibliography
16