OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Calculating Fourier transform, wrong graph outputed

  • Thread starter Thread starter Tomas Rivera
  • Start date Start date
T

Tomas Rivera

Guest
I started learning Python and I try to calculate some fun math stuff. So today I tried calculating the Fourier Transform of the function f(x) = 1 if 0 < x < pi or 2 if pi < x < 2pi. The code outputs a graph but it is not the correct one.

enter image description here

Here is my entire code:

Code:
import math
import matplotlib.pyplot as plt
import numpy as np

#parameters
integralresolution = 20000
graphresolution = 1000
minshowterm = 1
fourierterms = 201
xmin = 0
xmax = 2*math.pi

def function(x):
    if 0 <= x < math.pi:
        return 1
    elif math.pi <= x <= 2*math.pi:
        return 0
# b < a
def Integral(a,b,function):
    delta = abs(a-b)/integralresolution
    integral = 0
    c = b
    for i in range(integralresolution):
        integral = integral + delta*function(c)
        c = c + delta
    return integral
#calculate bm:
bnlist = []
for n in range(1,fourierterms+1):
    def fsin(x):
        return math.sin(n*x)*function(x)
    bn = Integral(2*math.pi,0,fsin)/math.pi
    bnlist.append(bn)
#calculate an:
anlist = []
for n in range(fourierterms+1):
    def fcos(x):
        return math.cos(n*x)*function(x)
    an = Integral(2*math.pi,0,fcos)/math.pi
    anlist.append(an)
#prepare the graph
fig, ax = plt.subplots()
delta = abs(xmin-xmax)/graphresolution
xlist = []
x = xmin
for i in range(graphresolution):
    xlist.append(x)
    x = x + delta
ylist = []
for x in xlist:
    ylist.append(function(x))
ax.set(xlim=(xmin,xmax),ylim=(min(ylist) - 1, max(ylist) + 1))
#print the initial function
ax.plot(xlist,ylist,color='0')
#print the Fourier transform
def PrintFourier(n):
    def Fourier(x):
        result = anlist[0]/2
        for i in range(1,n+1):
            result = result + anlist[i]*math.cos(i*x) + bnlist[i]*math.sin(i*x)
        return result
    listofx = []
    x = xmin
    for i in range(graphresolution):
        listofx.append(x)
        x = x + delta
    listofy = []
    for x in listofx:
        listofy.append(Fourier(x))
    ax.plot(listofx,listofy,color="r")
PrintFourier(200)
plt.show()

Thanks for your time and help.

I tried reading the entire code and changing everything that might cause problem. I tried to rewrite entierly the code and still the same probleme. I think the problem might by with math and not with my code.
<p>I started learning Python and I try to calculate some fun math stuff. So today I tried calculating the Fourier Transform of the function f(x) = 1 if 0 < x < pi or 2 if pi < x < 2pi. The code outputs a graph but it is not the correct one.</p>
<p><a href="https://i.sstatic.net/KkTHrHGy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KkTHrHGy.png" alt="enter image description here" /></a></p>
<p>Here is my entire code:</p>
<pre><code>import math
import matplotlib.pyplot as plt
import numpy as np

#parameters
integralresolution = 20000
graphresolution = 1000
minshowterm = 1
fourierterms = 201
xmin = 0
xmax = 2*math.pi

def function(x):
if 0 <= x < math.pi:
return 1
elif math.pi <= x <= 2*math.pi:
return 0
# b < a
def Integral(a,b,function):
delta = abs(a-b)/integralresolution
integral = 0
c = b
for i in range(integralresolution):
integral = integral + delta*function(c)
c = c + delta
return integral
#calculate bm:
bnlist = []
for n in range(1,fourierterms+1):
def fsin(x):
return math.sin(n*x)*function(x)
bn = Integral(2*math.pi,0,fsin)/math.pi
bnlist.append(bn)
#calculate an:
anlist = []
for n in range(fourierterms+1):
def fcos(x):
return math.cos(n*x)*function(x)
an = Integral(2*math.pi,0,fcos)/math.pi
anlist.append(an)
#prepare the graph
fig, ax = plt.subplots()
delta = abs(xmin-xmax)/graphresolution
xlist = []
x = xmin
for i in range(graphresolution):
xlist.append(x)
x = x + delta
ylist = []
for x in xlist:
ylist.append(function(x))
ax.set(xlim=(xmin,xmax),ylim=(min(ylist) - 1, max(ylist) + 1))
#print the initial function
ax.plot(xlist,ylist,color='0')
#print the Fourier transform
def PrintFourier(n):
def Fourier(x):
result = anlist[0]/2
for i in range(1,n+1):
result = result + anlist*math.cos(i*x) + bnlist*math.sin(i*x)
return result
listofx = []
x = xmin
for i in range(graphresolution):
listofx.append(x)
x = x + delta
listofy = []
for x in listofx:
listofy.append(Fourier(x))
ax.plot(listofx,listofy,color="r")
PrintFourier(200)
plt.show()
</code></pre>
<p>Thanks for your time and help.</p>
<p>I tried reading the entire code and changing everything that might cause problem. I tried to rewrite entierly the code and still the same probleme. I think the problem might by with math and not with my code.</p>
 

Latest posts

Top