电子说
这里做一下记录,关于FFT就不做介绍了,直接贴上代码,有详细注释的了:import numpy as npfrom scipy.fftpack import fft,ifftimport matplotlib.pyplot as pltimport seaborn#采样点选择1400个,因为设置的信号频率分量最高为600赫兹,根据采样定理知采样频率要大于信号频率2倍,所以这里设置采样频率为1400赫兹(即一秒内有1400个采样点,一样意思的)x=np.linspace(0,1,1400) #设置需要采样的信号,频率分量有180,390和600y=7*np.sin(2*np.pi*180*x) + 2.8*np.sin(2*np.pi*390*x)+5.1*np.sin(2*np.pi*600*x)yy=fft(y) #快速傅里叶变换yreal = yy.real # 获取实数部分yimag = yy.imag # 获取虚数部分yf=abs(fft(y)) # 取绝对值yf1=abs(fft(y))/len(x) #归一化处理yf2 = yf1[range(int(len(x)/2))] #由于对称性,只取一半区间xf = np.arange(len(y)) # 频率xf1 = xfxf2 = xf[range(int(len(x)/2))] #取一半区间plt.subplot(221)plt.plot(x[0:50],y[0:50]) plt.title('Original wave')plt.subplot(222)plt.plot(xf,yf,'r')plt.title('FFT of Mixed wave(two sides frequency range)',fontsize=7,color='#7A378B') #注意这里的颜色可以查询颜色代码表plt.subplot(223)plt.plot(xf1,yf1,'g')plt.title('FFT of Mixed wave(normalization)',fontsize=9,color='r')plt.subplot(224)plt.plot(xf2,yf2,'b')plt.title('FFT of Mixed wave)',fontsize=10,color='#F08080')plt.show()
结果:
再添加一个简单的例子
# -*- coding: utf-8 -*-import matplotlib.pyplot as pltimport numpy as npimport seabornFs = 150.0; # sampling rate采样率Ts = 1.0/Fs; # sampling interval 采样区间t = np.arange(0,1,Ts) # time vector,这里Ts也是步长ff = 25; # frequency of the signaly = np.sin(2*np.pi*ff*t)n = len(y) # length of the signalk = np.arange(n)T = n/Fsfrq = k/T # two sides frequency rangefrq1 = frq[range(int(n/2))] # one side frequency rangeYY = np.fft.fft(y) # 未归一化Y = np.fft.fft(y)/n # fft computing and normalization 归一化Y1 = Y[range(int(n/2))]fig, ax = plt.subplots(4, 1)ax[0].plot(t,y)ax[0].set_xlabel('Time')ax[0].set_ylabel('Amplitude')ax[1].plot(frq,abs(YY),'r') # plotting the spectrumax[1].set_xlabel('Freq (Hz)')ax[1].set_ylabel('|Y(freq)|')ax[2].plot(frq,abs(Y),'G') # plotting the spectrumax[2].set_xlabel('Freq (Hz)')ax[2].set_ylabel('|Y(freq)|')ax[3].plot(frq1,abs(Y1),'B') # plotting the spectrumax[3].set_xlabel('Freq (Hz)')ax[3].set_ylabel('|Y(freq)|')plt.show()
全部0条评论
快来发表一下你的评论吧 !