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
| """ 可穿戴fNIRS认知疲劳监测系统 基于 Pozdin & Bozkurt (Biosensors 2025) 复现
核心原理: 1. 660nm/840nm双波长LED照射前额 2. 光电探测器接收反射光 3. Modified Beer-Lambert Law计算HbO2/HbR 4. 前额叶皮层血氧变化 → 认知负荷/疲劳
Modified Beer-Lambert Law: OD(λ) = -log(I/I₀) = ε_HbO2(λ)·[HbO2]·L + ε_HbR(λ)·[HbR]·L + G
其中: λ: 波长 (660nm 或 840nm) I: 反射光强度 I₀: 参考光强度 ε: 消光系数 L: 光路长度 G: 散射损失 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple, Optional
class FNIRSSignalProcessor(nn.Module): """ fNIRS信号处理器 输入: 双波长光强度时序 输出: HbO2/HbR浓度变化 + 认知负荷评分 """ EXTINCTION = { '660_HbO2': 0.39, '660_HbR': 3.39, '840_HbO2': 0.59, '840_HbR': 0.40, } def __init__(self, pathlength_660: float = 2.5, pathlength_840: float = 2.5): super().__init__() self.L_660 = pathlength_660 self.L_840 = pathlength_840 self.classifier = nn.Sequential( nn.LSTM(input_size=4, hidden_size=64, num_layers=2, batch_first=True, dropout=0.2), ) self.fc = nn.Sequential( nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 3) ) def compute_hb(self, optical_660: torch.Tensor, optical_840: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ 计算氧合/脱氧血红蛋白浓度变化 Modified Beer-Lambert Law (双波长): Δ[HbO2] = (ε_660_HbR · ΔOD_840 - ε_840_HbR · ΔOD_660) / (L · (ε_660_HbR · ε_840_HbO2 - ε_660_HbO2 · ε_840_HbR)) Δ[HbR] = (ε_660_HbO2 · ΔOD_840 - ε_840_HbO2 · ΔOD_660) / (L · (ε_660_HbO2 · ε_840_HbR - ε_660_HbR · ε_840_HbO2)) Args: optical_660: (B, T) 660nm光强度 optical_840: (B, T) 840nm光强度 Returns: delta_HbO2: (B, T) 氧合血红蛋白变化 delta_HbR: (B, T) 脱氧血红蛋白变化 """ delta_OD_660 = -torch.log(optical_660 / ( optical_660.mean(dim=-1, keepdim=True) + 1e-8 )) delta_OD_840 = -torch.log(optical_840 / ( optical_840.mean(dim=-1, keepdim=True) + 1e-8 )) e660_HbO2 = self.EXTINCTION['660_HbO2'] e660_HbR = self.EXTINCTION['660_HbR'] e840_HbO2 = self.EXTINCTION['840_HbO2'] e840_HbR = self.EXTINCTION['840_HbR'] L = (self.L_660 + self.L_840) / 2 det = e660_HbO2 * e840_HbR - e660_HbR * e840_HbO2 delta_HbO2 = (e660_HbR * delta_OD_840 - e840_HbR * delta_OD_660) / (L * det) delta_HbR = (e840_HbO2 * delta_OD_660 - e660_HbO2 * delta_OD_840) / (L * det) return delta_HbO2, delta_HbR def forward(self, optical_660: torch.Tensor, optical_840: torch.Tensor) -> dict: """ Args: optical_660: (B, T) 660nm时序 optical_840: (B, T) 840nm时序 Returns: cognitive_load: (B, 3) 低/中/高概率 hbo2: (B, T) HbO2变化 hbr: (B, T) HbR变化 """ delta_HbO2, delta_HbR = self.compute_hb(optical_660, optical_840) features = torch.stack([delta_HbO2, delta_HbR, delta_HbO2 - delta_HbR, delta_HbO2 / (torch.abs(delta_HbR) + 1e-8)], dim=-1) lstm_out, _ = self.classifier(features) final = lstm_out[:, -1, :] logits = self.fc(final) return { 'cognitive_load': F.softmax(logits, dim=-1), 'load_level': logits.argmax(dim=-1), 'hbo2': delta_HbO2, 'hbr': delta_HbR, 'features': final }
class CognitiveFatigueMonitor: """ 认知疲劳监测器 应用场景: 1. 空中交通管制员疲劳预警 2. 飞行员认知负荷追踪 3. 驾驶员认知分心检测 4. 长时间任务疲劳管理 fNIRS信号解读: - HbO2增加 + HbR减少 → 认知激活 - HbO2降低 + HbR增加 → 认知疲劳 - HbO2波动减小 → 持续疲劳/困倦 """ FATIGUE_THRESHOLDS = { 'alert': 0.0, 'mild': -0.05, 'moderate': -0.15, 'severe': -0.25, } def __init__(self, processor: FNIRSSignalProcessor): self.processor = processor self.history = [] def update(self, optical_660: torch.Tensor, optical_840: torch.Tensor) -> dict: """更新状态""" result = self.processor(optical_660.unsqueeze(0), optical_840.unsqueeze(0)) hbo2 = result['hbo2'][0].mean().item() load = result['cognitive_load'][0] if hbo2 > self.FATIGUE_THRESHOLDS['alert']: fatigue = 'alert' elif hbo2 > self.FATIGUE_THRESHOLDS['mild']: fatigue = 'mild' elif hbo2 > self.FATIGUE_THRESHOLDS['moderate']: fatigue = 'moderate' else: fatigue = 'severe' return { 'fatigue_level': fatigue, 'cognitive_load': load.tolist(), 'hbo2_change': hbo2, 'recommendation': self._get_recommendation(fatigue) } def _get_recommendation(self, level: str) -> str: recs = { 'alert': '状态良好,可继续执行任务', 'mild': '轻度疲劳,建议短暂休息', 'moderate': '中度疲劳,建议15分钟休息', 'severe': '严重疲劳,建议停止任务', } return recs.get(level, '未知状态')
if __name__ == "__main__": processor = FNIRSSignalProcessor() monitor = CognitiveFatigueMonitor(processor) T = 1000 t = np.linspace(0, 100, T) I0_660 = 1.0 I0_840 = 1.0 cognitive_signal = np.where(t < 50, 0.02 * np.sin(2 * np.pi * 0.1 * t), -0.03 * (t - 50) / 50) optical_660 = I0_660 * np.exp(-cognitive_signal * 0.5) optical_840 = I0_840 * np.exp(-cognitive_signal * 0.3) optical_660 += np.random.normal(0, 0.002, T) optical_840 += np.random.normal(0, 0.002, T) opt_660 = torch.tensor(optical_660, dtype=torch.float32) opt_840 = torch.tensor(optical_840, dtype=torch.float32) result = processor(opt_660.unsqueeze(0), opt_840.unsqueeze(0)) print("=== fNIRS 认知疲劳监测结果 ===") print(f"采样时长: 100秒 @ 10Hz") print(f"HbO2变化均值: {result['hbo2'].mean().item():.4f}") print(f"HbR变化均值: {result['hbr'].mean().item():.4f}") print(f"认知负荷概率: {result['cognitive_load'][0].tolist()}") load_labels = ['低负荷', '中负荷', '高负荷'] print(f"判定负荷: {load_labels[result['load_level'].item()]}") print(f"\n=== 实时疲劳监测 ===") for t_check in [10, 30, 50, 70, 90]: start = max(0, t_check * 10 - 50) end = t_check * 10 result_t = processor( opt_660[start:end].unsqueeze(0), opt_840[start:end].unsqueeze(0) ) hbo2 = result_t['hbo2'].mean().item() if hbo2 > 0: level = 'alert' elif hbo2 > -0.05: level = 'mild' elif hbo2 > -0.15: level = 'moderate' else: level = 'severe' print(f" t={t_check:3d}s: HbO2={hbo2:+.4f} → {level}") print(f"\n=== 设备规格 ===") print(f"尺寸: 19 × 44 mm") print(f"重量: < 30g (含电池)") print(f"电池: 500 mAh, ~50小时") print(f"采样率: 10 Hz") print(f"LED: 660nm + 840nm") print(f"通信: BLE 5.0") print(f"成本估算: $50-200")
|