如何在 Numpy 中计算傅立叶级数?
我有一个周期为 T 的周期函数,想知道如何获取傅立叶系数列表。我尝试使用 numpy 中的 fft 模块,但它似乎更专注于傅里叶变换而不是级数。 也许是缺乏数学知识,但我不知道如何从 fft 计算傅里叶系数。
感谢帮助和/或示例。
I have a periodic function of period T and would like to know how to obtain the list of the Fourier coefficients. I tried using fft module from numpy but it seems more dedicated to Fourier transforms than series.
Maybe it a lack of mathematical knowledge, but I can't see how to calculate the Fourier coefficients from fft.
Help and/or examples appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最后,最简单的事情(用黎曼和计算系数)是解决我的问题的最便携/有效/稳健的方法:
给我:
In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem:
gives me:
这是一个老问题,但由于我必须对此进行编码,因此我在这里发布使用 numpy.fft 模块的解决方案,这可能比其他手工制作的解决方案更快。
DFT 是计算傅立叶级数系数达到数值精度的正确工具函数的,定义为参数的解析表达式或某些离散点上的数值插值函数。
这是一个实现,它允许通过传递适当的
return_complex
来计算傅里叶级数的实值系数或复值系数:这是一个用法示例:
以及所得
a0,a1,...,a10,b1,b2,...,b10
系数:这是针对两种操作模式的功能的可选测试。您应该在示例之后运行它,或者在运行代码之前定义一个周期函数
f
和一个周期T
。This is an old question, but since I had to code this, I am posting here the solution that uses the
numpy.fft
module, that is likely faster than other hand-crafted solutions.The DFT is the right tool for the job of calculating up to numerical precision the coefficients of the Fourier series of a function, defined as an analytic expression of the argument or as a numerical interpolating function over some discrete points.
This is the implementation, which allows to calculate the real-valued coefficients of the Fourier series, or the complex valued coefficients, by passing an appropriate
return_complex
:This is an example of usage:
And the plot of the resulting
a0,a1,...,a10,b1,b2,...,b10
coefficients:This is an optional test for the function, for both modes of operation. You should run this after the example, or define a periodic function
f
and a periodT
before running the code.Numpy 并不是真正计算傅里叶级数分量的正确工具,因为您的数据必须进行离散采样。您确实想使用 Mathematica 之类的东西,或者应该使用傅里叶变换。
为了粗略地做到这一点,让我们看一些简单的周期为 2pi 的三角波,其中我们可以轻松计算傅里叶系数(c_n = -i ((-1)^(n+1))/n for n>0; 例如, c_n = { -i, i/2, -i/3, i/4, -i/5, i/6, ... } 对于 n=1,2,3,4,5,6 (使用 Sum ( c_n exp(i 2 pi nx) ) 作为傅立叶级数)。
如果您查看前几个傅立叶分量:
首先忽略由于浮点精度而接近 0 的分量(~1e-16,为零)。更困难的部分是看到 3.14159 数字(在我们除以 1000 的周期之前出现的)也应该被识别为零,因为该函数是周期性的)。因此,如果我们忽略这两个因素,我们会得到:
你可以看到傅里叶级数与其他数字一样出现(我没有调查;但我相信这些分量对应于 [c0, c-1, c1, c-2 ,c2,...])。我根据 wiki 使用约定: http://en.wikipedia.org/wiki/Fourier_series。
再次,我建议使用能够积分和处理连续函数的mathematica 或计算机代数系统。
Numpy isn't the right tool really to calculate fourier series components, as your data has to be discretely sampled. You really want to use something like Mathematica or should be using fourier transforms.
To roughly do it, let's look at something simple a triangle wave of period 2pi, where we can easily calculate the Fourier coefficients (c_n = -i ((-1)^(n+1))/n for n>0; e.g., c_n = { -i, i/2, -i/3, i/4, -i/5, i/6, ... } for n=1,2,3,4,5,6 (using Sum( c_n exp(i 2 pi n x) ) as Fourier series).
If you look at the first several Fourier components:
First neglect the components that are near 0 due to floating point accuracy (~1e-16, as being zero). The more difficult part is to see that the 3.14159 numbers (that arose before we divide by the period of a 1000) should also be recognized as zero, as the function is periodic). So if we neglect those two factors we get:
and you can see the fourier series numbers come up as every other number (I haven't investigated; but I believe the components correspond to [c0, c-1, c1, c-2, c2, ... ]). I'm using conventions according to wiki: http://en.wikipedia.org/wiki/Fourier_series.
Again, I'd suggest using mathematica or a computer algebra system capable of integrating and dealing with continuous functions.
正如其他答案所提到的,您似乎正在寻找的是符号计算包,因此 numpy 不适合。如果您希望使用基于 Python 的免费解决方案,则可以使用 sympy 或 sage 应该可以满足您的需求。
As other answers have mentioned, it seems that what you are looking for is a symbolic computing package, so numpy isn't suitable. If you wish to use a free python-based solution, then either sympy or sage should meet your needs.
您是否有函数的离散样本列表,或者您的函数本身是离散的?如果是这样,使用 FFT 算法计算的离散傅里叶变换将直接提供傅里叶系数(请参阅此处)。
另一方面,如果您有该函数的解析表达式,您可能需要某种符号数学求解器。
Do you have a list of discrete samples of your function, or is your function itself discrete? If so, the Discrete Fourier Transform, calculated using an FFT algorithm, provides the Fourier coefficients directly (see here).
On the other hand, if you have an analytic expression for the function, you probably need a symbolic math solver of some kind.