gpt4 book ai didi

python - 与 Mathematica 中的 FFT 相比,Python 中复杂信号的 FFT 产生 "flipped"频谱

转载 作者:行者123 更新时间:2023-12-03 21:05:14 26 4
gpt4 key购买 nike

我有一个复杂的信号,我想对其进行 FFT。使用 Mathematica 我得到以下结果:

(* Some acquisition params *)
fS = 100. 10^6;
time = 10. 10^-6;
NbrSamp = Round[fS * time];

(* Generate signal *)
w0 = 2 \[Pi] 80 10^6;
ti = Subdivide[0., time, NbrSamp];
sig = Cos[w0 ti] + \[ImaginaryI] Sin[w0 ti];

(* Plot result of FFT *)
ListPlot[Abs@Fourier[sig], Joined -> True, PlotRange -> All, DataRange -> {0, fS}]
figure of obtained spectra
但是,在 Python 中执行相同的计算时,与 Mathematica 相比,我得到了“翻转”的光谱。为了获得相同的频谱,我必须翻转 FFT 的输出。我的代码如下:
import numpy as np
import matplotlib.pylab as plt
from scipy.fft import fft

# Some acquisition params
fs = 100e6
time = 10e-6
NbrSamp = round(fs*time)

# Generate signal
w0 = 2*np.pi*80e6
ti = np.arange(NbrSamp+1) / fs
sig = np.cos(w0*ti) + 1j*np.sin(w0*ti)

# calc FFT
sigFFT = fft(sig, norm = 'ortho')
freq = np.arange(0, NbrSamp+1) * fs / NbrSamp

# Plot result of FFT
plt.plot(freq, abs(sigFFT), label = "Actual result")
plt.plot(freq, abs(sigFFT[::-1]), label = "Flipped result")
plt.xlim(min(freq), max(freq))
plt.legend()
plt.show()
Results from Python FFT
我的问题是为什么会这样,以及如何使用 Python 获得与 Mathematica 相同的结果,而不必翻转从 FFT 获得的数据?
我将有大量这样的信号,因此我想避免这种翻转操作。

最佳答案

来自 documentation to Mathematica's Fourier function :

  • Other definitions are used in some scientific and technical fields.
  • Different choices of definitions can be specified using the option FourierParameters.
  • With the setting FourierParameters->{a,b}, the discrete Fourier transform computed by Fourier is <some equation with exp(2πib) in it>.
  • Some common choices for {a,b} are {0,1} (default), {-1,1} (data analysis), {1,-1} (signal processing).
  • The setting b = -1 effectively corresponds to conjugating both input and output lists.

因此,默认情况下,Mathematica 设置 b = 0 , 但设置 b = -1使其输出匹配 Python 的,后者使用更常见的信号处理定义。
要让 Python 匹配 Mathematica 的结果,请执行
sigFFT = np.conj(fft(np.conj(sig), norm = 'ortho'))
请注意,此操作与翻转结果不同,因为零频率分量不受影响(输出数组的第一个元素),仅翻转其余元素(也共轭)。
另请注意,如果您只需要 FFT 的幅度,则不必对输出进行共轭,如 abs(conj(x)) == abs(x) .

关于python - 与 Mathematica 中的 FFT 相比,Python 中复杂信号的 FFT 产生 "flipped"频谱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67575730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com