numpy.fft.fft 中参数 n 的影响是什么

What is the impact of argument n in numpy.fft.fft

我想将 fft 应用于我的时间序列数据,以提取最低的 5 个主要频率分量,以预测每个时间序列末尾的 y 值(细菌计数)。我的代码如下:

df = pd.read_csv('/content/drive/My Drive/df.csv', sep=',') 
X = df.iloc[0:2,0:10000]

dft_X = np.fft.fft(X)     # What should I fill in for argument n?
print(dft_X) 
print(len(dft_X))
plt.plot(dft_X)
plt.grid(True)
plt.show()

for i in dft_X: 

    m = i[np.argpartition(i,5)[:5]]
    n = i[np.argpartition(i,range(5))[:5]]

print(m,'\n',n)

numpy.fft.fft 上的 scipy 文档中指出

numpy.fft.fft(a, n=None, axis=-1, norm=None)

...

n : int, optional

Length of the transformed axis of the output. If n is smaller than the length of the input, the input is cropped. If it is larger, the input is padded with zeros. If n is not given, the length of the input along the axis specified by axis is used.

但我仍然不清楚参数 n 值对输出的影响以及如何决定使用什么值。

我注意到当n = 10时,输出如下:

# n= 10
# [-1.5       -1.11022302e-16j -0.46352549-1.42658477e+00j
#  -1.21352549-8.81677878e-01j -1.21352549+8.81677878e-01j
#  -0.46352549+1.42658477e+00j] 
#  [-1.5       -1.11022302e-16j -1.21352549-8.81677878e-01j
#  -1.21352549+8.81677878e-01j -0.46352549-1.42658477e+00j
#  -0.46352549+1.42658477e+00j]

而当n = 10000时,输出如下:

# n= 10000
# [-4752.15448944 +4113.44846878j -5199.36419709 -1826.78753048j
#  -4659.45705354-13014.97971229j -4752.15448944 -4113.44846878j
#  -5199.36419709 +1826.78753048j] 
#  [-5199.36419709 -1826.78753048j -5199.36419709 +1826.78753048j
#  -4752.15448944 -4113.44846878j -4752.15448944 +4113.44846878j
#  -4659.45705354-13014.97971229j]

什么决定了要使用的正确 n 值?此外,为什么输出值是复数?感谢任何帮助。

时间序列图供参考:

But I am still not clear about the effect of argument n value on the output and how to decide what value to use.

对于 "n = size of input",结果是简单的离散傅里叶变换:它表示持续时间 (T = n dt) 正好在频率 space 中的信号。最低频率成分是sine/cosine波周期2T.

对于 "n > size of input",您执行的信号变换是附加了零的原始信号。因此,可以表示的最低频率对应于较长的波周期 2T。信号因此被突然削减为零。根据输入信号,这可能会引入不需要的高频分量。

对于 "n < size of input",您截断了信号。如果您有 "stationary signal",分析较短的样本(可能使用窗口)可能是有意义的。

What determines the right n value to use?

这取决于应用程序和采样(非常长的平稳序列,短测量,...)。除非你有某种理由使用该选项,否则你可以省略 n.

Besides, why are output values complex numbers?

实数信号的傅里叶变换通常很复杂。它仅对偶数信号有效。

您可以使用 FFT 和简单信号(例如纯正弦或余弦)来理解这一点。