python通信瀑布图代码
时间: 2025-06-30 21:21:28 浏览: 14
### Python 实现通信瀑布图
要绘制通信中的瀑布图,通常会涉及信号处理领域的内容。以下是基于 NumPy 和 Matplotlib 的示例代码来展示如何生成并可视化一个简单的通信瀑布图。
#### 示例代码
```python
import numpy as np
import matplotlib.pyplot as plt
# 参数设置
fs = 1000 # 采样频率 (Hz)
duration = 2 # 数据持续时间 (秒)
frequencies = [50, 150, 250] # 不同时间段的中心频率列表
time_segments = [(0, 0.7), (0.7, 1.4), (1.4, 2)] # 时间分段
t = np.linspace(0, duration, int(fs * duration)) # 时间向量
signal = np.zeros_like(t)
for freq, segment in zip(frequencies, time_segments):
idx = (t >= segment[0]) & (t < segment[1])
signal[idx] += np.sin(2 * np.pi * freq * t[idx])
# 计算短时傅里叶变换 (STFT)
window_size = 100 # 窗口大小
step_size = 50 # 步长
nfft = window_size
segments = []
for start in range(0, len(signal) - window_size, step_size):
segments.append(signal[start:start + window_size])
stft_result = np.fft.fft(segments, n=nfft, axis=1)
magnitude = np.abs(stft_result).T
# 绘制瀑布图
plt.figure(figsize=(8, 6))
plt.imshow(magnitude, aspect='auto', origin='lower', cmap='viridis',
extent=[0, duration, 0, fs / 2], interpolation='none')
plt.colorbar(label="Magnitude")
plt.title("Communication Waterfall Plot", fontsize=14)
plt.xlabel("Time (s)", fontsize=12)
plt.ylabel("Frequency (Hz)", fontsize=12)
plt.tight_layout()
plt.show()
```
上述代码通过生成不同时间段内的正弦波信号模拟了一个简单的时间序列数据集,并利用短时傅里叶变换(Short-Time Fourier Transform, STFT)计算其频谱分布[^1]。最终的结果被绘制成二维图像形式表示随时间和频率变化的能量分布情况。
#### 关键技术说明
- **NumPy**: 提供高效的数组运算支持。
- **Matplotlib**: 负责图形化显示结果。
- **STFT**: 将长时间记录分割成较短部分分别做 FFT 处理以便观察局部特性。
阅读全文
相关推荐



















