【问题标题】:python numpy.fft.rfft: why is when NFFT included or not, outputs are very differentpython numpy.fft.rfft:为什么在包含或不包含 NFFT 时,输出有很大不同
【发布时间】:2019-07-18 21:47:16
【问题描述】:

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

【问题讨论】:

  • 列表[0, 1, 0, 0, 4.3, 3, 599] 包含7 项。当numpy.fft.rfft([0, 1, 0, 0, 4.3, 3, 599], 8) 被调用时,数组用零填充,如numpy.fft.rfft 的文档中所详述。因此,第一个 DFT 是数组 [0, 1, 0, 0, 4.3, 3, 599,0] 的 DFT:它的长度为 (n/2)+1=5,因为 n=8。第二个 DFT 的长度为 .(n+1)/2=4,因为 n=7。
  • 谢谢我现在知道了!

标签: python numpy signal-processing fft


【解决方案1】:

FFT 是Discrete Fourier Transform (DFT) 的有效实现,它是频率的离散函数。它还与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()

【讨论】:

  • 谢谢!我有一个关于 rfftfreq 的后续问题(我是信号处理的新手),你怎么知道在上面的例子中频率范围是 0 - 0.5(假设采样率为 1?)?如果我将采样率设置为 16000 Hz,那么对应的 rfft (np.fft.rfftfreq(N2, d = 1.0/16000)) 应该是多少??
  • 完整的频谱将覆盖-0.5*sampling_rate,+0.5*sampling_rate 的范围。对于实值信号,FFT 是对称的。 rfft 利用了这种对称性,它只返回频谱的非冗余一半,因此覆盖了 0,0.5*sampling_rate(这是 rfftfreq 返回的内容)。当未指定采样率时,rfftfreq 假定采样率为 1。要指定不同的采样率,您必须按照正确说明指定 d=1/sampling_rate
  • rfft如何推断采样率?函数调用中好像没有指定采样率的地方?
  • 你的意思是函数 numpy.fft.rfft(a, n=None, axis=-1, norm=None)[source] 推断采样率?但是fft.rfft中没有输入参数来指定采样率..这就是我感到困惑的地方...fftfreq是一个不同的函数,您可以在其中采样率..我也很困惑numpy.fft.rfft和numpy。 fft.rfftfreq 是相关的/链接的
  • 对不起,我以为你在谈论rfftfreqrfft 不推断采样率,因为 FFT 的定义不依赖于采样率,而是提供归一化等距频率空间中频率的结果。 rfftfreq 用于从 rfft 结果中获取每个 bin 的相应频率。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
相关资源
最近更新 更多