这是一个老问题,但是由于我必须编写代码,所以我在这里发布了使用numpy.fft模块的解决方案,这可能比其他手工编制的解决方案更快。

DFT是计算函数Fourier级数系数(定义为参数的解析表达式或某些离散点上的数值插值函数)达到数值精度的正确工具。

这是一个实现,它允许通过传递适当的return_complex来计算Fourier级数的实值系数或复数系数:def fourier_series_coeff_numpy(f, T, N, return_complex=False):

"""Calculates the first 2*N+1 Fourier series coeff. of a periodic function.

Given a periodic, function f(t) with period T, this function returns the

coefficients a0, {a1,a2,...},{b1,b2,...} such that:

f(t) ~= a0/2+ sum_{k=1}^{N} ( a_k*cos(2*pi*k*t/T) + b_k*sin(2*pi*k*t/T) )

If return_complex is set to True, it returns instead the coefficients

{c0,c1,c2,...}

such that:

f(t) ~= sum_{k=-N}^{N} c_k * exp(i*2*pi*k*t/T)

where we define c_{-n} = complex_conjugate(c_{n})

Refer to wikipedia for the relation between the real-valued and complex

valued coeffs at http://en.wikipedia.org/wiki/Fourier_series.

Parameters

----------

f : the periodic function, a callable like f(t)

T : the period of the function f, so that f(0)==f(T)

N_max : the function will return the first N_max + 1 Fourier coeff.

Returns

-------

if return_complex == False, the function returns:

a0 : float

a,b : numpy float arrays describing respectively the cosine and sine coeff.

if return_complex == True, the function returns:

c : numpy 1-dimensional complex-valued array of size N+1

"""

# From Shanon theoreom we must use a sampling freq. larger than the maximum

# frequency you want to catch in the signal.

f_sample = 2 * N

# we also need to use an integer sampling frequency, or the

# points will not be equispaced between 0 and 1. We then add +2 to f_sample

t, dt = np.linspace(0, T, f_sample + 2, endpoint=False, retstep=True)

y = np.fft.rfft(f(t)) / t.size

if return_complex:

return y

else:

y *= 2

return y[0].real, y[1:-1].real, -y[1:-1].imag

这是一个用法示例:from numpy import ones_like, cos, pi, sin, allclose

T = 1.5 # any real number

def f(t):

"""example of periodic function in [0,T]"""

n1, n2, n3 = 1., 4., 7. # in Hz, or nondimensional for the matter.

a0, a1, b4, a7 = 4., 2., -1., -3

return a0 / 2 * ones_like(t) + a1 * cos(2 * pi * n1 * t / T) + b4 * sin(

2 * pi * n2 * t / T) + a7 * cos(2 * pi * n3 * t / T)

N_chosen = 10

a0, a, b = fourier_series_coeff_numpy(f, T, N_chosen)

# we have as expected that

assert allclose(a0, 4)

assert allclose(a, [2, 0, 0, 0, 0, 0, -3, 0, 0, 0])

assert allclose(b, [0, 0, 0, -1, 0, 0, 0, 0, 0, 0])

以及由此产生的a0,a1,...,a10,b1,b2,...,b10系数的图:

对于两种操作模式,这是功能的可选测试。您应该在示例之后运行此函数,或者在运行代码之前定义周期函数f和周期T。# #### test that it works with real coefficients:

from numpy import linspace, allclose, cos, sin, ones_like, exp, pi, \

complex64, zeros

def series_real_coeff(a0, a, b, t, T):

"""calculates the Fourier series with period T at times t,

from the real coeff. a0,a,b"""

tmp = ones_like(t) * a0 / 2.

for k, (ak, bk) in enumerate(zip(a, b)):

tmp += ak * cos(2 * pi * (k + 1) * t / T) + bk * sin(

2 * pi * (k + 1) * t / T)

return tmp

t = linspace(0, T, 100)

f_values = f(t)

a0, a, b = fourier_series_coeff_numpy(f, T, 52)

# construct the series:

f_series_values = series_real_coeff(a0, a, b, t, T)

# check that the series and the original function match to numerical precision:

assert allclose(f_series_values, f_values, atol=1e-6)

# #### test similarly that it works with complex coefficients:

def series_complex_coeff(c, t, T):

"""calculates the Fourier series with period T at times t,

from the complex coeff. c"""

tmp = zeros((t.size), dtype=complex64)

for k, ck in enumerate(c):

# sum from 0 to +N

tmp += ck * exp(2j * pi * k * t / T)

# sum from -N to -1

if k != 0:

tmp += ck.conjugate() * exp(-2j * pi * k * t / T)

return tmp.real

f_values = f(t)

