You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python实现高斯函数离散傅里叶变换及绘图对比求助

高斯函数离散傅里叶变换与理论结果对比

实现步骤说明

  • 利用NumPy的fft模块完成离散傅里叶变换,通过频率轴转换和缩放匹配连续傅里叶变换的尺度
  • 计算理论傅里叶变换结果,将DFT结果与理论曲线对齐后可视化对比
  • 优化原有代码,使用NumPy向量运算替代循环提升效率

完整示例代码

import numpy as np
import matplotlib.pyplot as plt

# 创建[-50, 50]区间,步长dt=1e-3
dt = 1e-3
interval = np.linspace(-50, 50, int(100 / dt) + 1)  # 共100001个采样点

# 定义高斯函数
def Gaussian(t, t_H):
    return (1 / (np.sqrt(np.pi) * t_H)) * np.exp(-(t / t_H) ** 2)

# 计算两个t_H对应的高斯函数值
tH_5 = Gaussian(interval, 5)
tH_15 = Gaussian(interval, 15)

# ---------------------- 离散傅里叶变换处理 ----------------------
def compute_dft_and_scale(signal, dt):
    # 计算DFT
    fft_signal = np.fft.fft(signal)
    # 获取周期频率轴(单位:1/时间)
    freq = np.fft.fftfreq(len(signal), d=dt)
    # 转换为角频率w = 2πf,匹配理论公式变量
    w = 2 * np.pi * freq
    # 移位使频率轴以0为中心,方便观察对称结果
    w_shifted = np.fft.fftshift(w)
    fft_shifted = np.fft.fftshift(fft_signal)
    # 乘以采样步长dt,将DFT结果缩放至近似连续傅里叶变换
    magnitude = np.abs(fft_shifted) * dt
    return w_shifted, magnitude

# 处理t_H=5和t_H=15的信号
w_5, dft_mag_5 = compute_dft_and_scale(tH_5, dt)
w_15, dft_mag_15 = compute_dft_and_scale(tH_15, dt)

# ---------------------- 计算理论傅里叶变换结果 ----------------------
theo_g_5 = np.exp(-(w_5 ** 2 * 5 ** 2) / 4)
theo_g_15 = np.exp(-(w_15 ** 2 * 15 ** 2) / 4)

# ---------------------- 绘制对比图像 ----------------------
plt.figure(figsize=(12, 6))

# 子图1:t_H=5的变换对比
plt.subplot(1, 2, 1)
plt.plot(w_5, dft_mag_5, label='DFT结果', alpha=0.7)
plt.plot(w_5, theo_g_5, label='理论结果', linestyle='--', linewidth=2)
plt.xlabel('角频率w')
plt.ylabel('|G(w)|')
plt.title('t_H=5时高斯函数的傅里叶变换对比')
plt.legend()
plt.xlim(-1, 1)  # 聚焦变换的主要分布区间

# 子图2:t_H=15的变换对比
plt.subplot(1, 2, 2)
plt.plot(w_15, dft_mag_15, label='DFT结果', alpha=0.7)
plt.plot(w_15, theo_g_15, label='理论结果', linestyle='--', linewidth=2)
plt.xlabel('角频率w')
plt.ylabel('|G(w)|')
plt.title('t_H=15时高斯函数的傅里叶变换对比')
plt.legend()
plt.xlim(-0.3, 0.3)  # t_H越大,变换结果越窄,缩小x轴范围观察

plt.tight_layout()
plt.show()

关键细节说明

  • DFT缩放:离散傅里叶变换本质是求和运算,乘以采样步长dt才能近似连续傅里叶变换的积分结果
  • 频率轴转换:将fftfreq得到的周期频率转换为角频率w=2πf,与理论公式的变量保持一致
  • 轴移位:fftshift将频率轴从[0, f_max)调整为(-f_max/2, f_max/2],便于观察高斯函数傅里叶变换的对称性
  • 可视化范围:根据t_H大小调整x轴范围,因为t_H越大,原高斯函数越宽,其傅里叶变换越集中在0附近

内容的提问来源于stack exchange,提问作者LianNuo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 17:03:40