如何解释这个 fft 图
How to interpret this fft graph
我想使用 fft
函数对我的时间序列数据应用傅立叶变换,以通过提取观测数据中的主要频率分量来找到 "patterns",即。最低的 5 个主要频率来预测每个时间序列结束时的 y 值(细菌计数)。
我想保留最小的5个系数作为特征,并消除其余的。
我的代码如下:
df = pd.read_csv('/content/drive/My Drive/df.csv', sep=',')
X = df.iloc[0:2,0:10000]
dft_X = np.fft.fft(X)
print(dft_X)
print(len(dft_X))
plt.plot(dft_X)
plt.grid(True)
plt.show()
# What is the graph about(freq/amplitude)? How much data did it use?
for i in dft_X:
m = i[np.argpartition(i,5)[:5]]
n = i[np.argpartition(i,range(5))[:5]]
print(m,'\n',n)
这是输出:
但我不确定如何解释这张图。准确地说,
1) 图形是否显示输入数据的转换值?我只用了2行数据(每行是一个时间序列),所以数据是2x10000
,为什么图中有这么多行?
2) 要获取频率值,我应该使用np.fft.fftfreq(n, d=timestep)
吗?
Parameters:
n : int
Window length.
d : scalar, optional
Sample spacing (inverse of the sampling rate). Defaults to 1.
Returns:
f : ndarray
Array of length n containing the sample frequencies.
如何确定n
(window长度)和sample spacing
?
3) 为什么转换后的值都是复数?
谢谢
我会按照你问题的相反顺序回答
3) Why are transformed values all complex numbers?
傅里叶变换的输出总是复数。为了解决这个问题,您可以在转换的输出上应用绝对值,或者仅使用以下方法绘制实部:
plt.plot(dft_X.real)
2) To obtain frequency value, should I use np.fft.fftfreq(n, d=timestep)?
不,"frequency values" 将在 FFT 的输出中可见。
1) Does the graph show the transformed values of the input data? I only used 2 rows of data(each row is a time series), thus data is 2x10000, why are there so many lines in the graph?
你的图表有这么多线,因为它为你的数据集的每一列制作了一条线。分别在每一行上应用 FFT(或者可能只是转置你的数据帧),然后你会得到更多实际的频域图。
跟进
Would using absolute value or real part of the output as features for a later model have different effect than using the original output?
通常使用绝对值更容易。
使用实部
使用绝对值
这是生成此代码的 Octave 代码:
Fs = 4000; % Sampling rate of signal
T = 1/Fs; % Period
L = 4000; % Length of signal
t = (0:L-1)*T; % Time axis
freq = 1000; % Frequency of our sinousoid
sig = sin(freq*2*pi*t); % Fill Time-Domain with 1000 Hz sinusoid
f_sig = fft(sig); % Apply FFT
f = Fs*(0:(L/2))/L; % Frequency axis
figure
plot(f,abs(f_sig/L)(1:end/2+1)); % peak at 1kHz)
figure
plot(f,real(f_sig/L)(1:end/2+1)); % main peak at 1kHz)
在我的示例中,您可以看到绝对值在我生成的频率为 1kHz 的正弦波以外的频率下没有返回任何噪声,而实部在 1kHz 处具有更大的峰值但也有更多的噪声。
至于效果,我不知道你是什么意思。
is it expected that "frequency values" always be complex numbers
总是?不。傅里叶级数表示正弦和余弦之和完全等于任何连续周期函数的频率系数。正弦和余弦可以通过欧拉公式写成复数形式。这是存储傅里叶系数最方便的方法。实际上,频域信号的虚部代表信号的相位。 (即,如果我有 2 个相同频率的正弦函数,它们可以有不同的复杂形式,具体取决于时移)。但是,大多数提供 FFT 函数的库默认情况下会将 FFT 系数存储为复数,以方便相位和幅度计算。
Is it convention that FFT use each column of dataset when plotting a line
我认为这是 mathplotlib.plot 的问题,而不是 np.fft。
Could you please show me how to apply FFT on each row separately
有很多方法可以解决这个问题,我不想强迫你走一条路,所以我会提出通用的解决方案来遍历数据帧的每一行并对每一行应用 FFT。否则,在您的情况下,我相信转置您的输出 可能 也可以。
我想使用 fft
函数对我的时间序列数据应用傅立叶变换,以通过提取观测数据中的主要频率分量来找到 "patterns",即。最低的 5 个主要频率来预测每个时间序列结束时的 y 值(细菌计数)。
我想保留最小的5个系数作为特征,并消除其余的。
我的代码如下:
df = pd.read_csv('/content/drive/My Drive/df.csv', sep=',')
X = df.iloc[0:2,0:10000]
dft_X = np.fft.fft(X)
print(dft_X)
print(len(dft_X))
plt.plot(dft_X)
plt.grid(True)
plt.show()
# What is the graph about(freq/amplitude)? How much data did it use?
for i in dft_X:
m = i[np.argpartition(i,5)[:5]]
n = i[np.argpartition(i,range(5))[:5]]
print(m,'\n',n)
这是输出:
但我不确定如何解释这张图。准确地说,
1) 图形是否显示输入数据的转换值?我只用了2行数据(每行是一个时间序列),所以数据是2x10000
,为什么图中有这么多行?
2) 要获取频率值,我应该使用np.fft.fftfreq(n, d=timestep)
吗?
Parameters:
n : int Window length.d : scalar, optional Sample spacing (inverse of the sampling rate). Defaults to 1.
Returns:
f : ndarray Array of length n containing the sample frequencies.
如何确定n
(window长度)和sample spacing
?
3) 为什么转换后的值都是复数?
谢谢
我会按照你问题的相反顺序回答
3) Why are transformed values all complex numbers?
傅里叶变换的输出总是复数。为了解决这个问题,您可以在转换的输出上应用绝对值,或者仅使用以下方法绘制实部:
plt.plot(dft_X.real)
2) To obtain frequency value, should I use np.fft.fftfreq(n, d=timestep)?
不,"frequency values" 将在 FFT 的输出中可见。
1) Does the graph show the transformed values of the input data? I only used 2 rows of data(each row is a time series), thus data is 2x10000, why are there so many lines in the graph?
你的图表有这么多线,因为它为你的数据集的每一列制作了一条线。分别在每一行上应用 FFT(或者可能只是转置你的数据帧),然后你会得到更多实际的频域图。
跟进
Would using absolute value or real part of the output as features for a later model have different effect than using the original output?
通常使用绝对值更容易。
使用实部
Fs = 4000; % Sampling rate of signal
T = 1/Fs; % Period
L = 4000; % Length of signal
t = (0:L-1)*T; % Time axis
freq = 1000; % Frequency of our sinousoid
sig = sin(freq*2*pi*t); % Fill Time-Domain with 1000 Hz sinusoid
f_sig = fft(sig); % Apply FFT
f = Fs*(0:(L/2))/L; % Frequency axis
figure
plot(f,abs(f_sig/L)(1:end/2+1)); % peak at 1kHz)
figure
plot(f,real(f_sig/L)(1:end/2+1)); % main peak at 1kHz)
在我的示例中,您可以看到绝对值在我生成的频率为 1kHz 的正弦波以外的频率下没有返回任何噪声,而实部在 1kHz 处具有更大的峰值但也有更多的噪声。
至于效果,我不知道你是什么意思。
is it expected that "frequency values" always be complex numbers
总是?不。傅里叶级数表示正弦和余弦之和完全等于任何连续周期函数的频率系数。正弦和余弦可以通过欧拉公式写成复数形式。这是存储傅里叶系数最方便的方法。实际上,频域信号的虚部代表信号的相位。 (即,如果我有 2 个相同频率的正弦函数,它们可以有不同的复杂形式,具体取决于时移)。但是,大多数提供 FFT 函数的库默认情况下会将 FFT 系数存储为复数,以方便相位和幅度计算。
Is it convention that FFT use each column of dataset when plotting a line
我认为这是 mathplotlib.plot 的问题,而不是 np.fft。
Could you please show me how to apply FFT on each row separately
有很多方法可以解决这个问题,我不想强迫你走一条路,所以我会提出通用的解决方案来遍历数据帧的每一行并对每一行应用 FFT。否则,在您的情况下,我相信转置您的输出 可能 也可以。