python实现FFT(快速傅里叶变换)

简单定义一个FFT函数,以后的使用中可以直接幅值粘贴使用。

首先生成了一个频率为1、振幅为1的正弦函数:
频率为1
然后计算该信号的频率和幅值,得到计算结果如下:
在这里插入图片描述
其中计算相位角我使用的较少,为了提高计算效率一般是注释掉了,不在意这点效率的话可以保留。

# 所使用到的库函数
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft

# 简单定义一个FFT函数
def myfft(x,t):
    fft_x = fft(x)                                            #  fft计算
    amp_x = abs(fft_x)/len(x)*2                                 # 纵坐标变换
    label_x = np.linspace(0,int(len(x)/2)-1,int(len(x)/2))    # 生成频率坐标
    amp = amp_x[0:int(len(x)/2)]                              # 选取前半段计算结果即可
    # amp[0] = 0                                              # 可选择是否去除直流量信号
    fs =1/( t[2]-t[1])                                        # 计算采样频率
    fre = label_x/len(x)*fs                                   # 频率坐标变换
    pha = np.unwrap(np.angle(fft_x))                          # 计算相位角并去除2pi跃变
    return amp,fre,pha                                        # 返回幅度和频率


t = np.linspace(0,5*np.pi,200)                                # 时间坐标
x = np.sin(2*np.pi*t)                                         # 正弦函数

amp,fre,pha= myfft(x,t)                                       # 调用函数

# 绘图
plt.figure()
plt.plot(t,x)
plt.title('Signal')
plt.xlabel('Time / s')
plt.ylabel('Intencity / cd')

plt.figure()
plt.plot(fre,amp)
plt.title('Amplitute-Frequence-Curve')
plt.ylabel('Amplitute / a.u.')
plt.xlabel('Frequence / Hz')
plt.show()

我的另一篇文章里给了FFT文件的函数包及调用方法:
链接: https://blog.csdn.net/ruredfive/article/details/116024515.

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