[color=rgba(0, 0, 0, 0.9)]以下是一个使用Python模拟雷达/声纳基本工作原理的示例代码,结合了信号发射、目标反射和回波处理的核心算法: import numpy as npimport matplotlib.pyplot as pltfrom scipy.signal import chirp, correlate def radar_simulation(): # ======== 参数设置 ======== c = 3e8 # 光速 (m/s) - 声纳需改为声速(1500m/s) fs = 2e6 # 采样率 (2MHz) duration = 10e-6 # 信号持续时间 (10μs) bandwidth = 500e3 # 扫频带宽 (500kHz) target_range = 1200 # 目标距离 (m) target_rcs = 5.0 # 目标雷达散射截面积 # ======== 生成发射信号(线性调频脉冲) ======== t = np.arange(0, duration, 1/fs) tx_signal = chirp(t, f0=0, t1=duration, f1=bandwidth, phi=-90) # ======== 计算时间延迟 ======== time_delay = 2 * target_range / c # 往返时间 delay_samples = int(time_delay * fs) # ======== 生成接收信号 ======== # 添加延迟和衰减 rx_signal = np.zeros_like(tx_signal) rx_signal[delay_samples:delay_samples+len(tx_signal)] = tx_signal[:-delay_samples] * target_rcs # 添加高斯白噪声 noise = 0.1 * np.random.randn(len(tx_signal)) rx_signal += noise # ======== 信号处理(脉冲压缩) ======== # 匹配滤波器(发射信号的共轭翻转) filter_kernel = np.conj(tx_signal[::-1]) compressed = correlate(rx_signal, filter_kernel, mode='same') compressed = np.abs(compressed) # 取幅度 # ======== 距离计算 ======== peak_index = np.argmax(compressed) calculated_range = (peak_index / fs) * c / 2 # ======== 可视化结果 ======== plt.figure(figsize=(12, 10)) plt.subplot(3, 1, 1) plt.plot(t*1e6, tx_signal) plt.title(' 发射信号 (Chirp脉冲)') plt.xlabel(' 时间 (μs)') plt.ylabel(' 幅度') plt.grid(True) plt.subplot(3, 1, 2) plt.plot(t*1e6, rx_signal) plt.title(' 接收信号 (含噪声和延迟)') plt.xlabel(' 时间 (μs)') plt.ylabel(' 幅度') plt.grid(True) plt.subplot(3, 1, 3) range_axis = np.linspace(0, len(compressed)*c/(2*fs), len(compressed)) plt.plot(range_axis, compressed) plt.plot(calculated_range, np.max(compressed), 'ro') plt.annotate(f' 检测目标: {calculated_range:.1f}m', xy=(calculated_range, np.max(compressed)), xytext=(calculated_range+200, 0.8*np.max(compressed)), arrowprops=dict(facecolor='red', shrink=0.05)) plt.title(' 脉冲压缩结果 (距离门)') plt.xlabel(' 距离 (m)') plt.ylabel(' 相关值') plt.grid(True) plt.tight_layout() plt.show() if __name__ == "__main__": radar_simulation()
算法说明:信号生成: - 使用线性调频(Chirp)信号作为发射波形
- 频率从0Hz扫到500kHz(带宽越大,距离分辨率越高)
目标反射模型: - 根据目标距离计算信号往返延迟
- 添加雷达散射截面积(RCS)模拟目标反射特性
- 添加高斯白噪声模拟环境噪声
核心处理(脉冲压缩): # 匹配滤波器实现 filter_kernel = np.conj(tx_signal[::-1]) # 发射信号的共轭时间反转 compressed = correlate(rx_signal, filter_kernel, mode='same')距离检测: - 在压缩信号中找到峰值位置
- 根据时延计算目标距离:距离 = (时间延迟 × 光速) / 2
扩展功能:多目标检测: # 在压缩信号中查找多个峰值peaks, _ = find_peaks(compressed, height=threshold)for peak in peaks: range = (peak / fs) * c / 2多普勒测速: # 需要多个脉冲的相位变化 phase_shift = np.angle(compressed[peak_index]) velocity = (phase_shift * c) / (4 * np.pi * pulse_repetition_frequency)声纳适配: c = 1500 # 声速 (m/s)# 添加海洋环境噪声模型
典型应用场景:- 雷达系统:自动驾驶(毫米波雷达)、气象雷达
- 声纳系统:潜艇探测、鱼群探测
- 医学成像:超声波检查
- 地质勘探:地震波探测
[color=rgba(0, 0, 0, 0.9)]此代码展示了雷达/声纳最基本的"发射-接收-处理"流程,实际系统还需考虑: - 多径干扰消除
- 杂波抑制(CFAR检测)
- 运动补偿
- 阵列信号处理(波束形成)
[color=rgba(0, 0, 0, 0.9)]可根据具体需求扩展此基础框架,例如添加目标运动模型、环境衰减模型或多传感器融合算法。 |