簡體   English   中英

numpy.fft.fft 和 numpy.fft.fftfreq 有什么區別

[英]What is the difference between numpy.fft.fft and numpy.fft.fftfreq

我正在分析時間序列數據,並希望提取 5 個主要頻率分量並將其用作訓練機器學習模型的特征。 我的數據集是921 x 10080 每行是一個時間序列,總共有 921 個。

在探索可能的方法時,我遇到了各種函數,包括numpy.fft.fftnumpy.fft.fftfreqDFT ......我的問題是,這些函數對數據集有什么作用以及這些函數之間有什么區別?

對於Numpy.fft.fft ,Numpy 文檔說明:

Compute the one-dimensional discrete Fourier Transform.

This function computes the one-dimensional n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT].

而對於numpy.fft.fftfreq

numpy.fft.fftfreq(n, d=1.0)
Return the Discrete Fourier Transform sample frequencies.

The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

但這並沒有真正與我交談,可能是因為我沒有信號處理的背景知識。 我應該為我的案例使用哪個函數,即。 為數據集的每一行提取前 5 個主要頻率和幅度分量? 謝謝


更新:

使用下面的fft返回結果。 我的目的是獲得每個時間序列的前 5 個頻率和幅度值,但它們是頻率分量嗎?

這是代碼:

def get_fft_values(y_values, T, N, f_s):
    f_values = np.linspace(0.0, 1.0/(2.0*T), N//2)
    fft_values_ = rfft(y_values)
    fft_values = 2.0/N * np.abs(fft_values_[0:N//2])
    return f_values[0:5], fft_values[0:5]  #f_values - frequency(length = 5040) ; fft_values - amplitude (length = 5040)

t_n = 1
N = 10080
T = t_n / N
f_s = 1/T

result = pd.DataFrame(df.apply(lambda x: get_fft_values(x, T, N, f_s), axis =1)) 
result

和輸出

0   ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [52.91299603174603, 1.2744877093061115, 2.47064631896607, 1.4657299825335832, 1.9362280837538701])
1   ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [57.50430555555556, 4.126212552498241, 2.045294347349226, 0.7878668631936439, 2.6093502232989976])
2   ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [52.05765873015873, 0.7214089616631307, 1.8547819994826562, 1.3859749465142301, 1.1848485830307878])
3   ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [53.68928571428572, 0.44281647644149114, 0.3880646059685434, 2.3932194091895043, 0.22048418335196407])
4   ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [52.049007936507934, 0.08026717757664162, 1.122163085234073, 1.2300320578011028, 0.01109727616896663])
... ...
916 ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [74.39303571428572, 2.7956204803382096, 1.788360577194303, 0.8660509272194551, 0.530400826933975])
917 ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [51.88751984126984, 1.5768804453161231, 0.9932384706239461, 0.7803585797514547, 1.6151532436755451])
918 ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [52.16263888888889, 1.8672674706267687, 0.9955183554654834, 1.0993971449470716, 1.6476405255363171])
919 ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [59.22579365079365, 2.1082518972190183, 3.686245044113031, 1.6247500816133893, 1.9790245755039324])
920 ([0.0, 1.000198452073824, 2.000396904147648, 3.0005953562214724, 4.000793808295296], [59.32333333333333, 4.374568790482763, 1.3313693716184536, 0.21391538068483704, 1.414774377287436])

首先需要了解信號有時域和頻域表示。 下圖顯示了一些常見的基本信號類型及其時域和頻域表示。

在此處輸入圖片說明

請密切注意正弦曲線,我將用它來說明 fft 和 fftfreq 之間的區別。

傅立葉變換是時域和頻域表示之間的門戶。 因此

numpy.fft.fft() - 返回傅立葉變換。 這將有實部和虛部。 實部和虛部本身並不是特別有用,除非您對圍繞數據窗口中心的對稱屬性(偶數與奇數)感興趣。

numpy.fft.fftfreq - 以每單位樣本間距的周期返回頻率倉中心的浮點數組。

numpy.fft.fft()方法是一種獲得正確頻率的方法,可以讓您正確分離 fft。

這最好用一個例子來說明:

import numpy as np
import matplotlib.pyplot as plt

#fs is sampling frequency
fs = 100.0
time = np.linspace(0,10,int(10*fs),endpoint=False)

#wave is the sum of sine wave(1Hz) and cosine wave(10 Hz)
wave = np.sin(np.pi*time)+ np.cos(np.pi*time)
#wave = np.exp(2j * np.pi * time )

plt.plot(time, wave)
plt.xlim(0,10)
plt.xlabel("time (second)")
plt.title('Original Signal in Time Domain')

plt.show()

時域信號

# Compute the one-dimensional discrete Fourier Transform.

fft_wave = np.fft.fft(wave)

# Compute the Discrete Fourier Transform sample frequencies.

fft_fre = np.fft.fftfreq(n=wave.size, d=1/fs)

plt.subplot(211)
plt.plot(fft_fre, fft_wave.real, label="Real part")
plt.xlim(-50,50)
plt.ylim(-600,600)
plt.legend(loc=1)
plt.title("FFT in Frequency Domain")

plt.subplot(212)
plt.plot(fft_fre, fft_wave.imag,label="Imaginary part")
plt.legend(loc=1)
plt.xlim(-50,50)
plt.ylim(-600,600)
plt.xlabel("frequency (Hz)")

plt.show()

在此處輸入圖片說明

如果“主要成分”是指 5 個最強的頻率,您將在np.fft.fft()的結果中搜索這些值。要知道這些值屬於哪個頻率,您將使用np.fft.fftfreq 。兩者的輸出將是相同長度的陣列,從而可以從喂你指數np.fft.fft()到陣列從np.fft.fftfreq()以獲得相應的頻率。

例如,假設fft的輸出是 A, fftfreq的輸出是 B,假設 A[1] 是您的主要組件之一,B[1] = 0Hz 將是您的主要組件的頻率。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM