Dr. Joshua Vaughan
joshua.vaughan@louisiana.edu
http://www.ucs.louisiana.edu/~jev9637/
This notebook will look at the Fourier Analysis of periodic functions. In this notebook, we'll use SymPy, "a Python library for symbolic mathematics."
We can write any periodic function as an infinite sum of sines and cosines:
$ \quad f(t) = \sum_{n=0}^{\infty}a_n\cos(n\omega_0t) + \sum_{n=1}^{\infty}b_n\sin(n\omega_0t) $
where
$ \quad a_n = \frac{\omega_0}{\pi}\int_0^{\frac{2\pi}{\omega_0}}f(t)\cos(n\omega_0t)dt $,
$ \quad b_n = \frac{\omega_0}{\pi}\int_0^{\frac{2\pi}{\omega_0}}f(t)\sin(n\omega_0t)dt $,
and
$ \quad a_0 = \frac{\omega_0}{2\pi}\int_0^{\frac{2\pi}{\omega_0}}f(t)dt $
For more information on this process, you can see the lectures at the class website.
By changing the number of terms we use to approximate the original function we can approach its shape, as shown in Figure 1.
Figure 1: Increasing the Number of Terms in the Approximation
The remainder of this notebook will focus on determining the $a_n$ and $b_n$ terms in the Fourier Expansion.
# import Sympy and start "pretty printing"
import sympy
sympy.init_printing()
# Define the sympy symbolic variables we'll need
t, w0, tau_0 = sympy.symbols(['t', 'omega_0', 'tau_0'], real=True, positive=True)
#--------- Input your function to examine here --------
# Use the sympy Piecewise function to define the square wave - This matches the one in the Figure 1 above.
y = 2 + sympy.Piecewise((1, t < sympy.pi/w0), (-1, t > sympy.pi/w0))
# Use the sympy Piecewise function to define the triangle wave
# First define F0
# F0 = sympy.symbols('F0')
# y = sympy.Piecewise((F0/2*t, t < sympy.pi/w0), (-(F0/2)*t + 2*F0, t >= sympy.pi/w0))
# Use the sympy Piecewise function to define a trapezoid function
# y = sympy.Piecewise((3*F0*w0/(2*sympy.pi)*t, t < (2*sympy.pi/(3*w0))), (F0, t < (4*sympy.pi/(3*w0))),
# (-3*F0*w0/(2*sympy.pi)*t + 3*F0, t > (4*sympy.pi/(3*w0))))
# define the number of terms to use in the approximation
num_terms = 7
# get the a0 term
a0 = w0 / (2*sympy.pi) * sympy.integrate(y, (t, 0, 2*sympy.pi/w0))
# Define matrices of 0s to fill the the an and bn terms
a = sympy.zeros(1, num_terms)
b = sympy.zeros(1, num_terms)
# cycle through the 1 to num_terms Fourier coefficients (a_n and b_n)
for n in range(num_terms):
integral_cos = y * sympy.cos((n+1)*w0*t) # define the integral "interior"
a[n] = w0 / sympy.pi * sympy.integrate(integral_cos, (t, 0, 2*sympy.pi/w0)) # solve for a_n
integral_sin = y * sympy.sin((n+1)*w0*t) # define the integral "interior"
b[n] = w0 / sympy.pi * sympy.integrate(integral_sin, (t, 0, 2*sympy.pi/w0)) # solve for b_n
# Simplify and display a0
sympy.simplify(a0)
# Simplify and display the an terms
sympy.simplify(a)
# Simplify and diplay the bn terms
sympy.simplify(b)
Code is licensed under a 3-clause BSD style license. See the licenses/LICENSE.md file.
Other content is provided under a Creative Commons Attribution-NonCommercial 4.0 International License, CC-BY-NC 4.0.
# This cell will just improve the styling of the notebook
from IPython.core.display import HTML
import urllib.request
response = urllib.request.urlopen("https://cl.ly/1B1y452Z1d35")
HTML(response.read().decode("utf-8"))