0% found this document useful (0 votes)
26 views13 pages

Fourier Series Analysis with Python

The document discusses the analysis of Fourier series using Python, explaining how periodic functions can be represented as an infinite sum of sine and cosine functions. It outlines the Dirichlet conditions for Fourier series representation and provides examples of Fourier series analysis for square, sawtooth, and triangular wave functions using Python code. The document emphasizes the importance of Fourier series in harmonic analysis and its applications in representing system responses to periodic inputs.

Uploaded by

saudagarpre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views13 pages

Fourier Series Analysis with Python

The document discusses the analysis of Fourier series using Python, explaining how periodic functions can be represented as an infinite sum of sine and cosine functions. It outlines the Dirichlet conditions for Fourier series representation and provides examples of Fourier series analysis for square, sawtooth, and triangular wave functions using Python code. The document emphasizes the importance of Fourier series in harmonic analysis and its applications in representing system responses to periodic inputs.

Uploaded by

saudagarpre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Analysis of Fourier series using Python Code

Dr. Shyamal Bhar


Department of Physics
Vidyasagar College for Women
Kolkata – 700 006

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 xdx 
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
 

 built-in piecewise continuous functions such as square wave, sawtooth


wave and triangular wave

1. [Link] module

[Link] (x, duty=0.5)

Return a periodic square-wave waveform.

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. [Link] module

[Link](x, width=1)

Return a periodic sawtooth or triangle waveform.

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].

 Triangular wave from sawtooth signal

Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 4
3. [Link] () module

[Link](M, sym=True)

Return a triangular window.


w : ndarray

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

# Fourier series analysis for a sqaure wave function


# user defined function

import numpy as np
from [Link] import square
import [Link] as plt
from [Link] 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

# Generation of square wave

x=[Link](0,L,samples,endpoint=False)
y=square(2.0*[Link]*x*freq/L,duty=dutycycle)

# Calculation of Fourier coefficients


a0=2./L*simps(y,x)
an=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)
bn=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)

# sum of the series


s=a0/2.+sum([an(k)*[Link](2.*[Link]*k*x/L)+bn(k)*[Link](2.*[Link]*
k*x/L) for k in range(1,terms+1)])

# Plotting

[Link](x,s,label="Fourier series")
[Link](x,y,label="Original square wave")
[Link]("$x$")
[Link]("$y=f(x)$")
[Link](loc='best',prop={'size':10})
[Link]("Sqaure wave signal analysis by Fouries series")
[Link]("fs_square.png")
[Link]()

Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 6
Example-2 :

# Fourier series analysis for a sawtooth wave function

import numpy as np
from [Link] import square,sawtooth
import [Link] as plt
from [Link] import simps

L=1 # Periodicity of the periodic function f(x)


freq=2 # No of waves in time period L
width_range=1
samples=1000
terms=50

# Generation of Sawtooth function

x=[Link](0,L,samples,endpoint=False)
y=sawtooth(2.0*[Link]*x*freq/L,width=width_range)

# Calculation of Co-efficients
a0=2./L*simps(y,x)
an=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)
bn=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)

# Sum of the series


s=a0/2.+sum([an(k)*[Link](2.*[Link]*k*x/L)+bn(k)*[Link](2.*[Link]*
k*x/L) for k in range(1,terms+1)])

# Plotting

[Link](x,s,label="Fourier series")
[Link](x,y,label="Original sawtooth wave")
[Link]("$x$")
[Link]("$y=f(x)$")
[Link](loc='best',prop={'size':10})
[Link]("Sawtooth wave signal analysis by Fouries series")
[Link]("fs_sawtooth.png")
[Link]()

Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 7
Example -3:

# Fourier series analysis for a Triangular wave function

import numpy as np
from [Link] import square,sawtooth,triang
import [Link] as plt
from [Link] import simps

L=1 # Periodicity of the periodic function f(x)


samples=501
terms=50

# Generation of Triangular wave


x=[Link](0,L,samples,endpoint=False)
y=triang(samples)

# Fourier Coefficients
a0=2./L*simps(y,x)
an=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)
bn=lambda n:2.0/L*simps(y*[Link](2.*[Link]*n*x/L),x)

# Series sum
s=a0/2.+sum([an(k)*[Link](2.*[Link]*k*x/L)+bn(k)*[Link](2.*[Link]*
k*x/L) for k in range(1,terms+1)])

# Plotting
[Link](x,s,label="Fourier series")
[Link](x,y,label="Original Triangular wave")
Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 8
[Link]("$x$")
[Link]("$y=f(x)$")
[Link](loc='best',prop={'size':10})
[Link]("Triangular wave signal analysis by Fouries series")
[Link]("fs_triangular.png")
[Link]()

Example-4

# Fourier series analysis for a sawtooth wave function


# User defined function

import numpy as np
import [Link] as plt
from [Link] import simps

L=1.0 # half wavelength, Wavelength=2L


freq=2 # frequency
samples=1001
terms=300

# Defining sawtooth function


x=[Link](-L,L,samples,endpoint=False)
f=lambda x: (freq*x%(2*L)-L)/L

# Fouriers coefficients

a0=1./L*simps(f(x),x)
an=lambda n:1.0/L*simps(f(x)*[Link](1.*[Link]*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*[Link](1.*[Link]*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)*[Link](1.*[Link]*k*xp/L)+bn(k)*[Link](1.*[Link]
*k*xp/L) for k in range(1,terms+1)])

# Plotting
[Link](xp,s,label="Fourier series")
[Link](xp,f(xp),label="Original sawtooth wave")
[Link](loc='best',prop={'size':10})
[Link]("saw_ud.png")
[Link]()

