python - 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])
解决方案
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()
推荐阅读
- javascript - 是否可以在浏览器中使用 JavaScript 发出 HTTP CONNECT 请求?
- python - python vs源代码字符串不能包含空字节
- html - 'HTMLInputElement' 类型上不存在属性'touched'
- python-3.x - 如何使用 aiohttp 在服务器端存储 websocket?
- c# - 如何接收 AdaptiveCard 值?
- javascript - ReactJS:为什么一个状态的值不同?
- mule - 如何将 Netsuite 的 UPDATE API 修复到 Salesforce Flow?
- neo4j - 如何获取被推荐用户的角色
- python - 在子图的空白处插入文本的方法
- azure - Azure 数据工厂和 Databricks 之间的连接