Browse Source

adapt step1 to current data

kosciessa 2 years ago
parent
commit
55cf23e099
1 changed files with 104 additions and 0 deletions
  1. 104 0
      code/a1_prepare_preprocessing.m

+ 104 - 0
code/a1_prepare_preprocessing.m

@@ -0,0 +1,104 @@
+%% 01_prepare_preprocessing
+
+% prepare (i.e. filter + downsample data) for ICA1
+
+%% initialize
+
+restoredefaultpath;
+clear all; close all; pack; clc;
+
+%% pathdef
+
+currentFile = mfilename('fullpath');
+[pathstr,~,~] = fileparts( currentFile );
+cd(pathstr)
+
+% inputs
+pn.eeg_BIDS = fullfile('..', 'data', 'inputs', 'rawdata', 'eeg_BIDS');
+pn.channel_locations = fullfile('..', 'data', 'inputs', 'rawdata', 'channel_locations');
+pn.events = fullfile('..', 'data', 'inputs', 'rawdata', 'events');
+% outputs
+pn.eeg_ft = fullfile('..', 'data', 'outputs', 'eeg'); if ~exist(pn.eeg_ft); mkdir(pn.eeg_ft); end
+pn.history = fullfile('..', 'data', 'outputs', 'history'); if ~exist(pn.history); mkdir(pn.history); end
+
+% add fieldtrip toolbox
+addpath(fullfile('..', 'tools', 'fieldtrip')); ft_defaults;
+
+%% datadef
+
+cond_eeg = 'dynamic';
+
+%% define IDs for preprocessing
+
+% N = 33
+IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
+IDs = cellstr(IDs.participant_id);
+
+for id = 1:length(IDs)
+
+if ~exist(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'file')
+    
+    % load data
+    cfg_preproc           = [];
+    cfg_preproc.datafile  = ...
+        fullfile(pn.eeg_BIDS, IDs{id}, 'eeg', [IDs{id}, '_task-xxxx_eeg.eeg']);
+
+    % load header & event information
+    config              = ft_read_header(cfg_preproc.datafile);
+    config.data_file    = cfg_preproc.datafile;
+    config.mrk          = ft_read_event(cfg_preproc.datafile);
+
+    % define reading & preprocessing parameters
+    cfg_preproc.channel     = {'all'};
+    cfg_preproc.implicitref = 'REF'; % recover implicit reference
+
+    % get all data first, then apply specific steps only for subsets
+    data_eeg = ft_preprocessing(cfg_preproc);
+
+    %% preprocessing
+
+    cfg_preproc               = [];                                 
+    cfg_preproc.channel       = {'all'};
+
+    cfg_preproc.continuous  = 'yes';
+    cfg_preproc.demean      = 'yes';
+
+    cfg_preproc.reref       = 'yes';
+    cfg_preproc.refchannel  = {'M1', 'M2'};
+    cfg_preproc.implicitref = 'A2';
+
+    cfg_preproc.hpfilter    = 'yes';
+    cfg_preproc.hpfreq      = 1;
+    cfg_preproc.hpfiltord   = 4;
+    cfg_preproc.hpfilttype  = 'but';
+
+    cfg_preproc.lpfilter    = 'yes';
+    cfg_preproc.lpfreq      = 100;
+    cfg_preproc.lpfiltord   = 4;
+    cfg_preproc.lpfilttype  = 'but';
+
+    data_eeg = ft_preprocessing(cfg_preproc, data_eeg);
+    
+    % define settings for resampling
+    cfg_resample.resamplefs = 250;
+    cfg_resample.detrend    = 'no';
+    cfg_resample.feedback   = 'no';
+    cfg_resample.trials     = 'all';
+
+    data_eeg = ft_resampledata(cfg_resample,data_eeg);
+    
+    % change data precision to single
+    for t = 1:length(data_eeg.trial)
+        data_eeg.trial{t} = single(data_eeg.trial{t});
+    end; clear t
+
+    %% save outputs
+
+    save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'data_eeg');
+    % save config
+    save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
+    % clear variables
+    clear cfg_* config data_eeg
+    
+end
+end; clear id;