function a5_segmentation_raw_data(id, rootpath) if ismac % run if function is not pre-compiled id = '1'; % test for example subject currentFile = mfilename('fullpath'); [pathstr,~,~] = fileparts(currentFile); cd(fullfile(pathstr,'..', '..')) rootpath = pwd; end % inputs pn.eeg_BIDS = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'eeg_BIDS'); pn.channel_locations = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'channel_locations'); pn.events = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'events'); pn.tools = fullfile(rootpath, 'tools'); % outputs pn.eeg_ft = fullfile(rootpath, 'data', 'outputs', 'eeg'); pn.history = fullfile(rootpath, 'data', 'outputs', 'history'); if ismac % run if function is not pre-compiled addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults; addpath(fullfile(pn.tools, 'helpers')); end % N = 33 IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv')); IDs = cellstr(IDs.participant_id); id = str2num(id); display(['processing ID ' num2str(IDs{id})]); %% load raw data if ~exist(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_seg.mat']),'file') % load config config = []; load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config'); %% load events file events = readtable(fullfile(pn.events, ['EMP',IDs{id}(end-1:end),'_events.csv'])); %% generate segmentation structure % load marker file mrk = config.mrk; % generate trial structure mrk_val = {mrk(1,:).value}; indOnset = find(strcmp(mrk_val(:),'boundary')) + 1; mrk_val = mrk_val(indOnset:end); % Stim trials fsample = 512; trl = zeros(length(mrk_val),3); TOI1 = 1*fsample; % segmentation before trigger (s x fsample) TOI2 = 2*fsample; % segmentation following trigger (s x fsample) for j = 1:size(trl,1) trl(j,1) = mrk(1,indOnset+j-1).sample - TOI1; trl(j,2) = mrk(1,indOnset+j-1).sample + TOI2; trl(j,3) = -TOI1; % offset end; clear j % add trial structure to config config.trl = trl; %% load, filter, and re-reference raw data % define reading & preprocessing parameters % read in all data first % load data cfg = []; cfg.datafile = ... fullfile(pn.eeg_BIDS, IDs{id}, 'eeg', [IDs{id}, '_task-xxxx_eeg.eeg']); cfg.trl = config.trl; cfg.channel = {'all'}; data_eeg = ft_preprocessing(cfg); %% reref & filter EEG data cfg = []; cfg.continuous = 'yes'; cfg.demean = 'yes'; cfg.reref = 'yes'; cfg.refmethod = 'avg'; cfg.refchannel = {'M1', 'M2'}; cfg.implicitref = 'POz'; cfg.hpfilter = 'yes'; cfg.hpfreq = .2; cfg.hpfiltord = 4; cfg.hpfilttype = 'but'; cfg.lpfilter = 'yes'; cfg.lpfreq = 125; cfg.lpfiltord = 4; cfg.lpfilttype = 'but'; % get data data_eeg = ft_preprocessing(cfg, data_eeg); flt = cfg; % clear cfg structure clear cfg %% resample to 500 Hz % define settings for resampling cfg.resamplefs = 500; cfg.detrend = 'no'; cfg.feedback = 'no'; cfg.trials = 'all'; data_eeg = ft_resampledata(cfg,data_eeg); resample = cfg; % clear variables clear cfg % update config config.seg.flt = flt; config.seg.resample = resample; %% save eeg data and history save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_seg.mat']),'data_eeg', 'events'); save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config'); end % exist