1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| """ HYDROGEN CPD 完整检测流程 集成呼吸检测 + 波束成形 + 目标分类 """
import numpy as np from dataclasses import dataclass from enum import Enum from typing import List, Tuple, Optional
class TargetType(Enum): CHILD = "child" ADULT = "adult" PET = "pet" OBJECT = "object" UNKNOWN = "unknown"
@dataclass class DetectedTarget: """检测到的目标""" target_type: TargetType position: Tuple[float, float, float] breathing_rate: Optional[float] confidence: float class HYDROGEN_CPD: """HYDROGEN CPD 检测系统""" def __init__(self): self.breathing_detector = UWBBreathingDetector(fs=20.0) self.beamformer = DigitalBeamformer(n_tx=4, n_rx=4) self.cabin_range = (0.3, 2.5) def scan_cabin(self, adc_data: np.ndarray, range_bins: np.ndarray) -> List[DetectedTarget]: """ 扫描车舱,检测所有目标 Args: adc_data: 原始 ADC 数据 (n_chirps, n_range_bins, n_rx) range_bins: 距离bin对应的距离 (米) Returns: 检测到的目标列表 """ targets = [] n_chirps, n_range, n_rx = adc_data.shape theta_scan = np.linspace(-np.pi/2, np.pi/2, 91) for r_idx in range(n_range): distance = range_bins[r_idx] if distance < self.cabin_range[0] or distance > self.cabin_range[1]: continue rx_data = adc_data[:, r_idx, :] beamformed = self.beamformer.beamform(rx_data, theta_scan) energy = np.sum(np.abs(beamformed)**2, axis=0) if np.max(energy) < 1e-6: continue peak_idx = np.argmax(energy) target_theta = theta_scan[peak_idx] target_signal = beamformed[:, peak_idx] vital_signs = self.breathing_detector.extract_vital_signs( target_signal ) if vital_signs['breathing_confidence'] > 3.0: target_type = self.breathing_detector.classify_target(vital_signs) x = distance * np.sin(target_theta) y = distance * np.cos(target_theta) z = 0.0 target = DetectedTarget( target_type=TargetType(target_type), position=(x, y, z), breathing_rate=vital_signs['breathing_rate_bpm'], confidence=vital_signs['breathing_confidence'] ) targets.append(target) return targets def check_cpd_alert(self, targets: List[DetectedTarget], vehicle_locked: bool) -> Tuple[bool, str]: """ 检查是否需要 CPD 警报 Args: targets: 检测到的目标 vehicle_locked: 车辆是否已锁 Returns: (需要警报, 警报信息) """ if not vehicle_locked: return False, "" children = [t for t in targets if t.target_type == TargetType.CHILD] if children: child = children[0] alert_msg = ( f"检测到儿童遗留在车内!\n" f"位置: ({child.position[0]:.1f}, {child.position[1]:.1f}) m\n" f"呼吸频率: {child.breathing_rate:.0f} BPM\n" f"置信度: {child.confidence:.1f}" ) return True, alert_msg return False, ""
if __name__ == "__main__": cpd = HYDROGEN_CPD() n_chirps = 200 n_range = 100 n_rx = 4 adc_data = np.random.randn(n_chirps, n_range, n_rx) * 0.01 range_bins = np.linspace(0, 3, n_range) child_range_idx = 33 child_theta = np.pi / 6 t = np.arange(n_chirps) / 20.0 breath_signal = np.sin(2 * np.pi * 0.42 * t) steering = cpd.beamformer.compute_steering_vector(child_theta, 0) for i in range(n_rx): adc_data[:, child_range_idx, i] += 0.1 * breath_signal * steering[i].real targets = cpd.scan_cabin(adc_data, range_bins) print(f"检测到 {len(targets)} 个目标:") for t in targets: print(f" - 类型: {t.target_type.value}") print(f" 位置: {t.position}") print(f" 呼吸: {t.breathing_rate:.1f} BPM") print(f" 置信度: {t.confidence:.2f}") alert, msg = cpd.check_cpd_alert(targets, vehicle_locked=True) if alert: print(f"\n⚠️ CPD 警报:\n{msg}")
|