python数字.fft.rfft:为什么包括或不包括NFFT时,输出会有很大不同

2024-04-29 09:46:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着理解NFFT的含义数字.fft.rfft. 但是我搞不懂为什么当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])

Tags: fftnumpy数字array例子含义nfftrfft
1条回答
网友
1楼 · 发布于 2024-04-29 09:46:50

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()

enter image description here

相关问题 更多 >