This page was generated from notebooks/L6/3_fourier_analysis.ipynb.
You can directly download the pdf-version of this page using the link below.
download
Fourier Analysis#
Fourier analysis, or the approximation of functions as series of sine and cosine functions, can be a very useful tool in the numerical analysis of data as well as in the numerical solution of differential equations. When analyzing experiments, Fourier transforms are frequently used.
Optical tweezers are often characterised by the frequency spectrum of position fluctuations.
Lock-in detection works by Fourier analysis for a very specific frequency of signals.
Optics can be understood with the help of Fourier transforms.
There are a number of other areas where Fourier transforms and analysis are important. We will take a brief look at Fourier series and Fourier transforms from a mathematical point of view. We will apply them to analyse the frequency spectrum of the oscillations of our coupled pendulum. We will come back to this later when we simulate the motion of a Gaussian wave packet for quantum mechanics.
[1]:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib import animation
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
# default values for plotting
plt.rcParams.update({'font.size': 12,
'axes.titlesize': 18,
'axes.labelsize': 16,
'axes.labelpad': 14,
'lines.linewidth': 1,
'lines.markersize': 10,
'xtick.labelsize' : 16,
'ytick.labelsize' : 16,
'xtick.top' : True,
'xtick.direction' : 'in',
'ytick.right' : True,
'ytick.direction' : 'in',})
# center the plots
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")
[1]:
Fourier series#
A Fourier series is a representation of periodic function
where
and varying amplitudes. The cosine and sine functions in the sum (Eq.
Therefore the integral
can be split into two integrals over a cosine function with the sum
Equivalently, the same can be carried out for the cosine functions, which yields
As mentioned above the coefficients
and for
In this case
As a result of this, we obtain
Fourier transform#
The Fourier transform is a generalisation of the complex Fourier series to the representation of arbitrary non-periodic functions
The Fourier transform of the function
with
Note that the Fourier transform
and the amplitude at a frequency
There are a number of efficient numerical algorithms available, which simplify the Fourier transformation. These are called Fast Fourier Transforms and implemented in numpy for example. We will use these algorithms to calculate the numerical Fourier transforms of our signals to identify the different oscillations in our signal. Below is an example how the numpy function cab be used and how to obtain the proper frequency axis.
f=np.fft.fft(alpha)
freq = np.fft.fftfreq(t.shape[-1],time/t.shape[-1])
Frequency analysis of our coupled pendula#
To use the Fourier analysis we load the data of our previous simulation with the normal modes and the beat mode of the harmonic oscillator.
[2]:
nm1=np.loadtxt('nm1.txt',delimiter=',')
nm2=np.loadtxt('nm2.txt',delimiter=',')
beats=np.loadtxt('beats.txt',delimiter=',')
The follwing code extracts the data and sorts them into individual arrays for eaysier plotting.
[3]:
t=nm1[:,0]
theta1_nm1=nm1[:,1]
theta1_nm2=nm2[:,1]
theta1_beats=beats[:,1]
The next few lines are mainly for plotting the data but contain also the Fourier transform of the signal.
[4]:
# calculate the frequency spectrum of the oscillations for different initial conditions
plt.figure(1,figsize=(6,5))
plt.xlabel('frequency [Hz]', fontsize=16)
plt.ylabel('Amplitude',fontsize=16)
plt.tick_params(labelsize=14)
ft1=np.fft.fft(theta1_nm1)
freq = np.fft.fftfreq(t.shape[-1],t[-1]/t.shape[-1])
plt.plot(freq[:1000],np.abs(ft1)[:1000],label='normal mode 1')
ft1=np.fft.fft(theta1_nm2)
freq = np.fft.fftfreq(t.shape[-1],t[-1]/t.shape[-1])
plt.plot(freq[:1000],np.abs(ft1)[:1000],label='normal mode 2')
ft1=np.fft.fft(theta1_beats)
freq = np.fft.fftfreq(t.shape[-1],t[-1]/t.shape[-1])
plt.plot(freq[:1000],np.abs(ft1)[:1000],'k--',lw=1,label='beat mode')
plt.legend()
plt.xlim(0.2,0.4)
plt.show()

The result of our calculation is now, that the beat mode is actually a superposition of the two normal modes of the system. In fact, it turns out, that all of the possible states of a coupled oscillator system can be constructed from a superposition of its normal modes with specific amplitudes.