生活随笔
收集整理的這篇文章主要介紹了
numpy的快速傅里叶变换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
快速傅里葉變換(fft)
什么是傅里葉變換?
傅里葉定理: 任何一條周期曲線, 無(wú)論多么跳躍或不規(guī)則, 都能表示成一組光滑正弦曲線疊加之和. 傅里葉變換即是把這條周期曲線拆解成一組光滑正弦曲線的過(guò)程.
傅里葉變換的目的是將時(shí)域(時(shí)間域)上的信號(hào)轉(zhuǎn)變?yōu)轭l域(頻率域)上的信號(hào), 隨著域的不同,對(duì)同一個(gè)事物的了解角度也隨之改變. 因此在時(shí)域中某些不好處理的地方, 放在頻域中就可以較為簡(jiǎn)單的處理. 這樣可以大量減少處理的數(shù)據(jù)量.
傅里葉定理:
y=A1sin(ω1x+?1)+A2sin(ω2x+?2)+..+Cy = A_1sin(\omega_1x+\phi_1) + A_2sin(\omega_2x+\phi_2) + .. +C y=A1?sin(ω1?x+?1?)+A2?sin(ω2?x+?2?)+..+C
快速傅里葉變換相關(guān)API:
import numpy
.fft
as nf
freqs
= nf
.fftfreq
(采樣數(shù)量
, 采樣周期
)
復(fù)數(shù)序列
= nf
.fft
(原函數(shù)值序列
)
原函數(shù)值序列
= nf
.ifft
(復(fù)數(shù)序列
)
案例: 基于傅里葉變換, 拆解方波.
"""
demo04_fft.py 傅里葉變換
"""
import numpy
as np
import matplotlib
.pyplot
as mp
import numpy
.fft
as nfx
= np
.linspace
(-2*np
.pi
, 2*np
.pi
, 1000)
y
= np
.zeros
(x
.size
)
for i
in range(1, 1000):y
+= 4/(2*i
-1)*np
.pi
* np
.sin
((2*i
-1)*x
)
ffts
= nf
.fft
(y
)
freqs
= nf
.fftfreq
(x
.size
, x
[1]-x
[0])
pows
= np
.abs(ffts
)mp
.figure
('FFT', facecolor
='lightgray')
mp
.subplot
(121)
mp
.grid
(linestyle
=':')
mp
.plot
(x
, y
,linewidth
=2)
mp
.subplot
(122)
mp
.grid
(linestyle
=':')
mp
.plot
(freqs
[freqs
>0], pows
[freqs
>0], c
='orangered')
y2
= nf
.ifft
(ffts
)
mp
.subplot
(121)
mp
.plot
(x
, y2
, linewidth
=7, alpha
=0.5)mp
.show
()
基于傅里葉變換的頻域?yàn)V波
含噪信號(hào)是高能信號(hào)與低能噪聲疊加的信號(hào), 可以通過(guò)傅里葉變換的頻域?yàn)V波實(shí)現(xiàn)簡(jiǎn)單降噪.
通過(guò)FFT使含噪信號(hào)轉(zhuǎn)換為含噪頻譜, 手動(dòng)取出低能噪聲, 留下高能頻譜后,再通過(guò)IFFT生成高能信號(hào).
讀取音頻文件, 獲取音頻的基本信息: 采樣個(gè)數(shù)/采樣周期/每個(gè)采樣點(diǎn)的聲音值. 繪制音頻的時(shí)域: 時(shí)間/位移圖像.
sample_rate
, noised_sigs
= \wf
.read
('../da_data/noised.wav')
print(noised_sigs
.shape
)
times
= np
.arange
(len(noised_sigs
))/sample_ratemp
.figure
('Filter', facecolor
='lightgray')
mp
.subplot
(2,2,1)
mp
.title
('Time Domain', fontsize
=16)
mp
.ylabel
('Signal', fontsize
=12)
mp
.tick_params
(labelsize
=8)
mp
.grid
(linestyle
=':')
mp
.plot
(times
[:178], noised_sigs
[:178], c
='dodgerblue', label
='Noised Sigs')
mp
.legend
()
mp
.show
()
基于傅里葉變換, 獲取音頻頻域信息, 繪制頻域: 頻率/能量圖像.
freqs
= nf
.fftfreq
(times
.size
, 1/sample_rate
)
noised_ffts
= nf
.fft
(noised_sigs
)
noised_pows
= np
.abs(noised_ffts
)
mp
.subplot
(222)
mp
.title
('Frequency Domain', fontsize
=16)
mp
.ylabel
('Power', fontsize
=12)
mp
.tick_params
(labelsize
=8)
mp
.grid
(linestyle
=":")
mp
.semilogy
(freqs
[freqs
>0], noised_pows
[freqs
>0],c
='orangered', label
='Noised')
mp
.legend
()
將低頻噪聲去除后繪制音頻頻域: 頻率/能量圖像.
noised_inds
= np
.where
(freqs
!= fund_freq
)
filter_ffts
= noised_ffts
.copy
()
filter_ffts
[noised_inds
] = 0
filter_pows
= np
.abs(filter_ffts
)
mp
.subplot
(224)
mp
.title
('Frequency Domain', fontsize
=16)
mp
.ylabel
('Power', fontsize
=12)
mp
.tick_params
(labelsize
=8)
mp
.grid
(linestyle
=":")
mp
.plot
(freqs
[freqs
>0], filter_pows
[freqs
>0],c
='orangered', label
='Filter')
mp
.legend
()
基于逆向傅里葉變換,生成時(shí)域的音頻信號(hào), 繪制時(shí)域: 時(shí)間/位移圖像.
filter_sigs
= nf
.ifft
(filter_ffts
)
mp
.subplot
(2,2,3)
mp
.title
('Time Domain', fontsize
=16)
mp
.ylabel
('Signal', fontsize
=12)
mp
.tick_params
(labelsize
=8)
mp
.grid
(linestyle
=':')
mp
.plot
(times
[:178], filter_sigs
[:178], c
='dodgerblue', label
='Filter Sigs')
mp
.legend
()
生成音頻文件
wf
.write
('../da_data/filter.wav', sample_rate
, filter_sigs
.astype
(np
.int16
))
總結(jié)
以上是生活随笔為你收集整理的numpy的快速傅里叶变换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。