首页 > 解决方案 > python numpy.fft.rfft:为什么在包含或不包含 NFFT 时,输出有很大不同

问题描述

我试图理解 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])

标签: pythonnumpysignal-processingfft

解决方案


FFT 是离散傅里叶变换 (DFT)的有效实现,它是频率的离散函数。它还与离散时间傅里叶变换 (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()

在此处输入图像描述


推荐阅读