artais 2023-04-12 21:51 采纳率: 61.1%
浏览 158
已结题

用Python动画证明采样定理(奈奎斯特定理)

用Python编写程序,证明采样定理(香农采样定律、奈奎斯特采样定律)
请用动画描述这一定理。分为两种情况f s≥2f max和f s<2f max。
请对下边代码进行完善,使其符合要求,有动画效果


import numpy as np
import matplotlib.pyplot as plt

# 生成信号
Fmax = 20
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * Fmax * t)

# 采样
Fs1 = 20
Ts1 = 1 / Fs1
samples1 = np.sin(2 * np.pi * Fmax * np.arange(0, 1, Ts1))
Fs2 = 60
Ts2 = 1 / Fs2
samples2 = np.sin(2 * np.pi * Fmax * np.arange(0, 1, Ts2))
# 重建信号
reconstructed1 = np.zeros_like(t)
for i, s in enumerate(samples1):
    reconstructed1 += s * np.sinc(Fs1 * (t - i * Ts1))
reconstructed2 = np.zeros_like(t)
for i, s in enumerate(samples2):
    reconstructed2 += s * np.sinc(Fs2 * (t - i * Ts2))
# 绘图
fig, axs = plt.subplots(3, 2, figsize=(8, 6), sharex=True, sharey=False)
axs[0,0].plot(t, signal)
axs[0,0].set_title('Original Signal 1 ')
axs[1,0].stem(np.arange(0, 1, Ts1), samples1, use_line_collection=True)
axs[1,0].set_title('Samples 1 ')
axs[2,0].plot(t, reconstructed1)
axs[2,0].set_title('Reconstructed Signal 1 ')
axs[0,1].plot(t, signal)
axs[0,1].set_title('Original Signal 2 ')
axs[1,1].stem(np.arange(0, 1, Ts2), samples2, use_line_collection=True)
axs[1,1].set_title('Samples 2 ')
axs[2,1].plot(t, reconstructed2)
axs[2,1].set_title('Reconstructed Signal 2 ')
plt.tight_layout()
plt.show()
 
  • 写回答

3条回答 默认 最新

  • 「已注销」 2023-04-12 21:59
    关注

    运行结果如下:

    img

    代码如下:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    #设置采样频率
    fs = 80
    
    #设置信号最高频率
    fmax = 20
    
    #生成连续信号
    x = np.linspace(0, 1, 1000)
    y = np.sin(2 * np.pi * fmax * x)
    
    fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(12, 8))
    
    #初始化子图
    axs[0, 0].set_title('Original Signal (fs >= 2fmax)')
    axs[0, 1].set_title('Sampled Signal (fs >= 2fmax)')
    axs[0, 2].set_title('Reconstructed Signal (fs >= 2fmax)')
    axs[1, 0].set_title('Original Signal (fs < 2fmax)')
    axs[1, 1].set_title('Sampled Signal (fs < 2fmax)')
    axs[1, 2].set_title('Reconstructed Signal (fs < 2fmax)')
    
    lines = []
    for ax in axs.flatten():
        line, = ax.plot([], [])
        lines.append(line)
    
    #设置动画参数
    frames = np.arange(1, 6)
    interval = 2000
    repeat_delay = 5000
    
    def update(frame):
    
    #设置新的采样频率和最高频率
     global fs, fmax
     fs += 20
     fmax += 5
    # 重新生成采样点和连续信号
    n = np.arange(0, 1, 1 / fs)
    y_sample = np.sin(2 * np.pi * fmax * n)
    y = np.sin(2 * np.pi * fmax * x)
    
    # 计算重构信号
    t = np.linspace(0, 1, len(x))
    y_reconstruct = np.zeros_like(t)
    for i in range(len(n)):
        y_reconstruct += y_sample[i] * np.sinc(fs * (t - n[i]))
    
    # 更新绘图
    lines[0].set_data(x, y)
    lines[1].set_data(n, y_sample)
    lines[2].set_data(x, y_reconstruct)
    lines[3].set_data(x, y)
    lines[4].set_data(n, y_sample)
    lines[5].set_data(x, y_reconstruct)
    
    for ax in axs.flatten():
        ax.relim()
        ax.autoscale_view()
    # 创建动画对象
    ani = animation.FuncAnimation(fig, update, frames=frames, interval=interval, repeat_delay=repeat_delay)
    
    # 显示动画
    plt.show()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 创建了问题 4月12日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来