简体   繁体   English

使用Simpson规则在Python中绘制傅立叶级数系数

[英]Plotting Fourier Series coefficients in Python using Simpson's Rule

I want to 1. express Simpson's Rule as a general function for integration in python and 2. use it to compute and plot the Fourier Series coefficients of the function 我想1.将Simpson规则表达为在python中进行集成的通用函数,然后2.用它来计算和绘制函数的傅里叶级数系数 罪(T) .

I've stolen and adapted this code for Simpson's Rule, which seems to work fine for integrating simple functions such as 我已窃取并改编了此代码以用于Simpson规则,该规则对于集成诸如 Ë^ T , 罪(T) or 要么 聚

Given period 特定时期 期 , the Fourier Series coefficients are computed as: ,傅里叶级数系数​​计算如下:

a_k

b_k

where k = 1,2,3,... 其中k = 1,2,3,...

I am having difficulty figuring out how to express 我很难弄清楚如何表达 AK . I'm aware that 我知道 a_k = 0 since this function is odd, but I would like to be able to compute it in general for other functions. 由于此函数很奇怪,但是我希望能够对其他函数进行一般计算。

Here's my attempt so far: 到目前为止,这是我的尝试:

import matplotlib.pyplot as plt
from numpy import *

def f(t):
    k = 1
    for k in range(1,10000): #to give some representation of k's span
        k += 1
    return sin(t)*sin(k*t)

def trapezoid(f, a, b, n):
    h = float(b - a) / n
    s = 0.0
    s += f(a)/2.0
    for j in range(1, n):
        s += f(a + j*h)
    s += f(b)/2.0
    return s * h

print trapezoid(f, 0, 2*pi, 100)

This doesn't give the correct answer of 0 at all since it increases as k increases and I'm sure I'm approaching it with tunnel vision in terms of the for loop. 这根本不能给出正确的答案0,因为它会随着k的增加而增加,而且我确定我会以for循环的方式通过隧道视觉接近它。 My difficulty in particular is with stating the function so that k is read as k = 1,2,3,... 我的特别困难是陈述函数,以便将k读为k = 1,2,3,...。

The problem I've been given unfortunately doesn't specify what the coefficients are to be plotted against, but I am assuming it's meant to be against k. 不幸的是,我得到的问题没有指定要针对哪些系数进行绘制,但是我假设它是针对k的。

Here's one way to do it, if you want to run your own integration or fourier coefficient determination instead of using numpy or scipy 's built in methods: 如果您要运行自己的积分或傅立叶系数确定而不是使用numpyscipy的内置方法,则这是一种方法:

import numpy as np

def integrate(f, a, b, n):
    t = np.linspace(a, b, n)
    return (b - a) * np.sum(f(t)) / n

def a_k(f, k):
    def ker(t): return f(t) * np.cos(k * t)
    return integrate(ker, 0, 2*np.pi, 2**10+1) / np.pi

def b_k(f, k):
    def ker(t): return f(t) * np.sin(k * t)
    return integrate(ker, 0, 2*np.pi, 2**10+1) / np.pi

print(b_k(np.sin, 0))

This gives the result 这给出了结果

0.0 0.0


On a side note, trapezoid integration is not very useful for uniform time intervals . 另外,梯形积分对于统一的时间间隔不是很有用。 But if you desire: 但是,如果您希望:

def trap_integrate(f, a, b, n):
    t = np.linspace(a, b, n)
    f_t = f(t)
    dt = t[1:] - t[:-1]
    f_ab = f_t[:-1] + f_t[1:]
    return 0.5 * np.sum(dt * f_ab)

There's also np.trapz if you want to use pre-builtin functionality. 如果要使用内置功能,还可以使用np.trapz Similarly, there's also scipy.integrate.trapz 同样,也有scipy.integrate.trapz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM