python numpy.fft.rfft:为什么当包含或不包含 NFFT 时,输出有很大不同

python numpy.fft.rfft: why is when NFFT included or not, outputs are very different

我正在尝试理解 numpy.fft.rfft 中 NFFT 的含义。但是我很困惑,为什么当包含或不包含 NFFT 时,输出会变得非常不同。请看下面的例子。

numpy.fft.rfft([0, 1, 0, 0, 4.3, 3, 599], 8)
array([ 607.3         +0.j        ,   -5.71421356+600.41421356j,
   -594.7         -4.j        ,   -2.88578644-597.58578644j,
    599.3         +0.j        ])

numpy.fft.rfft([0, 1, 0, 0, 4.3, 3, 599])
array([ 607.3         +0.j        ,  369.55215218+472.32571033j,
   -133.53446083+578.34336489j, -539.66769135+261.30917157j])

FFT 是 Discrete Fourier Transform (DFT), which is a discrete function of frequency. It is also related to the Discrete-Time Fourier Transform (DTFT) 的有效实现,它本身是频率的连续函数。更具体地说,DFT 与在 DFT 的离散频率下评估的 DTFT 完全对应。

换句话说,当使用 numpy.fft.rfft 计算离散傅里叶变换时,您实际上是在离散频率点对 DTFT 函数进行采样。您可以通过在同一张图上绘制不同长度的变换来看到这一点:

import numpy as np
import matplotlib.pyplot as plt

x = [0, 1, 0, 0, 4.3, 3, 599]

# Compute the DTFT at a sufficiently large number of points using the explicit formula
N = 2048
f = np.linspace(0, 0.5, N)
dtft = np.zeros(len(f), dtype=np.complex128)
for n in range(0,len(x)):
  dtft += x[n] * np.exp(-1j*2*np.pi*f*n)

# Compute the FFT without NFFT argument (NFFT defaults to the length of the input)
y1 = np.fft.rfft(x)
f1 = np.fft.rfftfreq(len(x))

# Compute the FFT with NFFT argument
N2 = 8
y2 = np.fft.rfft(x,N2)
f2 = np.fft.rfftfreq(N2)

# Plot results
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(f, np.abs(dtft), label='DTFT')
plt.plot(f1, np.abs(y1), 'C1x', label='FFT N=7')
plt.plot(f2, np.abs(y2), 'C2s', label='FFT N=8')
plt.title('Magnitude')
plt.legend(loc='upper right')

plt.subplot(2,1,2)
plt.plot(f, np.angle(dtft), label='DTFT')
plt.plot(f1, np.angle(y1), 'C1x', label='FFT N=7')
plt.plot(f2, np.angle(y2), 'C2s', label='FFT N=8')
plt.title('Phase')
plt.legend(loc='upper right')

plt.show()