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
| import numpy as np from scipy import signal from dataclasses import dataclass from typing import List, Tuple, Optional
@dataclass class CPDTarget: """CPD检测目标""" range_idx: int doppler_idx: int snr: float breath_rate: float heart_rate: float confidence: float position: Tuple[float, float, float]
class IWR6843CPD: """ TI IWR6843 CPD处理器 完整处理流程: 1. ADC数据采集 2. 距离-多普勒处理 3. 静态目标检测 4. 生命体征提取 5. 儿童存在判断 """ def __init__( self, num_tx: int = 3, num_rx: int = 4, num_chirps: int = 128, num_samples: int = 256, sample_rate: float = 10e6, chirp_rate: float = 50e12, start_freq: float = 60e9 ): self.num_tx = num_tx self.num_rx = num_rx self.num_chirps = num_chirps self.num_samples = num_samples self.sample_rate = sample_rate self.chirp_rate = chirp_rate self.start_freq = start_freq self.range_resolution = self._compute_range_resolution() self.velocity_resolution = self._compute_velocity_resolution() print(f"距离分辨率: {self.range_resolution * 100:.2f} cm") print(f"速度分辨率: {self.velocity_resolution * 100:.2f} cm/s") def _compute_range_resolution(self) -> float: """计算距离分辨率""" bandwidth = self.chirp_rate * self.num_samples / self.sample_rate return 3e8 / (2 * bandwidth) def _compute_velocity_resolution(self) -> float: """计算速度分辨率""" wavelength = 3e8 / self.start_freq chirp_period = 100e-6 return wavelength / (2 * self.num_chirps * chirp_period) def process_frame(self, adc_data: np.ndarray) -> List[CPDTarget]: """ 处理单帧ADC数据 Args: adc_data: (num_chirps, num_samples, num_rx) 复数ADC数据 Returns: List[CPDTarget]: 检测到的目标列表 """ range_fft = self._range_fft(adc_data) range_doppler = self._doppler_fft(range_fft) static_targets = self._detect_static_targets(range_doppler) targets = [] for target in static_targets: vital_signs = self._extract_vital_signs(adc_data, target['range_idx']) cpd_target = CPDTarget( range_idx=target['range_idx'], doppler_idx=target['doppler_idx'], snr=target['snr'], breath_rate=vital_signs['breath_rate'], heart_rate=vital_signs['heart_rate'], confidence=vital_signs['confidence'], position=self._compute_position(target['range_idx']) ) targets.append(cpd_target) return targets def _range_fft(self, adc_data: np.ndarray) -> np.ndarray: """距离FFT""" range_fft = np.fft.fft(adc_data, n=self.num_samples, axis=1) return range_fft def _doppler_fft(self, range_fft: np.ndarray) -> np.ndarray: """多普勒FFT""" doppler_fft = np.fft.fftshift( np.fft.fft(range_fft, n=self.num_chirps, axis=0), axes=0 ) return np.abs(doppler_fft) def _detect_static_targets(self, range_doppler: np.ndarray) -> List[dict]: """ 检测静态目标 CPD关键:静态目标在零多普勒附近 """ targets = [] range_doppler_sum = np.sum(range_doppler, axis=2) if range_doppler.ndim > 2 else range_doppler zero_doppler_idx = self.num_chirps // 2 range_profile = range_doppler_sum[zero_doppler_idx, :] threshold = self._cfar_threshold(range_profile) peaks = np.where(range_profile > threshold)[0] for peak_idx in peaks: if 10 < peak_idx < self.num_samples - 10: snr = range_profile[peak_idx] / np.mean(range_profile) targets.append({ 'range_idx': peak_idx, 'doppler_idx': zero_doppler_idx, 'snr': snr }) return targets def _cfar_threshold(self, signal: np.ndarray, guard_cells: int = 4, train_cells: int = 8) -> np.ndarray: """CA-CFAR阈值""" threshold = np.zeros_like(signal) for i in range(train_cells + guard_cells, len(signal) - train_cells - guard_cells): left = signal[i - train_cells - guard_cells : i - guard_cells] right = signal[i + guard_cells + 1 : i + guard_cells + train_cells + 1] noise_level = (np.sum(left) + np.sum(right)) / (2 * train_cells) threshold[i] = noise_level * 3.0 return threshold def _extract_vital_signs(self, adc_data: np.ndarray, range_idx: int) -> dict: """ 提取生命体征 原理:呼吸和心跳引起微小相位变化 """ phase_sequence = np.angle(adc_data[:, range_idx, 0]) phase_unwrapped = np.unwrap(phase_sequence) phase_detrended = signal.detrend(phase_unwrapped) fs = 1.0 / (100e-6 * self.num_chirps) b_breath, a_breath = signal.butter(4, [0.1, 0.5], btype='band', fs=fs) breath_signal = signal.filtfilt(b_breath, a_breath, phase_detrended) b_heart, a_heart = signal.butter(4, [1.0, 2.0], btype='band', fs=fs) heart_signal = signal.filtfilt(b_heart, a_heart, phase_detrended) breath_freq, breath_psd = signal.periodogram(breath_signal, fs=fs) heart_freq, heart_psd = signal.periodogram(heart_signal, fs=fs) breath_peak_idx = np.argmax(breath_psd) heart_peak_idx = np.argmax(heart_psd) breath_rate_hz = breath_freq[breath_peak_idx] heart_rate_hz = heart_freq[heart_peak_idx] breath_rate = breath_rate_hz * 60 heart_rate = heart_rate_hz * 60 confidence = self._compute_vital_confidence( breath_psd[breath_peak_idx], heart_psd[heart_peak_idx], np.max(breath_psd), np.max(heart_psd) ) return { 'breath_rate': breath_rate, 'heart_rate': heart_rate, 'confidence': confidence } def _compute_vital_confidence( self, breath_peak: float, heart_peak: float, breath_max: float, heart_max: float ) -> float: """计算生命体征置信度""" breath_conf = breath_peak / breath_max if breath_max > 0 else 0 heart_conf = heart_peak / heart_max if heart_max > 0 else 0 confidence = 0.6 * breath_conf + 0.4 * heart_conf return min(confidence, 1.0) def _compute_position(self, range_idx: int) -> Tuple[float, float, float]: """计算目标位置""" range_m = range_idx * self.range_resolution x = 0.0 y = 0.0 z = range_m return (x, y, z)
if __name__ == "__main__": cpd = IWR6843CPD() num_chirps = 128 num_samples = 256 num_rx = 4 t = np.linspace(0, 1, num_chirps) breath_phase = 0.5 * np.sin(2 * np.pi * 0.25 * t) adc_data = np.zeros((num_chirps, num_samples, num_rx), dtype=complex) target_range_idx = 50 adc_data[:, target_range_idx, :] = np.exp(1j * breath_phase)[:, np.newaxis] adc_data += 0.1 * (np.random.randn(*adc_data.shape) + 1j * np.random.randn(*adc_data.shape)) targets = cpd.process_frame(adc_data) print(f"\n检测到 {len(targets)} 个目标:") for target in targets: print(f" 距离: {target.range_idx * cpd.range_resolution:.2f} m") print(f" 呼吸率: {target.breath_rate:.1f} 次/分") print(f" 心率: {target.heart_rate:.1f} 次/分") print(f" 置信度: {target.confidence:.2f}")
|