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
| """ 60GHz 雷达呼吸检测算法
基于 TI IWR6843AOP 芯片 """
import numpy as np from scipy import signal from typing import Tuple, List
class RadarBreathDetector: """ 雷达呼吸检测器 核心原理: 1. 提取雷达回波相位 2. 滤除身体大运动 3. 提取呼吸频率成分 4. 检测呼吸信号 """ def __init__( self, sample_rate: float = 20, breath_freq_range: Tuple[float, float] = (0.2, 0.8), child_breath_range: Tuple[float, float] = (0.5, 1.2), ): self.sample_rate = sample_rate self.breath_freq_range = breath_freq_range self.child_breath_range = child_breath_range def extract_phase( self, radar_data: np.ndarray ) -> np.ndarray: """ 提取雷达回波相位 Args: radar_data: (N, M) 雷达数据,N=帧数,M=天线数 Returns: phase: (N,) 相位序列 """ if len(radar_data.shape) > 1: radar_signal = radar_data[:, 0] else: radar_signal = radar_data phase = np.angle(radar_signal) phase = np.unwrap(phase) return phase def filter_motion( self, phase: np.ndarray, cutoff_freq: float = 2.0 ) -> np.ndarray: """ 滤除身体大运动 使用高通滤波器去除低频运动成分 Args: phase: 相位序列 cutoff_freq: 截止频率 (Hz) Returns: filtered_phase: 滤波后相位 """ nyquist = self.sample_rate / 2 normalized_cutoff = cutoff_freq / nyquist b, a = signal.butter(4, normalized_cutoff, btype='high') filtered_phase = signal.filtfilt(b, a, phase) return filtered_phase def detect_breath_freq( self, phase: np.ndarray ) -> Tuple[float, float]: """ 检测呼吸频率 Args: phase: 相位序列 Returns: breath_freq: 呼吸频率 (Hz) confidence: 置信度 """ freqs, psd = signal.periodogram(phase, fs=self.sample_rate) mask = (freqs >= self.breath_freq_range[0]) & (freqs <= self.breath_freq_range[1]) breath_freqs = freqs[mask] breath_psd = psd[mask] if len(breath_psd) == 0: return 0.0, 0.0 peak_idx = np.argmax(breath_psd) breath_freq = breath_freqs[peak_idx] total_power = np.sum(psd) peak_power = breath_psd[peak_idx] confidence = peak_power / total_power if total_power > 0 else 0 return breath_freq, confidence def classify_occupant( self, breath_freq: float ) -> Tuple[str, float]: """ 分类乘员类型(成人/儿童) Args: breath_freq: 呼吸频率 (Hz) Returns: occupant_type: 乘员类型 probability: 概率 """ if breath_freq < 0.2: return "unknown", 0.5 elif breath_freq < 0.33: return "adult", 0.8 elif breath_freq < 0.5: return "adult/child", 0.6 elif breath_freq < 0.8: return "child", 0.85 else: return "infant", 0.9 def detect( self, radar_data: np.ndarray ) -> dict: """ 综合检测 Args: radar_data: 雷达数据 Returns: result: 检测结果 """ phase = self.extract_phase(radar_data) filtered_phase = self.filter_motion(phase) breath_freq, confidence = self.detect_breath_freq(filtered_phase) occupant_type, prob = self.classify_occupant(breath_freq) breath_rate = breath_freq * 60 return { 'breath_detected': confidence > 0.3, 'breath_freq_hz': breath_freq, 'breath_rate_per_min': breath_rate, 'confidence': confidence, 'occupant_type': occupant_type, 'occupant_prob': prob, 'is_child': occupant_type in ['child', 'infant'] }
if __name__ == "__main__": np.random.seed(42) detector = RadarBreathDetector() n_samples = 300 t = np.arange(n_samples) / 20 breath_freq = 0.6 breath_signal = 0.5 * np.sin(2 * np.pi * breath_freq * t) noise = 0.1 * np.random.randn(n_samples) phase = breath_signal + noise radar_data = np.exp(1j * phase * 10) result = detector.detect(radar_data) print("检测结果:") print(f" 检测到呼吸: {result['breath_detected']}") print(f" 呼吸频率: {result['breath_rate_per_min']:.1f} 次/分") print(f" 置信度: {result['confidence']:.2f}") print(f" 乘员类型: {result['occupant_type']}") print(f" 是否儿童: {result['is_child']}")
|