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
| """ 入耳式EEG信号采集与处理 适用于座舱内认知负荷监测 """
import numpy as np from scipy.signal import butter, filtfilt, welch
class InEarEEGProcessor: """ 入耳式EEG信号处理器 硬件:入耳式电极(2-4通道) 采样率:256-512 Hz 应用:驾驶员认知负荷/疲劳连续监测 """ def __init__(self, config: dict): self.fs = config.get('sample_rate', 256) self.n_channels = config.get('n_channels', 2) self.ref_channel = config.get('ref', 'linked_ears') self.band_low = config.get('band_low', 0.5) self.band_high = config.get('band_high', 45.0) self.notch_freq = config.get('notch', 50.0) def preprocess(self, raw_signal: np.ndarray) -> np.ndarray: """ 预处理:滤波 → 去伪迹 → 参考 Args: raw_signal: 原始EEG信号, shape=(n_samples, n_channels) Returns: clean_signal: 清理后信号 """ b, a = butter(4, [self.band_low/(self.fs/2), self.band_high/(self.fs/2)], btype='band') filtered = filtfilt(b, a, raw_signal, axis=0) if self.notch_freq > 0: Q = 30.0 w0 = self.notch_freq / (self.fs / 2) b_n, a_n = butter(2, w0, btype='notch') filtered = filtfilt(b_n, a_n, filtered, axis=0) threshold = np.mean(np.abs(filtered)) + 5 * np.std(filtered) mask = np.abs(filtered) < threshold clean = filtered * mask return clean def extract_features(self, signal: np.ndarray) -> dict: """ 提取认知负荷相关特征 频段定义: - Delta (δ): 0.5-4 Hz → 深睡眠 - Theta (θ): 4-8 Hz → 疲劳/困倦 - Alpha (α): 8-13 Hz → 放松/闭眼 - Beta (β): 13-30 Hz → 活跃/专注 - Gamma (γ): 30-45 Hz → 高级认知 关键指标: - Theta/Alpha比:疲劳程度 - Beta/Theta比:警觉水平 - Alpha不对称:情感效价 """ freqs, psd = welch(signal, fs=self.fs, nperseg=self.fs*2) bands = { 'delta': (0.5, 4), 'theta': (4, 8), 'alpha': (8, 13), 'beta': (13, 30), 'gamma': (30, 45) } band_power = {} for name, (low, high) in bands.items(): mask = (freqs >= low) & (freqs < high) band_power[name] = np.mean(psd[mask]) if np.any(mask) else 0 theta_alpha_ratio = (band_power['theta'] + 1e-10) / (band_power['alpha'] + 1e-10) beta_theta_ratio = (band_power['beta'] + 1e-10) / (band_power['theta'] + 1e-10) fatigue_index = np.clip(theta_alpha_ratio / 2.0, 0, 1) alertness_index = np.clip(beta_theta_ratio / 3.0, 0, 1) return { 'band_power': band_power, 'theta_alpha_ratio': theta_alpha_ratio, 'beta_theta_ratio': beta_theta_ratio, 'fatigue_index': fatigue_index, 'alertness_index': alertness_index, 'cognitive_load': 1.0 - alertness_index } def detect_microsleep(self, signal: np.ndarray) -> bool: """ 微睡眠检测:Alpha功率骤增 + Theta功率上升 微睡眠特征: - 持续1-15秒 - Alpha功率突然上升(闭眼效应) - Theta功率持续上升 - 行为表现:短暂失神 """ features = self.extract_features(signal) alpha_power = features['band_power']['alpha'] theta_power = features['band_power']['theta'] alpha_surge = alpha_power > np.mean(psd) * 2 theta_high = theta_power > np.mean(psd) * 1.5 return alpha_surge and theta_high
if __name__ == "__main__": np.random.seed(42) n_samples = 120 * 256 eeg_normal = np.random.normal(0, 10, (n_samples, 2)) t = np.arange(n_samples) / 256 theta_wave = 5 * np.sin(2 * np.pi * 6 * t) eeg_fatigue = eeg_normal + theta_wave.reshape(-1, 1) processor = InEarEEGProcessor({'sample_rate': 256, 'n_channels': 2}) clean_normal = processor.preprocess(eeg_normal) feat_normal = processor.extract_features(clean_normal) clean_fatigue = processor.preprocess(eeg_fatigue) feat_fatigue = processor.extract_features(clean_fatigue) print("=" * 50) print("入耳式EEG座舱内认知监测") print("=" * 50) print("\n正常驾驶状态:") print(f" Theta/Alpha比: {feat_normal['theta_alpha_ratio']:.3f}") print(f" Beta/Theta比: {feat_normal['beta_theta_ratio']:.3f}") print(f" 疲劳指数: {feat_normal['fatigue_index']:.3f}") print(f" 警觉指数: {feat_normal['alertness_index']:.3f}") print("\n疲劳驾驶状态:") print(f" Theta/Alpha比: {feat_fatigue['theta_alpha_ratio']:.3f}") print(f" Beta/Theta比: {feat_fatigue['beta_theta_ratio']:.3f}") print(f" 疲劳指数: {feat_fatigue['fatigue_index']:.3f}") print(f" 警觉指数: {feat_fatigue['alertness_index']:.3f}") print("\n" + "=" * 50) print("消费级入耳式EEG厂商对比:") vendors = [ ("Naox Technologies", "法国", "FDA 510(k)", "$299", "医疗+消费"), ("Neurable", "美国", "CE认证", "$399", "疲劳管理"), ("NextSense", "美国", "FDA临床", "$499", "睡眠监测"), ("Idia", "丹麦", "CE认证", "$249", "冥想辅助"), ("Neurosteer", "以色列", "FDA临床", "$350", "麻醉监护"), ] print(f"{'厂商':<20} {'国家':<6} {'认证':<12} {'价格':<8} {'定位'}") print("-" * 65) for v in vendors: print(f"{v[0]:<20} {v[1]:<6} {v[2]:<12} {v[3]:<8} {v[4]}")
|