artais 2023-04-12 20:18 采纳率: 63.2%
浏览 196
已结题

用Python动画描述采样定理


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image, ImageSequence

fig = plt.figure()        #建立一个画板
ax = fig.add_subplot(1,1,1)  
x = np.linspace(0,1,500)
xdata = np.linspace(0,1,21)
ydata = np.cos(80*np.pi*xdata)
sample, = ax.plot([],[],'o')

line1, = ax.plot(x,np.cos(80*np.pi*x),'r--',lw=0.5)
ax.grid()
ax.set_xlim(0, 1)

def run(data):
    xdata = np.linspace(0,1,data)
    ydata = np.cos(80*np.pi*xdata)
    if data<=81:
        sample.set_data(xdata,ydata)
        sample.set_color('r')
    else:
        sample.set_data(xdata,ydata)
        sample.set_color('k')
    return sample,

ani = animation.FuncAnimation(fig,run,frames=np.arange(41,162,2),blit=False,interval=600,repeat=True)
plt.show()

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

  • 写回答

3条回答 默认 最新

  • 「已注销」 2023-04-12 20:30
    关注

    引用new bing部分回答作答:
    采样定理是一种数学定理,它指出如果要从一个连续信号中获取样本,那么采样频率必须大于等于信号最高频率的两倍,这样才能准确地重建原始信号。这个定理由两个著名的科学家分别发现,因此也被称为香农-奈奎斯特采样定理。

    接下来,我们将用Python动画来演示采样定理的原理。代码如下:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    # 设置采样频率
    fs = 80
    # 设置信号最高频率
    fmax = 20
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    
    # 生成连续信号
    x = np.linspace(0, 1, 1000)
    y = np.sin(2 * np.pi * fmax * x)
    
    # 生成采样点
    n = np.arange(0, 1, 1 / fs)
    y_sample = np.sin(2 * np.pi * fmax * n)
    
    # 绘制连续信号和采样点
    ax.plot(x, y, 'b-', label='Original Signal')
    ax.stem(n, y_sample, linefmt='r-', markerfmt='ro', label='Sampled Signal')
    
    # 添加图例和标题
    ax.legend(loc='upper right')
    ax.set_title('Sampling Theorem')
    
    # 设置动画参数
    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)
    
        # 更新绘图
        ax.clear()
        ax.plot(x, y, 'b-', label='Original Signal')
        ax.stem(n, y_sample, linefmt='r-', markerfmt='ro', label='Sampled Signal')
        ax.legend(loc='upper right')
        ax.set_title('Sampling Theorem (fs={}, fmax={})'.format(fs, fmax))
    
    # 创建动画对象
    ani = animation.FuncAnimation(fig, update, frames=frames, interval=interval, repeat_delay=repeat_delay)
    
    # 显示动画
    plt.show()
    
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月12日
  • 创建了问题 4月12日