Python 用三角法计算傅里叶级数

Python 用三角法计算傅里叶级数,python,numpy,signals,Python,Numpy,Signals,我尝试根据以下公式实现傅里叶级数函数: …在哪里 ……还有 以下是我解决问题的方法: 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(n

我尝试根据以下公式实现傅里叶级数函数:

…在哪里

……还有

以下是我解决问题的方法:

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()
…以下是绘制结果后得到的结果:

我已经阅读了,并且已经实现了这种方法。它工作得很好,但它使用了指数法,我想重点讨论三角函数和矩形法,以计算
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()

考虑以不同的方式逐块开发代码。如果这样的代码在第一次尝试时就可以工作,您应该感到惊讶。正如@tom10所说,调试是一种选择。另一个选择是在解释器中一步一步地快速原型化代码,使用ipython会更好

上面,您希望
b_1000
是非零的,因为输入
f(x)
是一个含有
1000
的正弦波。你也希望所有其他系数都是零,对吗

然后您应该只关注函数
b(n,L,精度=1000)
。看看它,有三件事出了问题。这里有一些提示

  • dx
    的乘法在循环中。确定吗
  • 在循环中,
    i
    应该是一个整数,对吗?它真的是一个整数吗?通过原型设计或调试,您会发现这一点
  • 无论何时编写
    (1/L)
    或类似表达式,都要小心。如果您使用的是python2.7,那么您可能做错了。如果没有,至少在源代码顶部使用来自_future _导入部门的
    。如果你不知道我在说什么,请阅读《政治公众人物》

如果解决这三点,
b()
将起作用。然后以类似的方式思考
a

如果您调试自己的代码,您将学到更多。(看起来你把这些东西当成了学习练习,但就在你学到最多的时候,你实际上不得不思考,你在上面贴了一个问题,这违背了你的目的。)我是一个自命不凡的人,只有当我与自己展开斗争时,我才会贴出一个问题。感谢您提供有关调试的提示。到目前为止,我还没有在Python中使用它。感谢您提供了非常有价值的提示。最后,我发现我在
Sf(x,L,n)
函数中也有一个错误:)现在一切都很好。很高兴它成功了。考虑接受答案,然后编辑问题,在当前文本之后添加最后的工作代码(保持完整的问题!)