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
| import numpy as np from scipy.signal import welch from scipy.stats import entropy
class MultimodalFeatureExtractor: """ 多模态驾驶员状态特征提取器 支持5种模态: EEG, ECG, EDA, Eye tracking, Vehicle control 每种模态提取时域+频域+非线性特征 """ @staticmethod def extract_eeg_features( eeg: np.ndarray, fs: int = 250, bands: dict = None ) -> dict: """ EEG特征提取 默认频段: - θ (4-8 Hz): 与走神相关 - α (8-13 Hz): 放松/闭眼 - β (13-30 Hz): 活跃注意 - γ (30-50 Hz): 高级认知 """ if bands is None: bands = { 'theta': (4, 8), 'alpha': (8, 13), 'beta': (13, 30), 'gamma': (30, 50) } n_ch, n_t = eeg.shape features = {} for band_name, (f_low, f_high) in bands.items(): band_power = np.zeros(n_ch) for ch in range(n_ch): freqs, psd = welch(eeg[ch], fs=fs, nperseg=fs*2) idx = (freqs >= f_low) & (freqs <= f_high) band_power[ch] = np.sum(psd[idx]) features[f'{band_name}_power'] = band_power features[f'{band_name}_mean'] = np.mean(band_power) features[f'{band_name}_std'] = np.std(band_power) for ch in range(n_ch): freqs, psd = welch(eeg[ch], fs=fs, nperseg=fs*2) psd_norm = psd / np.sum(psd) features[f'eeg_entropy_ch{ch}'] = entropy(psd_norm + 1e-12) features['theta_alpha_ratio'] = ( features['theta_mean'] / (features['alpha_mean'] + 1e-8) ) return features @staticmethod def extract_ecg_features(ecg: np.ndarray, fs: int = 500) -> dict: """ ECG特征提取 — HRV时域+频域 """ rr_intervals = np.diff(np.where(ecg > np.mean(ecg) + 2*np.std(ecg))[0]) rr_ms = rr_intervals / fs * 1000 features = { 'hr_mean': 60000 / np.mean(rr_ms) if len(rr_ms) > 0 else 0, 'hr_std': np.std(rr_ms), 'rmssd': np.sqrt(np.mean(np.diff(rr_ms)**2)) if len(rr_ms) > 1 else 0, 'nn50': np.sum(np.abs(np.diff(rr_ms)) > 50), 'pnn50': np.sum(np.abs(np.diff(rr_ms)) > 50) / len(rr_ms) * 100, } if len(rr_ms) > 10: rr_interp = np.interp( np.arange(0, len(rr_ms), 0.001), np.arange(0, len(rr_ms)), rr_ms ) freqs, psd = welch(rr_interp, fs=1000, nperseg=256) lf = np.sum(psd[(freqs >= 0.04) & (freqs < 0.15)]) hf = np.sum(psd[(freqs >= 0.15) & (freqs < 0.40)]) features['lf'] = lf features['hf'] = hf features['lf_hf_ratio'] = lf / (hf + 1e-8) return features @staticmethod def extract_eye_features( gaze_x: np.ndarray, gaze_y: np.ndarray, pupil_diameter: np.ndarray, blink_events: np.ndarray, window_sec: int = 30 ) -> dict: """ 眼动特征提取 关键特征: - 凝视熵: 注意力分散程度 - 扫视频率: 视觉搜索活跃度 - PERCLOS: 闭眼百分比 - 瞳孔变异: 认知负荷 """ n = len(gaze_x) hist_2d, _, _ = np.histogram2d( gaze_x, gaze_y, bins=20, range=[[-1, 1], [-1, 1]] ) hist_norm = hist_2d / hist_2d.sum() gaze_entropy = entropy(hist_norm.flatten() + 1e-12) saccade_freq = len(blink_events) / window_sec closed_ratio = np.mean(pupil_diameter < 0.3 * np.mean(pupil_diameter)) pupil_cv = np.std(pupil_diameter) / (np.mean(pupil_diameter) + 1e-8) return { 'gaze_entropy': gaze_entropy, 'saccade_freq': saccade_freq, 'perclos': closed_ratio * 100, 'pupil_cv': pupil_cv, 'pupil_mean': np.mean(pupil_diameter), 'pupil_std': np.std(pupil_diameter) } @staticmethod def extract_vehicle_features( steering: np.ndarray, speed: np.ndarray, lane_pos: np.ndarray, window_sec: int = 30 ) -> dict: """ 车辆控制特征 """ return { 'steering_sd': np.std(steering), 'steering_entropy': entropy( np.histogram(steering, bins=50)[0] / len(steering) + 1e-12 ), 'speed_cv': np.std(speed) / (np.mean(speed) + 1e-8), 'lane_pos_sd': np.std(lane_pos), 'lane_departures': np.sum(np.abs(lane_pos) > 0.5) }
if __name__ == "__main__": rng = np.random.default_rng(42) extractor = MultimodalFeatureExtractor() eeg = rng.normal(0, 10, (8, 2500)) ecg = rng.normal(0, 1, 5000) gaze_x = rng.uniform(-0.5, 0.5, 3000) gaze_y = rng.uniform(-0.3, 0.3, 3000) pupil = rng.normal(4, 0.5, 3000) blinks = rng.integers(0, 3000, 10) steering = rng.normal(0, 0.1, 3000) speed = rng.normal(50, 5, 3000) lane = rng.normal(0, 0.2, 3000) eeg_feat = extractor.extract_eeg_features(eeg) ecg_feat = extractor.extract_ecg_features(ecg) eye_feat = extractor.extract_eye_features(gaze_x, gaze_y, pupil, blinks) veh_feat = extractor.extract_vehicle_features(steering, speed, lane) print("=== EEG特征 ===") for k, v in eeg_feat.items(): if isinstance(v, float): print(f" {k}: {v:.4f}") print("\n=== ECG特征 ===") for k, v in ecg_feat.items(): print(f" {k}: {v:.4f}") print("\n=== 眼动特征 ===") for k, v in eye_feat.items(): print(f" {k}: {v:.4f}") print("\n=== 车辆特征 ===") for k, v in veh_feat.items(): print(f" {k}: {v:.4f}")
|