python - 用python绘制高斯函数的傅里叶变换,但结果错误

标签 python fft gaussian

我想了很久,也没找出问题所在。希望您能帮助我,谢谢。

F(s) Gaussian function
F(s)=1/(√2π s) e^(-(w-μ)^2/(2s^2 ))

代码:

import numpy as np
from matplotlib import pyplot as plt
from math import pi
from scipy.fft import fft

def F_S(w, mu, sig):
    return (np.exp(-np.power(w-mu, 2)/(2 * np.power(sig, 2))))/(np.power(2*pi, 0.5)*sig)

w=np.linspace(-5,5,100)
plt.plot(w, np.real(np.fft.fft(F_S(w, 0, 1))))
plt.show()

结果:

enter image description here

最佳答案

正如之前提到的,您需要绝对值,而不是实部。 一个最小的示例,显示 re/im 、abs/phase 光谱。

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

n=1001                       # add 1 to keep the interval a round number when using linspace
t = np.linspace(-5, 5, n )   # presumed to be time
dt=t[1]-t[0]                 # time resolution
print(f'sampling every  {dt:.3f} sec , so at {1/dt:.1f} Sa/sec, max. freq will be {1/2/dt:.1f} Hz')


y = np.exp(-(t**2)/0.01)      # signal in time

fr= np.fft.fftshift(np.fft.fftfreq(n, dt))  # shift helps with sorting the frequencies for better plotting
ft=np.fft.fftshift(np.fft.fft(y))           # fftshift only necessary for plotting in sequence

p.figure(figsize=(20,12))
p.subplot(231)
p.plot(t,y,'.-')
p.xlabel('time (secs)')
p.title('signal in time')

p.subplot(232)
p.plot(fr,np.abs(ft), '.-',lw=0.3)      
p.xlabel('freq (Hz)')
p.title('spectrum, abs');

p.subplot(233)
p.plot(fr,np.real(ft), '.-',lw=0.3)     
p.xlabel('freq (Hz)')
p.title('spectrum, real');

p.subplot(235)
p.plot(fr,np.angle(ft), '.-', lw=0.3)    
p.xlabel('freq (Hz)')
p.title('spectrum, phase');

p.subplot(236)
p.plot(fr,np.imag(ft), '.-',lw=0.3)      
p.xlabel('freq (Hz)')
p.title('spectrum, imag');

enter image description here

关于python - 用python绘制高斯函数的傅里叶变换,但结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970938/

相关文章:

python - 如何在网页抓取时使用 FindAll

python "round robin"

android - Android中的音频分析

android - 在android中每1/4秒获取音频文件的频率

python - 选择数据集中属于多元高斯分布的点

python - PyQt QSql数据库 : QMYSQL driver not loaded

python - 使用 Azure Functions 中的 Python 根据 Azure Blob 存储中的模式匹配检查 Blob 是否存在

math - n维快速傅立叶变换的计算复杂性?

python - 数据立方体的高斯卷积

matlab - 在 MATLAB 中绘制给定 MVN 的多元正态 PDF 的轮廓?