# 导入科学计算与绘图库 import numpy as np import matplotlib.pyplot as plt # ===================== 1. 连续信号定义 ===================== # 信号时间范围:0 ≤ t ≤ 5 t_start, t_end = 0, 5 # 生成高密度时间轴,用于绘制连续参考曲线 t_continuous = np.linspace(t_start, t_end, 1000) # 阻尼正弦振荡信号:x(t)=5e^(-t)cos(2πt) def damping_signal(t): return 5 * np.exp(-t) * np.cos(2 * np.pi * t) # 计算连续信号的幅值 x_continuous = damping_signal(t_continuous) # ===================== 2. 采样间隔设置 ===================== Ts_list = [0.01, 0.02, 0.04, 0.08, 0.1, 0.25, 0.5] # ===================== 3. 多子图可视化 ===================== plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文显示 plt.rcParams['axes.unicode_minus'] = False # 负号显示 plt.figure(figsize=(14, 18)) # 遍历所有采样间隔 for i, Ts in enumerate(Ts_list): # 生成离散时间点:0, Ts, 2Ts, ..., ≤5 t_discrete = np.arange(t_start, t_end + Ts, Ts) # 离散采样 x_discrete = damping_signal(t_discrete) # 创建子图 plt.subplot(7, 1, i+1) # 绘制灰色连续信号(参考) plt.plot(t_continuous, x_continuous, 'gray', linewidth=1, label='连续信号') # 绘制离散曲线:*标记点 + 蓝色连线 plt.plot(t_discrete, x_discrete, 'b*-', markersize=7, linewidth=1, label=f'离散信号 Ts={Ts}s') plt.title(f'采样间隔 $T_s={Ts}$ s', fontsize=12) plt.ylabel('信号幅值 x(t)') plt.grid(True, alpha=0.3) plt.legend(loc='upper right') # 统一X轴标签 plt.xlabel('时间 t (s)', fontsize=12) plt.tight_layout() # 自动调整子图间距 plt.show()
Note.ms
/Xrain