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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| import numpy as np from scipy import signal from scipy.fft import fft, fftfreq
class CPDRadarProcessor: """ 儿童存在检测雷达信号处理器 处理流程: 1. Range-FFT → 距离维 2. Doppler-FFT → 速度维 3. CFAR检测 → 目标提取 4. 生命体征提取 → 呼吸/心跳 5. 存在判定 """ def __init__(self, config: dict): self.config = config self.frame_buffer = [] self.cfar_guard_cells = 4 self.cfar_training_cells = 8 self.cfar_threshold = 10 def process_frame(self, adc_data: np.ndarray) -> dict: """ 处理单帧雷达数据 Args: adc_data: ADC数据 (chirps, samples, rx_antennas) Returns: result: 检测结果 """ range_fft = fft(adc_data, axis=1) range_profile = np.abs(range_fft)[:, :self.config['samples_per_chirp']//2, :] range_doppler = fft(range_profile, axis=0) rd_map = np.abs(range_doppler) detections = self.cfar_2d(rd_map) vital_signs = self.extract_vital_signs(adc_data, detections) is_child_present = self.check_presence(vital_signs) return { 'rd_map': rd_map, 'detections': detections, 'vital_signs': vital_signs, 'is_child_present': is_child_present } def cfar_2d(self, rd_map: np.ndarray) -> list: """ 2D CFAR目标检测 Args: rd_map: Range-Doppler图 (doppler_bins, range_bins, antennas) Returns: detections: 检测到的目标列表 """ rd_avg = np.mean(rd_map, axis=2) detections = [] for d in range(self.cfar_guard_cells + self.cfar_training_cells, rd_avg.shape[0] - self.cfar_guard_cells - self.cfar_training_cells): for r in range(self.cfar_guard_cells + self.cfar_training_cells, rd_avg.shape[1] - self.cfar_guard_cells - self.cfar_training_cells): training_region = rd_avg[ d - self.cfar_training_cells - self.cfar_guard_cells : d + self.cfar_training_cells + self.cfar_guard_cells + 1, r - self.cfar_training_cells - self.cfar_guard_cells : r + self.cfar_training_cells + self.cfar_guard_cells + 1 ] noise_level = np.mean(training_region) + 1e-10 threshold = noise_level * (10 ** (self.cfar_threshold / 10)) if rd_avg[d, r] > threshold: detections.append({ 'doppler_idx': d, 'range_idx': r, 'intensity': rd_avg[d, r], 'range_m': r * self.config['range_resolution'], 'velocity_ms': (d - rd_avg.shape[0]//2) * self.config['velocity_resolution'] }) return detections def extract_vital_signs(self, adc_data: np.ndarray, detections: list) -> dict: """ 提取生命体征 Args: adc_data: 原始ADC数据 detections: CFAR检测结果 Returns: vital_signs: 生命体征数据 """ if not detections: return { 'respiration_rate': 0.0, 'heart_rate': 0.0, 'confidence': 0.0 } nearest = min(detections, key=lambda x: x['range_m']) range_idx = nearest['range_idx'] phase_sequence = np.angle(fft(adc_data, axis=1)[:, range_idx, 0]) unwrapped = np.unwrap(phase_sequence) detrended = signal.detrend(unwrapped) fs = self.config['frame_rate'] resp_band = [0.1, 0.5] resp_filtered = self.bandpass_filter(detrended, resp_band[0], resp_band[1], fs) heart_band = [0.8, 2.0] heart_filtered = self.bandpass_filter(detrended, heart_band[0], heart_band[1], fs) resp_freq = self.find_dominant_frequency(resp_filtered, fs) heart_freq = self.find_dominant_frequency(heart_filtered, fs) respiration_rate = resp_freq * 60 heart_rate = heart_freq * 60 confidence = min(nearest['intensity'] / 1000, 1.0) return { 'respiration_rate': respiration_rate, 'heart_rate': heart_rate, 'confidence': confidence, 'phase_signal': detrended } def bandpass_filter(self, signal_data: np.ndarray, low: float, high: float, fs: float) -> np.ndarray: """带通滤波""" nyquist = fs / 2 b, a = signal.butter(4, [low/nyquist, high/nyquist], btype='band') return signal.filtfilt(b, a, signal_data) def find_dominant_frequency(self, signal_data: np.ndarray, fs: float) -> float: """找到主频""" fft_result = fft(signal_data) freqs = fftfreq(len(signal_data), 1/fs) positive_freqs = freqs[:len(freqs)//2] magnitude = np.abs(fft_result[:len(freqs)//2]) peak_idx = np.argmax(magnitude) return positive_freqs[peak_idx] def check_presence(self, vital_signs: dict) -> bool: """ 判定是否有儿童存在 Args: vital_signs: 生命体征数据 Returns: is_present: 是否存在 """ resp_valid = 6 < vital_signs['respiration_rate'] < 60 heart_valid = 40 < vital_signs['heart_rate'] < 180 conf_valid = vital_signs['confidence'] > 0.3 return resp_valid and heart_valid and conf_valid
class CPDSystem: """ 儿童存在检测完整系统 """ def __init__(self, radar_config: dict, install_config: dict): self.processor = CPDRadarProcessor(radar_config) self.install_config = install_config self.history = [] self.max_history = 30 self.alarm_triggered = False self.alarm_threshold = 5 def update(self, adc_data: np.ndarray) -> dict: """ 更新检测状态 Args: adc_data: ADC数据 Returns: result: 检测结果 """ result = self.processor.process_frame(adc_data) self.history.append(result['is_child_present']) if len(self.history) > self.max_history: self.history.pop(0) consecutive_detections = self.count_consecutive_true(self.history) if consecutive_detections >= self.alarm_threshold and not self.alarm_triggered: self.trigger_alarm() result['alarm_triggered'] = self.alarm_triggered result['consecutive_detections'] = consecutive_detections return result def count_consecutive_true(self, history: list) -> int: """计算连续True的数量""" count = 0 for val in reversed(history): if val: count += 1 else: break return count def trigger_alarm(self): """触发报警""" self.alarm_triggered = True print("⚠️ 儿童存在检测报警!请立即检查车辆!")
if __name__ == "__main__": radar_config = { 'device': 'TI IWR6843AOP', 'frequency': '60-64 GHz', 'bandwidth': 7e9, 'chirps_per_frame': 64, 'samples_per_chirp': 256, 'frame_rate': 30, 'tx_antennas': 3, 'rx_antennas': 4, 'virtual_antennas': 12, 'range_resolution': 0.02, 'velocity_resolution': 0.1, 'angular_resolution': 15, } install_config = { 'position': '车顶中央', 'height': '1.8m', 'coverage': '前后排座椅', 'fov_azimuth': 120, 'fov_elevation': 60, } cpd = CPDSystem(radar_config, install_config) adc_data = np.random.randn(64, 256, 4) * 0.1 + 1.0 result = cpd.update(adc_data) print(f"呼吸频率: {result['vital_signs']['respiration_rate']:.1f} BPM") print(f"心率: {result['vital_signs']['heart_rate']:.1f} BPM") print(f"儿童存在: {'是' if result['is_child_present'] else '否'}") print(f"连续检测: {result['consecutive_detections']} 帧")
|