前面的知识这里就不介绍了,下面是Python语言实现的离散傅里叶变换的处理:
时域信号的函数表达要处理的时域信号: f ( t ) = s i n ( t ) + 2 s i n ( 3 t ) + 2 c o s ( 2 t ) + 4 s i n ( 15 t ) f(t) = sin(t) + 2sin(3t) + 2cos(2t) + 4sin(15t) f(t)=sin(t)+2sin(3t)+2cos(2t)+4sin(15t)
绘制函数图像import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.sin(x) + 2*np.sin(3*x) + 2*np.cos(3*x) + 4*np.sin(15*x)
x = np.linspace(0, 2*np.pi, 2048)
plt.scatter(x, f(x))
plt.grid
plt.show()
下面是可视化出来的时域中信号在一个2π周期内的形态:
import numpy as np
from scipy.fftpack import fft
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 128)
y = np.sin(x) + 2*np.sin(3*x) + 2*np.cos(3*x) + 4*np.sin(15*x)
# 离散频率
xf = np.arange(len(y))
# 由于对称性,因此只取一半区域
xf_half = xf[range(int(len(x)/2))]
# 执行完fft以后,对各频率的能量进行归一化处理
yf = abs(fft(y))/len(x)
# 由于对称性,因此只取一半区间
yf_half = yf[range(int(len(x)/2))]
plt.plot(xf_half, yf_half)
plt.show()
可见,图中三个能量最高的峰值点,正对应时域函数
f
(
t
)
=
s
i
n
(
t
)
+
2
s
i
n
(
3
t
)
+
2
c
o
s
(
2
t
)
+
4
s
i
n
(
15
t
)
f(t) = sin(t) + 2sin(3t) + 2cos(2t) + 4sin(15t)
f(t)=sin(t)+2sin(3t)+2cos(2t)+4sin(15t) 中合成的三个谐波频率,且能量也和各谐波函数取模后的比例保持一致。