Python 實現簡單的快速傅利葉變換

2021-10-11 00:24:39 字數 3625 閱讀 6594

相比於matlab自帶的fft函式以及詳盡的官方文件來說,python在傅利葉變換這個方面相比就不是那麼簡單了,處處需要使用help檢視相關函式的定義。但是本質來說,都是傅利葉變換,只是程式語言不同而已。

import numpy as np

from scipy.fftpack import fft, ifft,fftshift

import matplotlib.pyplot as plt

from matplotlib.pylab import mpl

[

1] 阿里雲 http:

] 豆瓣http:

] 清華大學 https:

] 中國科學技術大學 http:

] 華中科技大學http:

怎麼使用呢?下面我給出乙個例子(在cmd命令下進行):

# 前面是對於庫函式的匯入

mpl.rcparams[

'font.sans-serif']=

['simhei'

] # 顯示中文

mpl.rcparams[

'axes.unicode_minus'

]= false # 顯示負號

# 取樣點選擇1400個,因為設定的訊號頻率分量最高為600赫茲,根據取樣定理知取樣頻率要大於訊號頻率2倍,所以這裡設定取樣頻率為1400赫茲(即一秒內有1400個取樣點,一樣意思的)

fs=1400

n=1400

x = np.

linspace(0

,1, fs)#生成時間刻度,生成原則是0到1,產生fs-

1個樣點,將區間分為fs份,共fs個點

# 設定需要取樣的訊號,頻率分量有200,400和600

y =7

* np.

sin(

2* np.pi *

200* x)+5

* np.

sin(

2* np.pi *

400* x)+3

* np.

sin(

2* np.pi *

600* x)

fft_y =

fft(y) # 快速傅利葉變換

flabel=fs*np.

arange

(n)/n

flabel1 = flabel-fs/

2 #雙邊譜頻率刻度

flabel2 = flabel[

range

(int

(n/2))

]#單邊譜頻率刻度

bothside =

fftshift

(np.

abs(fft_y)

) # 取複數的絕對值,即複數的模(雙邊頻譜),fft作用於原頻域樣點,使之滿足雙邊的條件

singleside = np.

abs(fft_y)#單邊譜

angle_y = np.

angle

(fft_y) # 取複數的角度

normalization_y = bothside / n # 歸一化處理(雙邊頻譜)

normalization_singleside=singleside/n;

normalization_half_y = normalization_singleside[

range

(int

(n /2)

)] # 由於對稱性,只取一半區間(單邊頻譜)

plt.

subplot

(231

)plt.

plot

(x, y)

plt.

title

('原始波形'

)plt.

subplot

(232

)plt.

plot

(flabel1, fft_y,

'black'

)plt.

title

('雙邊振幅譜(未求振幅絕對值)'

, fontsize=

9, color=

'black'

)plt.

subplot

(233

)plt.

plot

(flabel1, bothside,

'r')

plt.

title

('雙邊振幅譜(未歸一化)'

, fontsize=

9, color=

'red'

)plt.

subplot

(234

)plt.

plot

(flabel1, angle_y,

'violet'

)plt.

title

('雙邊相位譜(未歸一化)'

, fontsize=

9, color=

'violet'

)plt.

subplot

(235

)plt.

plot

(flabel1, normalization_y,

'g')

plt.

title

('雙邊振幅譜(歸一化)'

, fontsize=

9, color=

'green'

)plt.

subplot

(236

)plt.

plot

(flabel2, normalization_half_y,

'blue'

)plt.

title

('單邊振幅譜(歸一化)'

, fontsize=

9, color=

'blue'

)plt.

show

()①fft

fft就是dft的快速計算,定義式如下

d ft

=∑n=

0n−1

x[n]

e−j2

πn

n\mathrm\left\ =\sum_^n}}

dft=n=

0∑n−

1​x[

n]e−

jn2π

​n②fft****

這個函式在matlab也有出現,主要是做什麼的呢,你們是否還記得fft(dft)計算的結果是從[0,

]\left[ 0,2\pi \right]

[0,2π]

,以π\pi

π為週期。對應的頻率範圍為0頻到最高頻,再從負頻率到0,fftshift做的就是把頻率點,按照負頻率到正頻率重新排布,那麼我們生成對於的頻率標度與之對應就可以形成對乙個訊號的雙邊譜分析。

快速傅利葉

參考 ae 97 e6 b3 95 e5 ad a6 e4 b9 a0 e7 ac 94 e8 ae b0 hdu大整數乘法 include include include include include using namespace std const double pi acos 1.0 複數...

快速傅利葉(FFT)

快速傅利葉 更加形象的理解傅利葉變換 大概了解之後 從傅利葉級數到傅利葉變換 太大,只能裁剪為兩張 刨根問底的同學 雷德演算法 輸出序列是按自然順序排列的,而輸入序列的順序則是 位元反轉 方式排列的。也就是說,將序號用二進位制表示,然後將二進位制數以相反方向排列,再以這個數作為序號。如011變成11...

FFT快速傅利葉

description 給出兩個n位10進製整數x和y,你需要計算xy。input 第一行乙個正整數n。第二行描述乙個位數為n的正整數x。第三行描述乙個位數為n的正整數y output 輸出一行,即xy的結果。資料範圍 n 60000 乙個整數x a nan 1.a 0x a na a 0 x an...