c = fourier_series_coeff_numpy(f, T, 7, return_complex=True)

f_series_values = series_complex_coeff(c, t, T)

assert allclose(f_series_values, f_values, atol=1e-6)

python求级数的值_如何在Numpy中计算Fourier级数?相关推荐

  1. python画图修改背景颜色_如何在 Matplotlib 中更改绘图背景的实现

    介绍 Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库. 在本教程中,我们将研究如何在Matplotlib中更改绘图的背景. 导 ...

  2. 用python画奔驰的标志_如何在CATIA中快速画一个奔驰车标

    原标题:如何在CATIA中快速画一个奔驰车标 咱们这个公众号呀,总是发一些二次开发啊,代码啊什么的,这观众看的啊,是云里雾里的!哎,内位说了:您能不能讲点儿我们听的懂的内容啊?那好,今儿咱们就来说说, ...

  3. python怎么字体加阴影_如何在pythonptx中给文本添加阴影?

    我正在做一个项目,我必须用pythonptx创建一个PowerPoint.我需要添加有阴影的文本,使其显示如下: 如何在pythonptx中使用阴影格式化文本?在 下面是我使用的代码:from ppt ...

  4. python异步加载图片_如何在PyQt5中正确异步加载图像?

    我在尝试如何在pyqtqlistview中正确地完成异步映像加载.在 我的主小部件由一个Qlistview和一个QLineEdit文本框组成. 我有一个参与者数据库,我使用QAbstractListM ...

  5. python怎么交换xy轴_如何在matplotlib中更改x和y轴?

    代码中的内容是如何在matplotlib中启动直方图的示例.注意,您使用的是pyplot默认接口(不一定要构建自己的图形). 因此这一行:orientation=u'vertical', 应该是:or ...

  6. mysql节假日函数_如何在MySQL中计算不包括周末和节假日的日期差

    我需要计算两个日期之间的天数(工作日),不包括周末(最重要)和假期 SELECT DATEDIFF(end_date, start_date) from accounts 但是,我不知道该如何在MyS ...

  7. python求字典的平均值_获取字典列表中值的平均值

    我必须创建一个名为read_data的函数,该函数将文件名作为其唯一参数.然后,此函数必须使用给定名称打开文件并返回字典,其中的键是文件中的位置名称,值是读数列表. 第一个函数的结果起作用并显示: { ...

  8. python如何更改entry属性_如何在Python3中更改Gtk3 Entry文本颜色?

    我在我的应用程序中有一个Gtk.Entry()列表,我想改变其中一些文本的颜色. 我尝试了以下方法: #!/usr/bin/python3 # Filename: mywindow.py from g ...

  9. java如何获得键值_如何在java中取map中的键值 的两种方法

    第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; public class Test { /** * @param args ...

最新文章

  1. Blender写实建筑场景制作学习教程 Exterior Visualization in Blender 2.9
  2. linux 进程阻塞 语句,MPI进程拓扑及非阻塞通信程序示例
  3. BZOJ4373: 算术天才⑨与等差数列
  4. 查看电脑电池损耗的命令
  5. mysql创建一个表用来快速查询表_mysql数据库的创建表格、查询(多表查询)
  6. 头部数据人才24小时图鉴
  7. mysql offset函数_mysql查询语句解析
  8. vue element container 子路由
  9. vb.net编写函数应该在哪里_编写代码时清晰至上
  10. 【2019杭电多校第六场1011=HDU6644】11 Dimensions(dp+思维)
  11. ddm模型公式_绝对估值法DDM、DCF模型和RNAV简介
  12. 摄影测量空间后方交会外方位元素的解算程序
  13. 计算机水平cet2是什么等级,英语cet2等级考试试题
  14. java计算机毕业设计企业固定资产管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  15. 微软通过云存储插件简化Docker容器迁移
  16. 【Linux修炼】开篇
  17. 地下城堡游戏小脚本儿——自动炼金
  18. 加州大学圣克鲁兹分校计算机科学,加州大学圣克鲁兹分校专业设置详细介绍!...
  19. Android 基于Zxing二维码扫描的光速实现
  20. Chem 3D软件可以改变背景吗

热门文章

  1. 智能投顾奇葩发展术:越靠“爹”,越有机会
  2. js、jq实现答题上一题下一题
  3. 投资理念研究分析报告
  4. 宜信唐宁忠告北大毕业生 日进斗金不如守住初心
  5. 网络安全(Web-safe)字体
  6. java自动违例设计,Java违例控制,java违例
  7. 邻居好说话——冒泡排序
  8. DEI1016BD429使用遇到的问题及解决
  9. 教室录播系统方案_精品课程录播教室建设方案
  10. 相变材料二氧化钒薄膜制备总结