信号与系统实验手册:LTI 系统分析

实验 1-3:位移、翻转与离散卷积

1 实验环境配置

首先配置 Python 绘图库,确保支持中文和高质量公式显示。

import numpy as np
import matplotlib.pyplot as plt

# --- 中文支持 ---
plt.rcParams['font.sans-serif'] = ['SimHei'] 
plt.rcParams['axes.unicode_minus'] = False     
%config InlineBackend.figure_format = 'retina' 

print("✅ 环境配置已就绪。")

2 实验 1:单位冲激信号与时间位移 (Shifting)

说明:单位冲激信号 \(\delta[n]\) 是系统的基础。当时延为 \(n_0\) 时,信号变为 \(\delta[n-n_0]\)。

关键步骤:通过布尔判断生成脉冲,并演示 \(x[n-3]\) 的物理位置。

n = np.arange(-5, 11)
x = (n == 0).astype(float)       # delta[n]
x_delayed = (n == 3).astype(float) # delta[n-3]

plt.figure(figsize=(10, 3))
plt.stem(n, x, linefmt='b-', markerfmt='bo', label='原始 $\delta[n]$')
plt.stem(n, x_delayed, linefmt='r--', markerfmt='rx', label='时延 $\delta[n-3]$')
plt.title("实验 1:信号的时间位移")
plt.xlabel("n"); plt.ylabel("幅度"); plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

3 实验 2:卷积的预备动作——翻转 (Folding)

说明:在卷积公式 \(y[n] = \sum x[k]h[n-k]\) 中,变量 \(-k\) 要求我们将响应 \(h[k]\) 关于纵轴进行镜像翻转。利用 Python 切片 [::-1] 快速实现 \(h[k] \to h[-k]\)。

关键步骤:利用 Python 切片 [::-1] 快速实现 \(h[k] \to h[-k]\)。

h = np.array([1, 2, 3, 0.5]) 
indices = np.arange(len(h))
h_flipped = h[::-1] 

plt.figure(figsize=(10, 3))
plt.subplot(1, 2, 1)
plt.stem(indices, h, linefmt='b-')
plt.title("原始响应 $h[k]$")

plt.subplot(1, 2, 2)
plt.stem(indices, h_flipped, 'r-')
plt.title("翻转序列 $h[-k]$")
plt.show()

4 实验 3:离散卷积的数值计算 (Convolution)

说明:离散线性时不变系统的输出 \(y[n]\) 是输入 \(x[n]\) 与冲激响应 \(h[n]\) 的卷积。

关键步骤:使用 np.convolve 计算 LTI 系统的响应,并验证输出长度。

x = np.array([1, 1, 1, 1, 1]) 
h = np.array([1.0, 0.8, 0.6, 0.4, 0.2])

y = np.convolve(x, h)
n_y = np.arange(len(y))

plt.figure(figsize=(10, 3))
plt.stem(n_y, y, linefmt='g-', markerfmt='go', label='$y[n] = x[n]*h[n]$')
plt.title("实验 3:离散卷积的完整结果")
plt.xlabel("n"); plt.ylabel("幅度"); plt.legend()
plt.show()

print(f"验证长度:输入({len(x)}) + 响应({len(h)}) - 1 = 输出({len(y)})")