溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python如何實(shí)現(xiàn)快速傅里葉變換的方法

發(fā)布時(shí)間:2021-04-12 13:57:48 來(lái)源:億速云 閱讀:265 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python如何實(shí)現(xiàn)快速傅里葉變換的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體如下:

這里做一下記錄,關(guān)于FFT就不做介紹了,直接貼上代碼,有詳細(xì)注釋的了:

import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
import seaborn


#采樣點(diǎn)選擇1400個(gè),因?yàn)樵O(shè)置的信號(hào)頻率分量最高為600赫茲,根據(jù)采樣定理知采樣頻率要大于信號(hào)頻率2倍,所以這里設(shè)置采樣頻率為1400赫茲(即一秒內(nèi)有1400個(gè)采樣點(diǎn),一樣意思的)
x=np.linspace(0,1,1400)  

#設(shè)置需要采樣的信號(hào),頻率分量有180,390和600
y=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    # 獲取實(shí)數(shù)部分
yimag = yy.imag    # 獲取虛數(shù)部分

yf=abs(fft(y))    # 取絕對(duì)值
yf1=abs(fft(y))/len(x)   #歸一化處理
yf2 = yf1[range(int(len(x)/2))] #由于對(duì)稱性,只取一半?yún)^(qū)間

xf = np.arange(len(y))  # 頻率
xf1 = xf
xf2 = xf[range(int(len(x)/2))] #取一半?yún)^(qū)間


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()

結(jié)果:

Python如何實(shí)現(xiàn)快速傅里葉變換的方法

2017/7/11更新

再添加一個(gè)簡(jiǎn)單的例子

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import seaborn



Fs = 150.0;     # sampling rate采樣率
Ts = 1.0/Fs;    # sampling interval 采樣區(qū)間
t = np.arange(0,1,Ts)  # time vector,這里Ts也是步長(zhǎng)

ff = 25;     # frequency of the signal
y = np.sin(2*np.pi*ff*t)

n = len(y)     # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T     # two sides frequency range
frq1 = frq[range(int(n/2))] # one side frequency range

YY = 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 spectrum
ax[1].set_xlabel('Freq (Hz)')
ax[1].set_ylabel('|Y(freq)|')

ax[2].plot(frq,abs(Y),'G') # plotting the spectrum
ax[2].set_xlabel('Freq (Hz)')
ax[2].set_ylabel('|Y(freq)|')

ax[3].plot(frq1,abs(Y1),'B') # plotting the spectrum
ax[3].set_xlabel('Freq (Hz)')
ax[3].set_ylabel('|Y(freq)|')

plt.show()

Python如何實(shí)現(xiàn)快速傅里葉變換的方法

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Python如何實(shí)現(xiàn)快速傅里葉變換的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI