How can I extract and store signals of the different frequency band from the EEG signal?
- MNE-Python version: 0.19.2
- Google Colab:
**
for each_subject in list_ids:
path_0=PATH_SIGNALS_CSV+each_subject+'_'+FILES_USE[0]
path_1=PATH_SIGNALS_CSV+each_subject+'_'+FILES_USE[1]
path_2=PATH_SIGNALS_CSV+each_subject+'_'+FILES_USE[2]
print('ID_list', len(list_ids))
df_subject_channels=pd.read_csv(path_0,
sep=',',
decimal=".")
print('size of channel data', df_subject_channels.shape)
#channels labels
ch_labels=list(df_subject_channels.labels)
#apply montage
internal_montage = mne.channels.make_standard_montage(MONTAGE)
print('montage', internal_montage)
#create info for object
info = mne.create_info(ch_names=ch_labels,
sfreq=s_freq,
ch_types='eeg',
montage=internal_montage)
#signal
#load and scale signal
dat_test=np.loadtxt(path_1, delimiter=',')*SCALE
print('size of signal data', dat_test.shape)
#Create the MNE Raw data object
raw = mne.io.RawArray(dat_test, info)
#create in stimuation channel
stim_info = mne.create_info(['stim'], s_freq, 'stim')
#create zero signal to store stimulus
stim_raw = mne.io.RawArray(np.zeros(shape=[1, len(raw._times)]), stim_info)
#add stim channle to raw signal
raw.add_channels([stim_raw], force_update_info=True)
#events
#read csv of events
df_subject_event=pd.read_csv(path_2,
delimiter=",",
decimal=".")
#fake structure of events
evs = np.empty(shape=[0, 3])
#from HBT, the signals were already marked each 20 seconds.
for each_element in df_subject_event.values[1:len(df_subject_event)-1]:
if('break cnt'!=each_element[0]):
if(int(each_element[0])==EVENT_ID):
evs = np.vstack((evs, np.array([each_element[1], 0,
int(each_element[0])])))
#print(evs)
# Add events to data object
raw.add_events(evs, stim_channel='stim')
#Check events
#print(mne.find_events(raw))
#detect flat channels
flat_chans = np.mean(raw._data[:len(ch_labels), :], axis=1) == 0
# Interpolate bad channels
raw.info['bads'] = \
list(np.array(raw.ch_names[:len(ch_labels)])[flat_chans])
print('Bad channels: ', raw.info['bads'])
raw.interpolate_bads()
# Get good eeg channel indices
#eeg_chans = mne.pick_types(raw.info, meg=False, eeg=True)
#resample to have to 250 hz,
#this will allow us to compare with
#the ADHD dataset.
raw.resample(R_S_FREQ, npad='auto')
#set reference to Cz
raw.set_eeg_reference(ref_channels=['Cz'])
raw.drop_channels(['Cz'])
events = mne.find_events(raw)
picks = mne.pick_types(raw.info, eeg=True, eog=False, stim=True)
#print('pick.info', picks)
# Construct Epochs
event_id, tmin, tmax = 30, -1., 3.
baseline = (None, 0)
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=baseline,
preload=True)
print('epoch shape',len(epochs))
epochs.resample(200., npad='auto') # resample to reduce computation time
mne.events_from_annotations(raw)
sf=250
time=np.arange(len(epochs))/sf
print('Time', time)
''' calculate the PSD and extract power bands from it'''
**.
Please help me extract features from the signals!
When I try to calculate the PSD of a signal
from scipy import signal
win = 4 * sf
freqs, psd = signal.welch(raw, sf, nperseg=20)
pritnt('Freqs shape',freqs.shape)
pritnt('Psd shape',Psd.shape)
I get an error → ValueError: All picks must be < n_channels (111), got 111