Python: 傅里叶级数

目标

本文简述傅里叶级数(Fourier Series),并使用Python实现简单的傅里叶级数的展开。由于本人对数学不是很了解,纯粹从工科的角度出发,会用即可。有叙述不当之处请各位包涵与指正,非常感谢。

意义

傅里叶变换在各个领域有着广泛的应用。一篇有趣的文章《主宰世界的十大算法》在文章中排名第二。李永乐老师对傅里叶变换的视频评分是掌握世界本质的关键。我们可以看到它应用的广度和重要性。

[En]

Fourier transform is widely used in various fields. An interesting article “Ten algorithms that dominate the World” ranks second in the article. Teacher Li Yongle’s video rating of Fourier transform is the key to mastering the nature of the world. We can see the breadth and importance of its application.

例如,傅里叶变换在音频降噪中的应用是对音频信号进行傅里叶变换,然后去除音频中的低能频率部分,再通过傅里叶变换的逆变换将处理后的频域信号变换成声音信号,得到降噪后的音频数据。

[En]

For example, the application of Fourier transform in audio noise reduction is to carry out the Fourier transform of the audio signal, then remove the low-energy frequency part of the audio, and then transform the processed frequency domain signal into the sound signal through the inverse transform of the Fourier transform to get the audio data after noise reduction.

傅立叶变换后的频率信息也可以作为机器学习的特征输入。例如,在普通语音识别中,可以通过傅立叶变换将音频信号转换为频域信号,然后将该频域信号作为特征输入到深度神经网络进行模型训练。

[En]

The frequency information after Fourier transform can also be used as the feature input of machine learning. For example, in common speech recognition, the audio signal can be transformed into a frequency domain signal by Fourier transform, and then the frequency domain signal can be input as a feature to the depth neural network for model training.

简介

傅里叶级数可以用一句话来概括:任何周期函数都可以用正弦函数和余弦函数组成的无穷级数来表示。

[En]

Fourier series can be summed up in one sentence: any periodic function can be expressed by an infinite series composed of sine function and cosine function.

如下图的周期函数f(t),

Python: 傅里叶级数
可将其展开为:
Python: 傅里叶级数
具体来说,上图的方波可以分解为多少个sin(t)与cos(t)的组合呢?
先看如下函数的图像:
Python: 傅里叶级数

Python: 傅里叶级数
如果在f(t)中增加一项,则图像变为:
Python: 傅里叶级数
Python: 傅里叶级数

再加一项试试:

Python: 傅里叶级数

Python: 傅里叶级数
因此,当越来越多的多项式被分解为正无穷大时,图像就变成了方波(当然,这是不可能的)。
[En]

So, when more and more polynomials are decomposed to positive infinity, the image becomes a square wave (which is impossible, of course).

通过对上述方波进行傅里叶变换,还可以得出以下结论:

[En]

The following conclusions can also be drawn from the Fourier transform of the above square waves:

  1. 上述方波都是由sin()组成,即cos()的振幅(a0, a1, a2…)都为0。
  2. 频率为t的sin(),其振幅为6/pi,频率为3t的sin(), 其振幅为6/(3*pi)…

  3. 频率为t的sin()对方波起决定性影响。即方波的低频(t)部分更重要,因为低频的振幅最大。

; 实现

将上述的傅里叶级数表达式写简单点儿,如下图所示,目标即为了求出其中的A0, Ak, Bk,统称为傅里叶系数(Fourier coefficient)。

Python: 傅里叶级数
傅里叶系数的求解公式如下图所示,参考引用[8]中第5分钟左右的视频。
Python: 傅里叶级数
接下来看看求解傅里叶系数的代码实现,首先用代码生成f(x),其中-pi < x < pi, 0 < f(t) < 1。如下代码所示:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap

plt.rcParams['figure.figsize'] = [8, 8]
plt.rcParams.update({'font.size': 18})

Define domain
dx = 0.001
L = np.pi
x = L * np.arange(-1+dx,1+dx,dx)
n = len(x)
nquart = int(np.floor(n/4))
print(dx, L, x, n, nquart)

Define hat function
f = np.zeros_like(x)
f[nquart:2*nquart] = (4/n)*np.arange(1,nquart+1)
f[2*nquart:3*nquart] = np.ones(nquart) - (4/n)*np.arange(0,nquart)

fig, ax = plt.subplots()
ax.plot(x,f,'-',color='k',LineWidth=2)

Python: 傅里叶级数
其次,套用上述公式对f(x)求出傅里叶各级的系数。实际情况受算力限制,能计算的级数是有限的,通过lineNumber参数控制:
Compute Fourier series
name = "Accent"
cmap = get_cmap('tab10')
colors = cmap.colors
ax.set_prop_cycle(color=colors)

A0 = np.sum(f * np.ones_like(x)) * dx
fFS = A0/2

lineNumber = 3

A = np.zeros(lineNumber)
B = np.zeros(lineNumber)
for k in range(lineNumber):
    A[k] = np.sum(f * np.cos(np.pi*(k+1)*x/L)) * dx # Inner product
    B[k] = np.sum(f * np.sin(np.pi*(k+1)*x/L)) * dx
    fFS = fFS + A[k]*np.cos((k+1)*np.pi*x/L) + B[k]*np.sin((k+1)*np.pi*x/L)

    ax.plot(x,fFS,'-')
    print('Line ', k, ': A ', A[k], ' B ', B[k], ' serial shape ', fFS.shape)

Python: 傅里叶级数

复数表示

上述傅里叶级数的另一个更简洁的表达式是复数形式:

[En]

Another more concise expression of the above Fourier series is in the plural:

Python: 傅里叶级数
其中复数的实部与虚部分别表示每个频域分量的振幅与相位信息,更详细的说明可以从引用[1]中了解。坦白说,对我而言知道结论与使用就好:)。

; 参考

[1] https://www.youtube.com/watch?v=r6sGWTCMz2k&t=212s
[2] https://www.youtube.com/watch?v=MB6XGQWLV04&list=PLMrJAkhIeNNT_Xh3Oy0Y4LTj0Oxo8GqsC&index=2
[3] https://www.khanacademy.org/science/electrical-engineering/ee-signals/ee-fourier-series/v/ee-fourier-series-intro
[4] https://baike.baidu.com/item/%E5%82%85%E9%87%8C%E5%8F%B6%E7%BA%A7%E6%95%B0/5210337?fr=aladdin
[5] https://zh.wikipedia.org/wiki/%E5%82%85%E9%87%8C%E5%8F%B6%E7%BA%A7%E6%95%B0
[6] https://cloud.tencent.com/developer/article/1043465
[7] https://www.bilibili.com/video/av51932171/
[8] https://www.youtube.com/watch?v=Ud9Xtxsi2HI&list=PLMrJAkhIeNNT_Xh3Oy0Y4LTj0Oxo8GqsC&index=3

Original: https://blog.csdn.net/chenxiemin/article/details/108379862
Author: chenxiemin
Title: Python: 傅里叶级数

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/526805/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球