# Record data for mental activity 0 BCIw.beep() eeg_data0, timestamps0 = inlet.pull_chunk(timeout=training_length + 1, max_samples=fs * training_length) eeg_data0 = np.array(eeg_data0)[:, index_channel] print('\nClose your eyes!\n') # Record data for mental activity 1 BCIw.beep() # Beep sound eeg_data1, timestamps1 = inlet.pull_chunk(timeout=training_length + 1, max_samples=fs * training_length) eeg_data1 = np.array(eeg_data1)[:, index_channel] # Divide data into epochs eeg_epochs0 = BCIw.epoch(eeg_data0, epoch_length * fs, overlap_length * fs) eeg_epochs1 = BCIw.epoch(eeg_data1, epoch_length * fs, overlap_length * fs) """ 4. COMPUTE FEATURES AND TRAIN CLASSIFIER """ feat_matrix0 = BCIw.compute_feature_matrix(eeg_epochs0, fs) feat_matrix1 = BCIw.compute_feature_matrix(eeg_epochs1, fs) [classifier, mu_ft, std_ft, score] = BCIw.train_classifier(feat_matrix0, feat_matrix1, 'SVM') print(str(score * 100) + '% correctly predicted') BCIw.beep() """ 5. USE THE CLASSIFIER IN REAL-TIME""" # Initialize the buffers for storing raw EEG and decisions
# Record data for mental activity 0 BCIw.beep() eeg_data0, timestamps0 = inlet.pull_chunk( timeout=training_length+1, max_samples=fs * training_length) eeg_data0 = np.array(eeg_data0)[:, index_channel] print('\nClose your eyes!\n') # Record data for mental activity 1 BCIw.beep() # Beep sound eeg_data1, timestamps1 = inlet.pull_chunk( timeout=training_length+1, max_samples=fs * training_length) eeg_data1 = np.array(eeg_data1)[:, index_channel] # Divide data into epochs eeg_epochs0 = BCIw.epoch(eeg_data0, epoch_length * fs, overlap_length * fs) eeg_epochs1 = BCIw.epoch(eeg_data1, epoch_length * fs, overlap_length * fs) """ 4. COMPUTE FEATURES AND TRAIN CLASSIFIER """ feat_matrix0 = BCIw.compute_feature_matrix(eeg_epochs0, fs) feat_matrix1 = BCIw.compute_feature_matrix(eeg_epochs1, fs) [classifier, mu_ft, std_ft, score] = BCIw.train_classifier( feat_matrix0, feat_matrix1, 'SVM') print(str(score * 100) + '% correctly predicted') BCIw.beep()
def preprocess_eeg(p): uf = butter_bandpass_filter(p, 2, 8, 256) uf = BCIw.epoch(uf, 256, 0.8 * 256) return uf