juliankeil

FieldTrip Preprocessing

Aug 8th, 2012
2,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 8.53 KB | None | 0 0
  1. %% Montreal-Tutorial
  2. % Steps:
  3. % 1. Take a look at the data, what's in there?
  4. % 2. Define the trials based on triggers
  5. % 2.1. Read in the data trials
  6. % 3. Take a look at the data again, what does it look like?
  7. % 4. Throw out all you don't need
  8. % 4.1. Remove TMS-Artefact
  9. % 4.2. Filter
  10. % 4.3. Downsample
  11. % 4.4. Remove Blinks etc.
  12. % 4.5. ICA
  13.  
  14. % Julian Keil, 13.04.2012
  15.  
  16. %% Set Path and Data
  17.  
  18. inpath = ('/Users/juliankeil/Documents/Arbeit/ACN-Exchange/Tutorial/Tutorial Data'); % path to the data
  19.  
  20. cd (inpath) % GoTo path
  21.  
  22. data{1} = 'Pilot1.cnt'; % What's the dataset called?
  23.  
  24. %% 1. Take a look
  25.  
  26. cfg=[]; % empties the universally used cfg-structure
  27. cfg.dataset=data{1}; % which dataset? Useful if a for-loop is used
  28. cfg.trialdef.eventtype= '?'; % We don't know what events have happened
  29. cfg.headerformat='ns_cnt32'; % Watch Out! With the neuroscan system, you have to set the bit-rate
  30. cfg.dataformat='ns_cnt32'; % Otherwise you'll have corrupt data
  31. cfg.eventformat='ns_cnt32';
  32.  
  33. cfg=ft_definetrial(cfg); % definetrail looks at all events in the dataset
  34.  
  35. %% 2. Define the trials based on triggers
  36.  
  37. cfg=[]; % Empty the cfg again, so you don't mess up stuff
  38. cfg.dataset=data{1}; % which dataset? Useful if a for-loop is used
  39. cfg.trialdef.eventtype='trigger'; % We now know that the trigger channel is called 'trigger'
  40. %cfg.trialdef.eventvalue=6; % Only read in the trigger 13, more complex trial definitions need a trial-function
  41. cfg.trialdef.prestim=1.5; % Seconds before the stimulus
  42. cfg.trialdef.poststim=1.5; % Seconds after the stimulus
  43. cfg.trialfun='trialfun_MEP'; % the normal trial-function is fine for now, otherwise call for an own
  44. cfg.headerformat='ns_cnt32'; % Watch Out! With the neuroscan system, you have to set the bit-rate
  45. cfg.dataformat='ns_cnt32'; % Otherwise you'll have corrupt data
  46. cfg.eventformat='ns_cnt32';
  47.  
  48. cfg=ft_definetrial(cfg); % define trials based on these settings
  49.  
  50. %% 2.1. Actually read in the data. This might take a while. Get some fresh air.
  51. % Watch out! Don't clear the cfg!
  52.  
  53. cfg.detrend='yes'; % De-Trending, so that we get rid of the offset and slow drift in the EEG-data
  54. % cfg.hpfilter='yes'; % High-Pass-Filter On
  55. % cfg.hpfreq=2; % High-Pass Frequenc
  56. % cfg.hpfilttype='but'; % Filter Type Butterworth (IIR)
  57. % cfg.hpfiltord=6; % % Filter Order, watch out for Instability!
  58. % cfg.lpfilter='yes'; % Low-Pass Filter On
  59. % cfg.lpfreq=100; % Low-Pass Frequency
  60. % cfg.lpfilttype='but'; % Filter Type
  61. % cfg.lpfiltord=16; % Filter Order
  62. cfg.dftfilter='yes'; % Line Noise Filter using Discrete Fourier Transform
  63. cfg.dftfreq=[30 60 90 120 180]; % Line Frequency (in Europe this would be 50)
  64. %cfg.trials=[1 2 3];
  65. tic
  66. dat=ft_preprocessing(cfg); % Read in the trials definded above
  67. toc
  68. % Now we have the dat-structure containing the actual EEG-Data with the
  69. % fields:
  70. % hdr: Header information
  71. % label: labels for the data-channels
  72. % time: time-vector for each trial
  73. % trial: actual data for each trial in chan-by-samplepoint
  74. % fsample: sample-frequency
  75. % sampleinfo: Beginning and End for each trial in sample points
  76. % trialinfo: trigger value for each trial
  77. % cfg: configuration that was used to call definetrial
  78. %% 3. Take a look
  79. load ~/subversion/tinnitus/matlab/Julian/NeuroScan64.mat % this contains the Neuroscan Layout.
  80.  
  81. cfg=[]; % the usual
  82. cfg.channel={'EEG' 'CB1' 'CB2' '-CP3'};
  83. cfg.viewmode='vertical'; % or butterfly
  84. cfg.selectmode='eval'; % what should happen if you select a patch of data
  85. cfg.selcfg.layout=lay; % we use the neuroscan Layout, more Layouts can be found in the fieldtrip %directory in the "templates"-Folder or you can create your own using ft_prepare_layout
  86.  
  87. ft_databrowser(cfg,dat); % we call the databrowser with the cfg-settings and the dat-structure defined above
  88.  
  89. %% 4. Now let's start trowing out all the stuff we don't need
  90. % First clean the TMS-Artifact as it srews up the filtering and ICA
  91. % Then Downsample the data, as you don't really need 2000 Hz sampling
  92. % Then maybe filter a bit
  93. % And remove artifacts by visual rejection
  94. % Finally do some fancy ICA cleaning
  95.  
  96. %% 4.1. clean TMS artifact as described in Current Biology by Thut et al. 2011
  97. cfg=[];
  98. cfg.interp='spline'; % method for interpolation of the artifact
  99. %cfg.pretime=10;
  100. %cfg.posttime=50;
  101.  
  102. dat_cubic=cleanTMS(cfg,dat); % You'll end up with a new dataset
  103.            
  104. %% 4.2. Downsample
  105. cfg=[];
  106. cfg.resamplefs=300; % New Samplerate in Hz
  107. cfg.detrend='no'; % We already detrended our data.
  108.  
  109. dat_res=ft_resampledata(cfg,dat);
  110.  
  111. %% 4.3. Filter a little (or a lot ;-))
  112.  
  113. cfg=[];
  114. cfg.channel={'EEG' 'CB1' 'CB2'};
  115. cfg.hpfilter='yes'; % High-Pass-Filter On
  116. cfg.hpfreq=3; % High-Pass Frequenc
  117. cfg.hpfilttype='but'; % Filter Type Butterworth (IIR)
  118. cfg.hpfiltord=2; % % Filter Order, watch out for Instability!
  119. cfg.lpfilter='yes'; % Low-Pass Filter On
  120. cfg.lpfreq=25; % Low-Pass Frequency
  121. cfg.lpfilttype='but'; % Filter Type
  122. cfg.lpfiltord=16; % Filter Order
  123.  
  124. dat_filt=ft_preprocessing(cfg,dat_res); % Here we call again for preprocessing,
  125. %but this time with an actual dataset at and. Don't get confused, we're
  126. %just using the same function again. Also, you might want to check the
  127. %filter settings so this doesn't screw up the data!
  128.  
  129. %% 4.4. Visual Artifact Rejection
  130. % Watch out!
  131. % I fyou get an error when plotting the channels, you might have to set
  132. % cfg.mp.interactive='yes'; in rejectvisual_summary. It's a but, not a
  133. % feature.
  134.  
  135. cfg=[];
  136. cfg.channel={'EEG' 'CB1' 'CB2'}; % We'll only look at the EEG-Channels, the rest at the moment doesn't matter to us
  137. cfg.method='summary';% We'll look at all the info we can get
  138. cfg.latency=[-.7 .7];% Set the latenc
  139. cfg.keepchannel='yes';% If you want to interpolate channels, you have to keep the bad ones!
  140. cfg.layout=lay; % Also, you have to actually set CB1 and CB2 as EEG
  141.  
  142. dat_clear=ft_rejectvisual(cfg,dat_res);
  143.  
  144. %% 4.4.1. Interpolate Channels
  145. % First you have to define, which channels are neighbors to which channels
  146. % Then you can interpolate from the surrounding channels
  147. % However, we need to do this in 3D-space, so we need the actual
  148. % 3-Dimensional Senesor positions:
  149.  
  150. elec.pnt=Scan64_pos; % the 3D-Positions
  151. elec.label=Scan64_lab; % and the names
  152. %% 4.4.1.1. Neighbors
  153. cfg=[];
  154. cfg.method='distance'; % how should the neighbors be selected?
  155. cfg.neighbourdist=50; % I have no Idea what range this has, just make sure, that you get meaningful neighbors
  156. cfg.elec=elec; % Here we need the 3d-positions!
  157. dummy=ft_prepare_neighbours(cfg); % between 5 and 10 neighbors is a good value, always good to check!
  158.    
  159. %% 4.4.1.2. Check!
  160. cfg=[];
  161. cfg.neighbours=dummy; % what neighbor-structure
  162. cfg.elec=elec; % what layout
  163. ft_neighbourplot(cfg)
  164. % Again, ist's best to check the actual neighbors for each channel. On
  165. % average you'll want to end up with ahout 5 to 10 neighbors for each
  166. % channel, at least have 2, otherwise you'll just end up copying one
  167. % channel.
  168.  
  169. %% 4.4.1.3. Repair
  170. cfg=[];
  171. cfg.badchannel={'FT8' 'CP3' 'FP1'}; % Who's bad?
  172. cfg.neighbours=dummy; % What channels should be used to fix?
  173.  
  174. dat_clear.elec=elec; % and we need the 3d positions
  175.  
  176. dat_fix=ft_channelrepair(cfg,dat_clear);
  177.  
  178. %% 4.5. ICA
  179. % in order to get rid of blink artefacts and external noise, the
  180. % independent component analysis can be useful. But Beware, don't exclude
  181. % to much! Whereas teh ICA can split the signal into independent
  182. % components, this does not mean that one component does not contain
  183. % information from more than one source!
  184.  
  185. %% 4.5.1. Compute ICA-Components
  186. cfg=[];
  187. cfg.channel={'EEG' 'CB1' 'CB2'}; % Super important to only use the EEG-Channels, otherwise the ICA won't work
  188. cfg.method='fastica'; % Whcih method should be used?
  189. cfg.numcomponent=50; % if the ICA does not converge, limit the number of components to compute
  190. cfg.demean='no';
  191. comp=ft_componentanalysis(cfg,dat_fix);
  192.  
  193. %% 4.5.2. Take a look at the components
  194.  
  195. cfg=[];
  196. cfg.layout=lay;
  197. cfg.viewmode='component'; % same as above, just with a different mode
  198. ft_databrowser(cfg,comp);
  199.  
  200. %% 4.5.3. And take out the ones that are clearly artefacts
  201.  
  202. cfg=[];
  203. cfg.component=[9]; % I focus on blinks and muscle artefacts
  204. dat_ica=ft_rejectcomponent(cfg,comp,dat_fix);
  205.  
  206.  
  207. %% 4.5.4 Compare before & after
  208. figure;plot(dat_ica.time{1},dat_ica.trial{1}(22,:),'r');hold;plot(dat_clear.time{1},dat_clear.trial{1}(22,:),'b')
  209.  
  210. %% 6 Save
  211. %mkdir 2ndtry
  212. cd 2ndtry
  213. save dat_orig.mat dat -V7.3
  214. %save dat_cubic.mat dat_cubic -V7.3
  215. save dat_clear.mat dat_clear -V7.3
  216. %save dat_filt.mat dat_filt -V7.3
  217. save dat_res.mat dat_res -V7.3
  218. save dat_ica.mat dat_ica comp -V7.3
  219. save dat_fix.mat dat_fix -V7.3
Advertisement
Add Comment
Please, Sign In to add comment