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
| import numpy as np from scipy import signal
class EEGFatigueDetector: """EEG 疲劳检测器""" def __init__(self, fs: int = 256): self.fs = fs self.bands = { 'delta': (0.5, 4), 'theta': (4, 8), 'alpha': (8, 13), 'beta': (13, 30), 'gamma': (30, 50) } def extract_features(self, eeg_signal: np.ndarray) -> dict: """ 提取频域特征 Args: eeg_signal: EEG 信号 (channels, time) Returns: features: 频带功率比 """ features = {} for band_name, (low, high) in self.bands.items(): sos = signal.butter(4, [low, high], btype='band', fs=self.fs, output='sos') filtered = signal.sosfiltfilt(sos, eeg_signal, axis=1) power = np.mean(filtered ** 2, axis=1) features[f'{band_name}_power'] = power features['fatigue_index'] = ( features['theta_power'] / (features['alpha_power'] + 1e-6) ) return features def classify_fatigue(self, features: dict) -> str: """分类疲劳等级""" fatigue_index = features['fatigue_index'].mean() if fatigue_index > 2.0: return 'severe_fatigue' elif fatigue_index > 1.5: return 'mild_fatigue' else: return 'alert'
|