Fourier_series_python_code
Fourier_series_python_code
We know that there are many ways by which any complicated function may be expressed as
power series. This is not the only way in which a function may be expressed as a series but there
is a method of expressing a periodic function as an infinite sum of sine and cosine functions.
This representation is known as Fourier series. The computation and study of Fourier series is
known as harmonic analysis and is useful as a way to break up an arbitrary periodic function into
a set of simple harmonic terms that can be plugged in, solved individually, and then recombined
to obtain the solution to the original problem or an approximation to it to whatever accuracy is
desired. Unlike Taylor series, a Fourier series can describe functions that are not everywhere
continuous and/or differentiable. There are other advantages of using trigonometric terms. They
are easy to differentiate and integrate and each term contain only one characteristic frequency.
Analysis of Fourier series becomes important because this method is used to represent the
response of a system to a periodic input and the response depends on the frequency content of
the input.
Dirichlet Conditions: The conditions that a function f x may be expressed as Fourier series are
known as the Dirichlet conditions. The conditions are
i) The function must be periodic
ii) It must be single valued and continuous. There may a finite number of finite
discontinuities
iii) It must have only a finite number of maxima and minima within one period
iv) The integral over one period of f x must converge.
Fourier series makes of the orthogonality relationships of the sine and cosine functions. The
integral over one period of the product of any two terms have the following properties:
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 1
x0 L
2 nx 2 mx
sin cos dx 0 for all m and n
x0 L L
L for m n 0
x0 L L
2 nx 2 mx
cos cos dx for m n 0
x0 L L 2
0 for m n
L for m n 0
x0 L L
2 nx 2 mx
sin sin dx for m n 0
x0 L L 2
0 for m n
So the Fourier series of the function f x over the periodic interval 0, L is written as
a0 2 nx 2 nx
f x an cos bn sin
2 n 1 L L
where an and bn are constants called the Fourier coefficients and
L
2
a0
L f x dx
0
L
2 2 nx
an
L 0
f x cos dx
L
L
2 2 nx
bn f x sin dx
L0 L
The Fourier series of the function f x over the periodic interval L, L is written as
a0 nx nx
f x an cos bn sin
2 n 1 L L
where,
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 2
L
1
a0 f xdx
L L
L
1 nx
an f x cos dx
L L L
L
1 nx
bn f x sin dx
L L L
The Fourier series of the function f x over the periodic interval , is written as
a0
f x an cos nx bn sin nx
2 n 1
where,
1
a0 f x dx
1
an f x cos nx dx
1
bn f x sin nx dx
1. scipy.signal.square module
The square wave has a period 2*pi, has value +1 from 0 to 2*pi*duty and -1 from
2*pi*duty to 2*pi. duty must be in the interval [0,1].
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 3
2. scipy.signal.sawtooth module
scipy.signal.sawtooth(x, width=1)
The sawtooth waveform has a period 2*pi, rises from -1 to 1 on the interval 0 to
width*2*pi, then drops from 1 to -1 on the interval width*2*pi to 2*pi. width must be
in the interval [0, 1].
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 4
3. Scipy.signal.triang () module
scipy.signal.triang(M, sym=True)
The window, with the maximum value normalized to 1 (though the value 1 does not
appear if M is even and sym is True).
Example -1
import numpy as np
from scipy.signal import square
import matplotlib.pyplot as plt
from scipy.integrate import simps
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 5
L=4 # Periodicity of the periodic function f(x)
freq=4 # No of waves in time period L
dutycycle=0.5
samples=1000
terms=100
x=np.linspace(0,L,samples,endpoint=False)
y=square(2.0*np.pi*x*freq/L,duty=dutycycle)
# Plotting
plt.plot(x,s,label="Fourier series")
plt.plot(x,y,label="Original square wave")
plt.xlabel("$x$")
plt.ylabel("$y=f(x)$")
plt.legend(loc='best',prop={'size':10})
plt.title("Sqaure wave signal analysis by Fouries series")
plt.savefig("fs_square.png")
plt.show()
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 6
Example-2 :
import numpy as np
from scipy.signal import square,sawtooth
import matplotlib.pyplot as plt
from scipy.integrate import simps
x=np.linspace(0,L,samples,endpoint=False)
y=sawtooth(2.0*np.pi*x*freq/L,width=width_range)
# Calculation of Co-efficients
a0=2./L*simps(y,x)
an=lambda n:2.0/L*simps(y*np.cos(2.*np.pi*n*x/L),x)
bn=lambda n:2.0/L*simps(y*np.sin(2.*np.pi*n*x/L),x)
# Plotting
plt.plot(x,s,label="Fourier series")
plt.plot(x,y,label="Original sawtooth wave")
plt.xlabel("$x$")
plt.ylabel("$y=f(x)$")
plt.legend(loc='best',prop={'size':10})
plt.title("Sawtooth wave signal analysis by Fouries series")
plt.savefig("fs_sawtooth.png")
plt.show()
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 7
Example -3:
import numpy as np
from scipy.signal import square,sawtooth,triang
import matplotlib.pyplot as plt
from scipy.integrate import simps
# Fourier Coefficients
a0=2./L*simps(y,x)
an=lambda n:2.0/L*simps(y*np.cos(2.*np.pi*n*x/L),x)
bn=lambda n:2.0/L*simps(y*np.sin(2.*np.pi*n*x/L),x)
# Series sum
s=a0/2.+sum([an(k)*np.cos(2.*np.pi*k*x/L)+bn(k)*np.sin(2.*np.pi*
k*x/L) for k in range(1,terms+1)])
# Plotting
plt.plot(x,s,label="Fourier series")
plt.plot(x,y,label="Original Triangular wave")
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 8
plt.xlabel("$x$")
plt.ylabel("$y=f(x)$")
plt.legend(loc='best',prop={'size':10})
plt.title("Triangular wave signal analysis by Fouries series")
plt.savefig("fs_triangular.png")
plt.show()
Example-4
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import simps
# Fouriers coefficients
a0=1./L*simps(f(x),x)
an=lambda n:1.0/L*simps(f(x)*np.cos(1.*np.pi*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*np.sin(1.*np.pi*n*x/L),x)
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 9
# Series sum
xp=4*x
s=a0/2.+sum([an(k)*np.cos(1.*np.pi*k*xp/L)+bn(k)*np.sin(1.*np.pi
*k*xp/L) for k in range(1,terms+1)])
# Plotting
plt.plot(xp,s,label="Fourier series")
plt.plot(xp,f(xp),label="Original sawtooth wave")
plt.legend(loc='best',prop={'size':10})
plt.savefig("saw_ud.png")
plt.show()
Example-5:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import simps
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 10
# Generating Square wave
x=np.linspace(-L,L,samples,endpoint=False)
F=lambda x: np.array([-1 if -L<=u<0 else 1 for u in x])
f=lambda x: F(freq*x%(2*L)-L)
# Fourier Coefficients
a0=1./L*simps(f(x),x)
an=lambda n:1.0/L*simps(f(x)*np.cos(1.*np.pi*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*np.sin(1.*np.pi*n*x/L),x)
# Series sum
xp=4*x
s=a0/2.+sum([an(k)*np.cos(1.*np.pi*k*xp/L)+bn(k)*np.sin(1.*np.pi
*k*xp/L) for k in range(1,terms+1)])
#Plotting
plt.plot(xp,s,label="Fourier series")
plt.plot(xp,f(xp),label="Original Square wave")
plt.legend(loc='best',prop={'size':10})
plt.savefig("square_ud.png")
plt.show()
Example -6
# Fourier series analysis for a Arbitrary waves function
# User defined function
import numpy as np
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 11
import matplotlib.pyplot as plt
from scipy.integrate import simps
# Generating wave
x=np.linspace(-L,L,samples,endpoint=False)
F=lambda x: np.array([u**2 if -L<=u<0 else 1 if 0<u<0.5 else 0
for u in x])
#F=lambda x: abs(np.sin(2*np.pi*x))
f=lambda x: F(freq*x%(2*L)-L)
# Fourier Coefficients
a0=1./L*simps(f(x),x)
an=lambda n:1.0/L*simps(f(x)*np.cos(1.*np.pi*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*np.sin(1.*np.pi*n*x/L),x)
# Series sum
xp=x
s=a0/2.+sum([an(k)*np.cos(1.*np.pi*k*xp/L)+bn(k)*np.sin(1.*np.pi
*k*xp/L) for k in range(1,terms+1)])
#Plotting
plt.plot(xp,s,label="Fourier series")
plt.plot(xp,f(xp),label="Original wave")
plt.legend(loc='best',prop={'size':10})
plt.savefig("arb_ud.png")
plt.show()
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 12
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 13