【发布时间】:2021-05-31 22:20:31
【问题描述】:
我正在使用 FFT 在我的博士论文中执行一些转换。由于我需要傅里叶变换在某些频率下,我想到了编写自己的 DFT(我不能使用 FFT,因为它的频率由样本数和速率固定)。
但是我遇到了两种算法的输出之间的差异。
这是一个最小的例子,它再现了 DFT 和 FFT 的计算,并输出了一些对比图:
from scipy.fft import fft, fftshift, fftfreq
import numpy as np
import matplotlib.pyplot as plt
#Setting gaussian signal to perform example
N = 200 #sample number
x = np.linspace (-100,100,N) #axis to perform fft into
delta = x[1]-x[0] #space between samples
sigma = 10 #numerical value
freqs = fftshift(fftfreq(N, d=delta)) #FFT output frequencies. Will also be used in DFT to contrast results
#Calculating signal
signal = np.exp(-x**2/sigma**2)
#Plot input signal, for reference
plt.figure(figsize = (8,5))
plt.plot(x, signal)
plt.title("Input signal")
plt.show()
#Perform fft
output_fft = fftshift(fft(signal))
#Perform DFT in same frequencies as FFT
output_DFT1 = []
for fx in freqs:
spec = np.sum(signal * np.exp(-1j*2*np.pi * fx * x))
output_DFT1.append(spec)
#Plotting comparative results
#Real part
plt.figure(figsize=(8,5))
plt.plot(freqs, np.real(output_fft), label='FFT')
plt.plot(freqs, np.real(output_DFT1), label='DFT1')
plt.legend(loc='upper right')
plt.title("Comparative on FFT and DFT real parts")
plt.show()
#Imaginary part
plt.figure(figsize=(8,5))
plt.plot(freqs, np.imag(output_fft), label='FFT')
plt.plot(freqs, np.imag(output_DFT1), label='DFT1')
plt.legend(loc='upper right')
plt.title("Comparative on FFT and DFT imag parts")
plt.show()
这是我的输出图像: Real part comparison Imaginary part comparison
为什么结果不同?
【问题讨论】:
标签: python numpy scipy fft dft