Example-5:

# Fourier series analysis for a square wave function

# User defined function

import numpy as np
import [Link] as plt
from [Link] import simps

L=1.0 # half wavelength, Wavelength=2L


freq=2 # frequency
samples=1001
terms=300

Dr. Shyamal Bhar, Department of Physics, Vidyasagar College for Women, Kolkata – 700 006 10
# Generating Square wave
x=[Link](-L,L,samples,endpoint=False)
F=lambda x: [Link]([-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)*[Link](1.*[Link]*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*[Link](1.*[Link]*n*x/L),x)

# Series sum
xp=4*x
s=a0/2.+sum([an(k)*[Link](1.*[Link]*k*xp/L)+bn(k)*[Link](1.*[Link]
*k*xp/L) for k in range(1,terms+1)])

#Plotting
[Link](xp,s,label="Fourier series")
[Link](xp,f(xp),label="Original Square wave")
[Link](loc='best',prop={'size':10})
[Link]("square_ud.png")
[Link]()

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 [Link] as plt
from [Link] import simps

L=1.0 # half wavelength, Wavelength=2L


freq=2 # frequency
samples=1001
terms=300

# Generating wave
x=[Link](-L,L,samples,endpoint=False)
F=lambda x: [Link]([u**2 if -L<=u<0 else 1 if 0<u<0.5 else 0
for u in x])
#F=lambda x: abs([Link](2*[Link]*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)*[Link](1.*[Link]*n*x/L),x)
bn=lambda n:1.0/L*simps(f(x)*[Link](1.*[Link]*n*x/L),x)

# Series sum
xp=x
s=a0/2.+sum([an(k)*[Link](1.*[Link]*k*xp/L)+bn(k)*[Link](1.*[Link]
*k*xp/L) for k in range(1,terms+1)])

#Plotting
[Link](xp,s,label="Fourier series")
[Link](xp,f(xp),label="Original wave")
[Link](loc='best',prop={'size':10})
[Link]("arb_ud.png")
[Link]()

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

Common questions

Powered by AI

The use of sine and cosine functions in a Fourier series offers several advantages. They are easy to differentiate and integrate, which simplifies analysis. Each term in the series contains only a single frequency component, facilitating the characterization of frequency content in periodic signals. This orthogonality property of trigonometric functions helps in isolating individual harmonic components from a complex signal .

The Dirichlet conditions specify that a function can be expressed as a Fourier series if it meets the following criteria: it must be periodic, single-valued, and continuous, with a finite number of finite discontinuities; it must have only a finite number of maxima and minima within one period; and the integral over one period must converge. These conditions ensure that the function can be decomposed accurately into harmonics, which is essential for determining the frequency content of a periodic input and its response in systems .

The scipy.signal.sawtooth function effectively creates a triangular wave by modifying the width argument, allowing the waveform to rise and fall symmetrically. Fourier series are then used to decompose this waveform into trigonometric components. The series sum and Fourier coefficients, calculated via integration, reconstruct the triangular wave by adding harmonic terms of sine and cosine functions, representing its periodicity and shape accurately .

Harmonic analysis via Fourier series is crucial in physics and engineering for breaking down complex periodic signals into simpler components. This decomposition helps understand system responses to periodic inputs by examining frequency content. In engineering, it's essential for analyzing vibrations, waves, and signal processing. Harmonic analysis simplifies solving differential equations by applying specific frequency responses, enhancing the design and analysis of systems subjected to periodic forces .

Unlike Taylor series, which represent functions as power series and require them to be differentiable around a point, Fourier series express periodic functions as an infinite sum of sine and cosine functions. Fourier series can describe functions that are not continuous or differentiable, making them suitable for analyzing periodic signals with discontinuities. This allows Fourier series to decompose a complex periodic function into simpler harmonic components based on orthogonality properties .

Fourier coefficients, denoted as a0, an, and bn, determine the amplitude of the components in the Fourier series. They are calculated through integrals over a period of the function involving sine and cosine terms. The coefficient a0 is found by integrating the function over the period and dividing by the period length. Coefficients an and bn are obtained similarly by integrating the product of the function with cosine and sine functions, respectively, across the same interval .

The scipy.signal.square module creates a periodic square wave by setting the waveform value to +1 for the first half of its period and -1 for the second half, adjustable through the duty cycle. Fourier series analysis then decomposes this square wave into its harmonic components by calculating Fourier coefficients. The sum of these series reproduces the square wave by approximating it with sine and cosine waves of various frequencies .

The convergence of the integral over one period of the function is crucial for expressing it as a Fourier series because it ensures the Fourier coefficients, which are derived from these integrals, are finite and well-defined. This convergence is a precondition for the series to converge to the function itself or a good approximation thereof, enabling the accurate decomposition of the function into its respective harmonic components .

The orthogonality relationships of sine and cosine functions are pivotal in Fourier series as they allow the isolation of each harmonic component in the frequency domain. These relationships state that the integral of the product of sine and cosine functions over a period is zero, leading to decoupling in the linear algebraic sense. Thus, each term in the Fourier series independently contributes to the overall function approximation without interference, enabling precise harmonic analysis .

Python, with libraries like SciPy, can perform Fourier series analysis by computing the Fourier coefficients and summing the harmonic series to approximate functions like square and sawtooth waves. SciPy's signal module provides functions like scipy.signal.square and scipy.signal.sawtooth for generating these waveforms. Integration functions, such as simps, compute coefficients by integrating the product of the waveform and trigonometric terms. Python's plotting libraries visualize both original waves and their Fourier approximations .

You might also like