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
| import numpy as np from dataclasses import dataclass from typing import Optional from scipy import signal
@dataclass class CPDDetection: """儿童存在检测结果""" detected: bool confidence: float vital_signs: dict position: tuple motion_level: float
class MillimeterWaveCPD: """ 60GHz毫米波雷达CPD检测 硬件:TI IWR6843AOP / ARIA Hydrogen 核心原理: 1. 发射FMCW信号 2. 接收反射信号 3. 分离静态和动态分量 4. 从动态分量提取呼吸/心跳频率 """ def __init__( self, sample_rate: int = 100, fft_size: int = 2048, breath_freq_range: tuple = (0.1, 0.5), heart_freq_range: tuple = (1.0, 2.0) ): self.sample_rate = sample_rate self.fft_size = fft_size self.breath_freq_range = breath_freq_range self.heart_freq_range = heart_freq_range def process_radar_data( self, adc_data: np.ndarray ) -> CPDDetection: """ 处理雷达ADC数据 Args: adc_data: 雷达ADC采样数据 Returns: CPDDetection: 检测结果 """ range_doppler = self._compute_range_doppler(adc_data) range_doppler_clean = self._remove_static_clutter(range_doppler) motion_map = self._detect_motion(range_doppler_clean) vital_signs = self._extract_vital_signs(adc_data) detected = ( motion_map['total_motion'] > 0.1 or vital_signs['breath_rate'] > 0 ) return CPDDetection( detected=detected, confidence=self._compute_confidence(motion_map, vital_signs), vital_signs=vital_signs, position=self._locate_target(range_doppler_clean), motion_level=motion_map['total_motion'] ) def _compute_range_doppler(self, adc_data: np.ndarray) -> np.ndarray: """计算距离-多普勒图""" range_fft = np.fft.fft(adc_data, n=self.fft_size, axis=0) range_doppler = np.fft.fftshift( np.fft.fft(range_fft, axis=0), axes=0 ) return np.abs(range_doppler) def _remove_static_clutter(self, range_doppler: np.ndarray) -> np.ndarray: """静态杂波抑制(移除零多普勒分量)""" center = range_doppler.shape[0] // 2 range_doppler[center-5:center+5] *= 0.01 return range_doppler def _detect_motion(self, range_doppler: np.ndarray) -> dict: """检测运动目标""" total_energy = np.sum(range_doppler) motion_threshold = 0.05 * total_energy motion_map = (range_doppler > motion_threshold).astype(float) total_motion = np.sum(motion_map) / range_doppler.size return { 'motion_map': motion_map, 'total_motion': total_motion } def _extract_vital_signs(self, adc_data: np.ndarray) -> dict: """ 提取生命体征(呼吸率、心率) 原理:胸腔微动调制雷达回波相位 - 呼吸:胸腔起伏约1-5mm,频率0.1-0.5Hz - 心跳:胸壁微动约0.2-0.5mm,频率1-2Hz """ phase = np.angle(adc_data[:, 0]) phase_unwrapped = np.unwrap(phase) phase_detrended = signal.detrend(phase_unwrapped) freq = np.fft.fftfreq(len(phase_detrended), 1/self.sample_rate) spectrum = np.abs(np.fft.fft(phase_detrended)) pos_freq = freq[:len(freq)//2] pos_spectrum = spectrum[:len(spectrum)//2] breath_mask = (pos_freq >= self.breath_freq_range[0]) & \ (pos_freq <= self.breath_freq_range[1]) if np.any(breath_mask): breath_peak_idx = np.argmax(pos_spectrum[breath_mask]) breath_freq = pos_freq[breath_mask][breath_peak_idx] breath_rate = breath_freq * 60 else: breath_rate = 0 heart_mask = (pos_freq >= self.heart_freq_range[0]) & \ (pos_freq <= self.heart_freq_range[1]) if np.any(heart_mask): heart_peak_idx = np.argmax(pos_spectrum[heart_mask]) heart_freq = pos_freq[heart_mask][heart_peak_idx] heart_rate = heart_freq * 60 else: heart_rate = 0 return { 'breath_rate': breath_rate, 'heart_rate': heart_rate, 'phase_signal': phase_detrended } def _compute_confidence(self, motion_map: dict, vital_signs: dict) -> float: """计算检测置信度""" motion_score = min(motion_map['total_motion'] / 0.5, 1.0) breath_score = 1.0 if 10 < vital_signs['breath_rate'] < 40 else 0.5 heart_score = 1.0 if 60 < vital_signs['heart_rate'] < 160 else 0.5 confidence = 0.3 * motion_score + 0.4 * breath_score + 0.3 * heart_score return confidence def _locate_target(self, range_doppler: np.ndarray) -> tuple: """定位目标位置""" max_idx = np.unravel_index(np.argmax(range_doppler), range_doppler.shape) row = max_idx[0] // (range_doppler.shape[0] // 3) seat = max_idx[1] // (range_doppler.shape[1] // 3) return (row, seat)
if __name__ == "__main__": cpd = MillimeterWaveCPD(sample_rate=100, fft_size=2048) t = np.linspace(0, 5, 500) breath_signal = 0.5 * np.sin(2 * np.pi * 0.25 * t) heart_signal = 0.1 * np.sin(2 * np.pi * 1.2 * t) noise = 0.05 * np.random.randn(len(t)) phase = breath_signal + heart_signal + noise adc_data = np.exp(1j * 2 * np.pi * phase).reshape(-1, 1) result = cpd.process_radar_data(adc_data) print("=" * 60) print("CPD检测结果") print("=" * 60) print(f"检测到儿童: {result.detected}") print(f"置信度: {result.confidence:.2f}") print(f"呼吸率: {result.vital_signs['breath_rate']:.1f} 次/分钟") print(f"心率: {result.vital_signs['heart_rate']:.1f} 次/分钟") print(f"运动水平: {result.motion_level:.3f}") print(f"位置: 第{result.position[0]+1}排, 第{result.position[1]+1}座")
|