網站首頁 編程語言 正文
numpy下fft模塊提供了豐富的fft函數,幾種常用的在這里記錄一下使用方式
fft
輸入實數samples,如果輸入的sample是帶虛數部分的話,虛數部分會被默認刪除。
t=np.arange(12)
b=np.sin(t)
print(b)
print("sum(b)=", np.sum(b))
s = np.fft.fft(b)
print(s)
運行結果截圖如下
從圖中可以看到,
- [0]是一個實數,實數部分是所有input中各個元素之和。
- [i]與[N-i]共軛;輸入的N如果是偶數,那么[N/2]沒有共軛的元素。 rfft
rfft
其實就是對fft的結果輸出做了省略。 針對剛剛提到的共軛特性,其實輸出結果是要保留(N+1)//2個結果就可以了。
t=np.arange(12)
b=np.sin(t)
print(b)
print("sum(b)=", np.sum(b))
s = np.fft.fft(b)
print("fft result:", s)
s = np.fft.rfft(b)
print("rfft result:", s)
fftfreq
返回fft的頻率節點
上面的fft和rfft將時域數據轉為頻域,得到的數據的bin是哪些范圍?
可以通過fftfreq來獲取
第一個參數n是時域數據的數據個數,第二個參數d是表示每一個bin的尺度。一般是1/sample_rate
t=np.arange(12)
b=np.sin(t)
print(b)
print("sum(b)=", np.sum(b))
s = np.fft.fft(b)
print("fft result:", s)
s = np.fft.rfft(b)
print("rfft result:", s)
s= np.fft.fftfreq(12, d=1/8000)
print(s)
其結果為
[ ? ?0. ? ? ? ? ?666.66666667 ? ? ? ? ? ? ? ? ? ? ?1333.33333333 ? ? ? ? ? ? ? ? ? ? 2000.
? 2666.66666667 ?3333.33333333 ? ? ? ? ? ? ? ? ? ?-4000. ? ? ? ? ? ? ? ? ? ? ? ? ?-3333.33333333
?-2666.66666667 -2000. ? ? ? ? ? ? ? ? ? ? ? ? ? ?-1333.33333333 ? ? ? ? ? ? ? ? ? ?-666.66666667]
那么結合rfft的數據就有
Bin | Range | Value |
---|---|---|
bin[1] | 1~667HZ | 0.46997981+0.41183211j |
bin[2] | 667~1334HZ | -1.36179847-5.76500237j |
bin[3] | 1334~2000HZ | 0.14669493-0.4965488j |
bin[4] | 2000~2667HZ | 0.20513541-0.2233417j |
bin[5] | 2667~3333HZ | 0.22157176-0.09538547j |
bin[6] | 3333~4kHZ | 0.22563497+0.j |
ifft
ifft是逆向fft操作,代碼如下
import numpy as np
t=np.arange(12)
b=np.sin(t)
print(b)
s = np.fft.fft(b)
#print(s)
y = np.fft.ifft(s)
print("restore:", y)
它的結果雖然也是復數,但是在實數部分,可以看到,就是結果;
所以也可以直接輸出實數部分np.fft.ifft(s).real
irfft
irfft是配合rfft使用的; 上面的例子可以看到,如果信號長度是n, 那么fft的輸出結果的長度也是n;
但是rfft的結果是n//2+1;
irfft匹配的是rfft,所以它的參數長度與ifft是不同的;兩者也不可混用。
import numpy as np
t=np.arange(12)
b=np.sin(t)
print(b)
s = np.fft.rfft(b)
#print(s)
y = np.fft.irfft(s)
print("restore:", y)
總結
原文鏈接:https://blog.csdn.net/mimiduck/article/details/118549640
相關推薦
- 2022-08-19 Python包中__init__.py文件的作用與用法實例詳解_python
- 2022-08-22 Python基礎異常處理梳理總結_python
- 2023-05-14 Python實現批量導入1000條xlsx數據_python
- 2023-01-15 rust異步編程詳細講解_Rust語言
- 2022-09-03 ASP.NET實現Repeater控件的數據綁定_基礎知識
- 2022-09-13 cmd命令打開及切換目錄路徑的實現_DOS/BAT
- 2022-11-10 Redis源碼設計剖析之事件處理示例詳解_Redis
- 2023-02-10 python使用xlsx和pandas處理Excel表格的操作步驟_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支