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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
| """ 60GHz FMCW雷达车内乘员检测信号处理
功能: 1. Range-Doppler图生成 2. CFAR目标检测 3. 生命体征估计 """
import numpy as np from scipy import signal from typing import Tuple, List, Dict import matplotlib.pyplot as plt
class FMCWRadarProcessor: """FMCW雷达信号处理器""" def __init__( self, fc: float = 60e9, bandwidth: float = 4e9, num_chirps: int = 128, num_samples: int = 256, chirp_duration: float = 50e-6, frame_rate: float = 50.0 ): self.fc = fc self.bandwidth = bandwidth self.num_chirps = num_chirps self.num_samples = num_samples self.chirp_duration = chirp_duration self.frame_rate = frame_rate self.c = 3e8 self.wavelength = self.c / self.fc self.range_resolution = self.c / (2 * self.bandwidth) self.velocity_resolution = self.wavelength / (2 * num_chirps * chirp_duration) print(f"雷达参数:") print(f" 波长: {self.wavelength * 1000:.2f} mm") print(f" 距离分辨率: {self.range_resolution * 100:.2f} cm") print(f" 速度分辨率: {self.velocity_resolution:.3f} m/s") def range_fft(self, adc_data: np.ndarray) -> np.ndarray: """ 距离维FFT Args: adc_data: [num_chirps, num_samples] ADC数据 Returns: range_profile: [num_chirps, num_samples] 距离剖面 """ range_profile = np.fft.fft(adc_data, axis=1) range_profile = np.abs(range_profile) ** 2 return range_profile def doppler_fft(self, range_profile: np.ndarray) -> np.ndarray: """ 速度维FFT Args: range_profile: [num_chirps, num_samples] 距离剖面 Returns: range_doppler: [num_doppler, num_range] Range-Doppler图 """ range_doppler = np.fft.fftshift( np.fft.fft(range_profile, axis=0), axes=0 ) range_doppler = np.abs(range_doppler) ** 2 return range_doppler def cfar_2d( self, range_doppler: np.ndarray, guard_cells: Tuple[int, int] = (4, 4), training_cells: Tuple[int, int] = (8, 8), pfa: float = 1e-3 ) -> np.ndarray: """ 2D CFAR检测 Args: range_doppler: [num_doppler, num_range] RD图 guard_cells: 保护单元 (doppler, range) training_cells: 训练单元 (doppler, range) pfa: 虚警概率 Returns: detections: 检测结果掩码 """ num_doppler, num_range = range_doppler.shape detections = np.zeros_like(range_doppler, dtype=bool) alpha = (pfa ** (-1 / (training_cells[0] * training_cells[1])) - 1) for i in range(training_cells[0], num_doppler - training_cells[0]): for j in range(training_cells[1], num_range - training_cells[1]): train_region = range_doppler[ i - training_cells[0]:i + training_cells[0] + 1, j - training_cells[1]:j + training_cells[1] + 1 ] guard_start_d = i - guard_cells[0] guard_end_d = i + guard_cells[0] + 1 guard_start_r = j - guard_cells[1] guard_end_r = j + guard_cells[1] + 1 train_region[ guard_start_d - (i - training_cells[0]):guard_end_d - (i - training_cells[0]), guard_start_r - (j - training_cells[1]):guard_end_r - (j - training_cells[1]) ] = 0 noise_level = np.mean(train_region[train_region > 0]) threshold = noise_level * alpha if range_doppler[i, j] > threshold: detections[i, j] = True return detections def estimate_vital_signs( self, range_doppler: np.ndarray, detections: np.ndarray, time_series: List[np.ndarray] ) -> Dict[str, float]: """ 生命体征估计 Args: range_doppler: 当前帧RD图 detections: 检测结果 time_series: 历史时间序列数据 Returns: vital_signs: 生命体征 {呼吸率, 心率, 运动幅度} """ target_bins = np.where(detections) if len(target_bins[0]) == 0: return {'breathing_rate': 0.0, 'heart_rate': 0.0, 'motion_amplitude': 0.0} max_idx = np.argmax(range_doppler[target_bins]) doppler_bin = target_bins[0][max_idx] range_bin = target_bins[1][max_idx] if len(time_series) > 0: recent_frames = time_series[-100:] phases = [] for frame in recent_frames: phase = np.angle(frame[doppler_bin, range_bin]) phases.append(phase) phases = np.array(phases) phases = np.unwrap(phases) fs = self.frame_rate nyq = fs / 2 b_breath, a_breath = signal.butter(4, [0.1/nyq, 0.5/nyq], btype='band') breathing_signal = signal.filtfilt(b_breath, a_breath, phases) breathing_fft = np.abs(np.fft.fft(breathing_signal)) freqs = np.fft.fftfreq(len(breathing_signal), 1/fs) valid_mask = (freqs > 0.1) & (freqs < 0.5) if np.any(valid_mask): breath_freq = freqs[valid_mask][np.argmax(breathing_fft[valid_mask])] breathing_rate = breath_freq * 60 else: breathing_rate = 0.0 b_heart, a_heart = signal.butter(4, [0.8/nyq, 2.0/nyq], btype='band') heart_signal = signal.filtfilt(b_heart, a_heart, phases) heart_fft = np.abs(np.fft.fft(heart_signal)) heart_valid = (freqs > 0.8) & (freqs < 2.0) if np.any(heart_valid): heart_freq = freqs[heart_valid][np.argmax(heart_fft[heart_valid])] heart_rate = heart_freq * 60 else: heart_rate = 0.0 motion_amplitude = np.std(phases) * self.wavelength / (4 * np.pi) return { 'breathing_rate': breathing_rate, 'heart_rate': heart_rate, 'motion_amplitude': motion_amplitude * 1000 } return {'breathing_rate': 0.0, 'heart_rate': 0.0, 'motion_amplitude': 0.0} def process_frame(self, adc_data: np.ndarray) -> Dict: """ 处理单帧数据 Args: adc_data: [num_chirps, num_samples] 原始ADC数据 Returns: result: 处理结果 """ range_profile = self.range_fft(adc_data) range_doppler = self.doppler_fft(range_profile) detections = self.cfar_2d(range_doppler) detected_points = np.where(detections) ranges = detected_points[1] * self.range_resolution velocities = (detected_points[0] - self.num_chirps/2) * self.velocity_resolution return { 'range_doppler': range_doppler, 'detections': detections, 'points': list(zip(ranges, velocities)), 'num_detections': len(ranges) }
class ChildPresenceDetector: """儿童存在检测系统""" def __init__(self): self.radar = FMCWRadarProcessor() self.time_series = [] self.detection_history = [] self.breathing_threshold = 0.1 self.motion_threshold = 1.0 def update(self, adc_data: np.ndarray) -> Dict: """ 更新检测 Args: adc_data: [num_chirps, num_samples] ADC数据 Returns: detection_result: 检测结果 """ result = self.radar.process_frame(adc_data) self.time_series.append(result['range_doppler']) if len(self.time_series) > 200: self.time_series.pop(0) vital_signs = self.radar.estimate_vital_signs( result['range_doppler'], result['detections'], self.time_series ) is_child_present = ( vital_signs['breathing_rate'] > self.breathing_threshold or vital_signs['motion_amplitude'] > self.motion_threshold ) detection_result = { 'is_child_present': is_child_present, 'num_detections': result['num_detections'], 'breathing_rate': vital_signs['breathing_rate'], 'heart_rate': vital_signs['heart_rate'], 'motion_amplitude': vital_signs['motion_amplitude'] } self.detection_history.append(detection_result) if len(self.detection_history) > 100: self.detection_history.pop(0) return detection_result def get_status(self) -> str: """获取系统状态""" if len(self.detection_history) < 10: return "WARMUP" recent = self.detection_history[-10:] child_detected_count = sum(1 for d in recent if d['is_child_present']) if child_detected_count >= 7: return "CHILD_PRESENT" elif child_detected_count >= 3: return "CHILD_POSSIBLY_PRESENT" else: return "NO_CHILD"
if __name__ == "__main__": processor = FMCWRadarProcessor( fc=60e9, bandwidth=4e9, num_chirps=128, num_samples=256 ) print("\n" + "=" * 60) print("模拟车内场景测试") print("=" * 60) num_chirps = 128 num_samples = 256 print("\n场景1: 空车辆") adc_empty = np.random.randn(num_chirps, num_samples) * 0.01 result_empty = processor.process_frame(adc_empty) print(f" 检测点数: {result_empty['num_detections']}") print("\n场景2: 后排有儿童") t = np.linspace(0, 0.1, num_samples) breathing_freq = 0.3 adc_child = np.random.randn(num_chirps, num_samples) * 0.01 for chirp_idx in range(num_chirps): phase_modulation = 0.5 * np.sin(2 * np.pi * breathing_freq * chirp_idx / 50) target_bin = 50 adc_child[chirp_idx, target_bin] += 0.1 * np.exp(1j * phase_modulation) result_child = processor.process_frame(adc_child) print(f" 检测点数: {result_child['num_detections']}") print("\n" + "=" * 60) print("儿童存在检测系统测试") print("=" * 60) cpd = ChildPresenceDetector() print("\n连续检测10帧...") for i in range(10): result = cpd.update(adc_child) print(f" 帧{i+1}: 状态={cpd.get_status()}, " f"呼吸={result['breathing_rate']:.1f}次/分, " f"运动={result['motion_amplitude']:.2f}mm") print(f"\n最终判断: {cpd.get_status()}")
|