Reputation: 1109
I am trying to understand to the meaning of NFFT in numpy.fft.rfft. But I get confused why when NFFT included or not, the outputs get very different. Please see the example below.
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])
Upvotes: 0
Views: 2025
Reputation: 14577
The FFT is an efficient implementation of the Discrete Fourier Transform (DFT), which is a discrete function of frequency. It is also related to the Discrete-Time Fourier Transform (DTFT), itself a continuous function of frequency. More specifically, the DFT corresponds exactly to the DTFT evaluated at the discrete frequencies of the DFT.
In other words, when computing a Discrete Fourier Transform with numpy.fft.rfft
, you are essentially sampling the DTFT function at discrete frequency points. You can see this by plotting transforms of different lengths on the same graph with the following:
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()
Upvotes: 2