blu*_*xel 7 python signals numpy
我尝试根据以下公式实现傅立叶级数函数:

...哪里...

...和...


这是我解决问题的方法:
import numpy as np
import pylab as py
# Define "x" range.
x = np.linspace(0, 10, 1000)
# Define "T", i.e functions' period.
T = 2
L = T / 2
# "f(x)" function definition.
def f(x):
return np.sin(np.pi * 1000 * x)
# "a" coefficient calculation.
def a(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a) / accuracy
integration = 0
for i in np.linspace(a, b, accuracy):
x = a + i * dx
integration += f(x) * np.cos((n * np.pi * x) / L)
integration *= dx
return (1 / L) * integration
# "b" coefficient calculation.
def b(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a) / accuracy
integration = 0
for i in np.linspace(a, b, accuracy):
x = a + i * dx
integration += f(x) * np.sin((n * np.pi * x) / L)
integration *= dx
return (1 / L) * integration
# Fourier series.
def Sf(x, L, n = 10):
a0 = a(0, L)
sum = 0
for i in np.arange(1, n + 1):
sum += ((a(i, L) * np.cos(n * np.pi * x)) + (b(i, L) * np.sin(n * np.pi * x)))
return (a0 / 2) + sum
# x axis.
py.plot(x, np.zeros(np.size(x)), color = 'black')
# y axis.
py.plot(np.zeros(np.size(x)), x, color = 'black')
# Original signal.
py.plot(x, f(x), linewidth = 1.5, label = 'Signal')
# Approximation signal (Fourier series coefficients).
py.plot(x, Sf(x, L), color = 'red', linewidth = 1.5, label = 'Fourier series')
# Specify x and y axes limits.
py.xlim([0, 10])
py.ylim([-2, 2])
py.legend(loc = 'upper right', fontsize = '10')
py.show()
Run Code Online (Sandbox Code Playgroud)
......这是我在绘制结果后得到的结果:

我已经读过如何在Numpy中计算傅立叶级数?我已经实现了这种方法.它工作得很好,但是它使用了指数方法,我想要关注三角函数和矩形方法,以便计算a_{n}和b_{n}系数的积分.
先感谢您.
更新(已解决)
最后,这是一个代码的工作示例.但是,我会花更多的时间在上面,所以如果有什么可以改进的话,它就会完成.
from __future__ import division
import numpy as np
import pylab as py
# Define "x" range.
x = np.linspace(0, 10, 1000)
# Define "T", i.e functions' period.
T = 2
L = T / 2
# "f(x)" function definition.
def f(x):
return np.sin((np.pi) * x) + np.sin((2 * np.pi) * x) + np.sin((5 * np.pi) * x)
# "a" coefficient calculation.
def a(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a) / accuracy
integration = 0
for x in np.linspace(a, b, accuracy):
integration += f(x) * np.cos((n * np.pi * x) / L)
integration *= dx
return (1 / L) * integration
# "b" coefficient calculation.
def b(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a) / accuracy
integration = 0
for x in np.linspace(a, b, accuracy):
integration += f(x) * np.sin((n * np.pi * x) / L)
integration *= dx
return (1 / L) * integration
# Fourier series.
def Sf(x, L, n = 10):
a0 = a(0, L)
sum = np.zeros(np.size(x))
for i in np.arange(1, n + 1):
sum += ((a(i, L) * np.cos((i * np.pi * x) / L)) + (b(i, L) * np.sin((i * np.pi * x) / L)))
return (a0 / 2) + sum
# x axis.
py.plot(x, np.zeros(np.size(x)), color = 'black')
# y axis.
py.plot(np.zeros(np.size(x)), x, color = 'black')
# Original signal.
py.plot(x, f(x), linewidth = 1.5, label = 'Signal')
# Approximation signal (Fourier series coefficients).
py.plot(x, Sf(x, L), '.', color = 'red', linewidth = 1.5, label = 'Fourier series')
# Specify x and y axes limits.
py.xlim([0, 5])
py.ylim([-2.2, 2.2])
py.legend(loc = 'upper right', fontsize = '10')
py.show()
Run Code Online (Sandbox Code Playgroud)

考虑以不同的方式逐块开发代码。如果这样的代码在第一次尝试时就能工作,你应该感到惊讶。正如 @tom10 所说,调试是一种选择。另一种选择是在解释器中逐步快速构建代码原型,使用 ipython 效果更好。
上面,您期望它b_1000不为零,因为输入f(x)是一个带有 a 的正弦曲线1000。您还期望所有其他系数都为零,对吧?
那么你应该只关注功能b(n, L, accuracy = 1000)。仔细一看,有 3 处地方出了问题。这里有一些提示。
dx在循环内。确定吗?i应该是一个整数,对吗?它真的是一个整数吗?通过原型设计或调试你会发现这一点(1/L)每当你写或类似的表达时都要小心。如果您使用的是 python2.7,那么您可能做错了。如果没有,至少from __future__ import division在源代码顶部使用 a 。如果您不知道我在说什么,请阅读此PEP。如果你解决了这3点,b()就会起作用。a然后以类似的方式思考。