JA_pilot_analysis_DEF_rep.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. %%
  2. clear
  3. general_directory = '/Users/b1037210/Desktop/Progetti/JA_Stage1/'
  4. cd(general_directory)
  5. addpath(general_directory)
  6. %%
  7. addpath(genpath('/Users/b1037210/Documents/MATLAB/fieldtrip-20161117'))
  8. ft_defaults
  9. %%
  10. subjects = ...
  11. {'S01_pilot',...
  12. 'S02_pilot',...
  13. 'S03_pilot',...
  14. 'S04_pilot',...
  15. 'S05_pilot',...
  16. 'S06_pilot',...
  17. 'S07_pilot',...
  18. 'S08_pilot',...
  19. 'S09_pilot',...
  20. 'S10_pilot',...
  21. 'S11_pilot',...
  22. 'S12_pilot'}
  23. threshold = {[0.018 0.032],...
  24. [0.016 0.035],...
  25. [0.018 0.040],...
  26. [0.019 0.039],...
  27. [0.017 0.037],...
  28. [0.016 0.036],...
  29. [0.019 0.042],...
  30. [0.018 0.039],...
  31. [0.016 0.035],...
  32. [0.015 0.035],...
  33. [0.02 0.03],...
  34. [0.02 0.04]}
  35. TMS_timings = {'-0.05', '0.0'};
  36. final_table = table;
  37. %%
  38. for s = 1:length(subjects)
  39. %% cd directory
  40. cd([general_directory,subjects{s}])
  41. %% ...for each tms timing
  42. for t = 1:length(TMS_timings)
  43. %% file loading
  44. file_tmp = (dir(strcat(subjects{s},'_', TMS_timings{t},'_TMS*')))
  45. if size(file_tmp,1)>1
  46. disp(strcat('concatenate ',file_tmp(1).name))
  47. [y SR MetaData] = GB_gtec_files_concatenation(file_tmp);
  48. else
  49. disp(strcat('loading ', file_tmp.name))
  50. load(file_tmp.name)
  51. end
  52. clear file_tmp
  53. %% name the trigger channel and time channel, then erase time from y
  54. % select trigger row and time row
  55. trigger = y(end,:); % trigger channel for glove touch detection
  56. time = y(1,:); % time-series in seconds
  57. % new data without time, so channels in the gtect settings and data
  58. % analysis have the same nnumber
  59. y = y(2:end,:);
  60. %% get triggers and sample points at which each event of interest happens
  61. % get were variations happen within trigger channel
  62. diff_trigger = diff(trigger);
  63. % get trigger codes wihtin trgger line
  64. trigger_numbers = unique(trigger);
  65. % find were the wash_out ends
  66. end_wash_out = find(diff_trigger == 1)+1;
  67. end_wash_out = end_wash_out(1);
  68. % for each code present, get the sample were the rising triggers are present
  69. for x = 1:length(trigger_numbers)
  70. % find rising triggers and add 1 sampling point (due to diff which removes one)
  71. tmp = find(diff_trigger == trigger_numbers(x))+1;
  72. % based on the code --> assign the tmp numbers to the correct variable
  73. switch trigger_numbers(x)
  74. % case 14 % color press CF
  75. % sp_slow_id = tmp(tmp>end_wash_out);
  76. %
  77. % case 15 % color press CF
  78. % sp_fast_id = tmp(tmp>end_wash_out);
  79. case 16 % color press CF
  80. sp_out_slow_id = tmp(tmp>end_wash_out);
  81. case 17 % color press CF
  82. sp_out_fast_id = tmp(tmp>end_wash_out);
  83. % case 18 % color press CF
  84. % sp_out_ok_catch_id = tmp(tmp>end_wash_out);
  85. %
  86. % case 19 % color press CF
  87. % sp_out_failed_catch_id = tmp(tmp>end_wash_out);
  88. %
  89. case 20 % color press CF
  90. sp_color_CF_press = tmp(tmp>end_wash_out);
  91. case 21 % color lift CF
  92. sp_color_CF_lift = tmp(tmp>end_wash_out);
  93. case 22 % color catch CF
  94. sp_color_CF_catch = tmp(tmp>end_wash_out);
  95. case 30 % color press SBJ
  96. sp_color_SBJ_press = tmp(tmp>end_wash_out);
  97. case 31 % color_lift SBJ
  98. sp_color_SBJ_lift = tmp(tmp>end_wash_out);
  99. case 40 % CF hitting
  100. sp_CF_hitting = tmp(tmp>end_wash_out);
  101. case 41 % SBJ hitting
  102. sp_SBJ_hitting = tmp(tmp>end_wash_out);
  103. case 128 % TMS pulse
  104. sp_TMS = tmp(tmp>end_wash_out);
  105. end
  106. end
  107. %%
  108. % check dimensions
  109. size(sp_color_CF_press)
  110. size(sp_color_CF_lift)
  111. size(sp_color_CF_catch)
  112. size(sp_TMS)
  113. %
  114. [CF_colors_idx] = sortrows(vertcat(sp_color_CF_press',sp_color_CF_lift',sp_color_CF_catch'));
  115. CF_colors_order = CF_colors_idx;
  116. % assign codes to sample points corresponding to press lift and catch
  117. CF_colors_order(ismember(CF_colors_idx, sp_color_CF_press)) = 1;
  118. CF_colors_order(ismember(CF_colors_idx, sp_color_CF_lift)) = -1;
  119. CF_colors_order(ismember(CF_colors_idx, sp_color_CF_catch)) = 0;
  120. %%
  121. size(sp_out_slow_id)
  122. size(sp_out_fast_id)
  123. [trials_idx] = sortrows(vertcat(sp_out_slow_id',sp_out_fast_id'))
  124. %%
  125. for ex = 1:length(trials_idx)
  126. [v_abs(ex) p_abs(ex)] = min(abs(CF_colors_idx - trials_idx(ex)))
  127. ddd = CF_colors_idx - trials_idx(ex);
  128. exclude(ex) = p_abs(ex);
  129. is_exclude_positive(ex) = ddd(exclude(ex))
  130. end
  131. clear v_abs
  132. clear ddd
  133. clear is_exclude_positive
  134. %% EMG preprocessing
  135. ECD_right = y(2,:);
  136. FDS_right = y(4,:);
  137. ECD_response = y(6,:);
  138. Fsample = SR;
  139. hp_filter = 10
  140. lp_filter = 2500
  141. ECD_right_filt = ft_preproc_highpassfilter(ECD_right, Fsample, hp_filter,1);
  142. FDS_right_filt = ft_preproc_highpassfilter(FDS_right, Fsample, hp_filter,1);
  143. ECD_right_filt = ft_preproc_lowpassfilter(ECD_right_filt, Fsample, lp_filter,1);
  144. FDS_right_filt = ft_preproc_lowpassfilter(FDS_right_filt, Fsample, lp_filter,1);
  145. ECD_right_filt = ft_preproc_dftfilter(ECD_right_filt, Fsample);
  146. FDS_right_filt = ft_preproc_dftfilter(FDS_right_filt, Fsample);
  147. %% take away the decay from MEPs
  148. cfg = [];
  149. cfg.start_time = 0.005;
  150. cfg.end_time = 0.15;
  151. cfg.SR = SR;
  152. cfg.sp_trigger = sp_TMS;
  153. [ECD_sweep_cuts_tmp, ECD_sweep_time, ECD_sample_points] = gb_cutting_sweeps(cfg, ECD_right_filt);
  154. [FDS_sweep_cuts_tmp, FDS_sweep_time, FDS_sample_points] = gb_cutting_sweeps(cfg, FDS_right_filt);
  155. %% get MEPs p2p amplitudes
  156. cfg = [];
  157. cfg.start_time = threshold{s}(1);
  158. cfg.end_time = threshold{s}(2);
  159. cfg.SR = SR;
  160. cfg.sp_trigger = sp_TMS;
  161. [ECD_MEP_amplitudes] = gb_MEP_p2p(cfg, ECD_right_filt);
  162. [FDS_MEP_amplitudes] = gb_MEP_p2p(cfg, FDS_right_filt);
  163. %% fit the decay and remove it
  164. cfg = [];
  165. cfg.start_time = -0.15;
  166. cfg.end_time = -0.002;
  167. cfg.SR = SR;
  168. cfg.sp_trigger = sp_TMS;
  169. [ECD_sweep_cuts_bkgrnd_tmp, ECD_sweep_time_bkgrnd, ECD_sample_points_bkgrnd] = gb_cutting_sweeps(cfg, ECD_right_filt)
  170. [FDS_sweep_cuts_bkgrnd_tmp, FDS_sweep_time_bkgrnd, FDS_sample_points_bkgrnd] = gb_cutting_sweeps(cfg, FDS_right_filt)
  171. %%
  172. for x = 1:length(ECD_sweep_cuts_bkgrnd_tmp)
  173. [~, ~, resECD{x}] = fit(ECD_sweep_time_bkgrnd',ECD_sweep_cuts_bkgrnd_tmp{x}','poly3'); % time in ms
  174. ECD_sweep_cuts_bkgrnd{x} =resECD{x}.residuals';
  175. [~, ~, resFDS{x}] = fit(FDS_sweep_time_bkgrnd',FDS_sweep_cuts_bkgrnd_tmp{x}','poly3'); % time in ms
  176. FDS_sweep_cuts_bkgrnd{x} =resFDS{x}.residuals';
  177. end
  178. clear resECD resFDS
  179. %%
  180. for x = 1:length(ECD_sample_points_bkgrnd)
  181. ECD_right_filt(ECD_sample_points_bkgrnd{x}) = ECD_sweep_cuts_bkgrnd{x};
  182. FDS_right_filt(FDS_sample_points_bkgrnd{x}) = FDS_sweep_cuts_bkgrnd{x};
  183. end
  184. %% any contraction befor TMS?
  185. cfg = [];
  186. cfg.start_time = -0.05;
  187. cfg.end_time = -0.002;
  188. cfg.SR = SR;
  189. cfg.sp_trigger = sp_TMS;
  190. cfg.analysis = 'p2p';
  191. [ECD_MEP_bkgrnd] = gb_background_activity(cfg, ECD_right_filt)
  192. [FDS_MEP_bkgrnd] = gb_background_activity(cfg, FDS_right_filt)
  193. %% muscle contraction before TMS?
  194. MEP_excl_ECD_idx = find(ECD_MEP_bkgrnd>200) % microvolt
  195. MEP_excl_FDS_idx = find(FDS_MEP_bkgrnd>200) % microvolt
  196. %% EXCLUDE MEP<50 p2p microVolts
  197. MEP_excl2_ECD_idx = find(ECD_MEP_amplitudes<50) % microvolt
  198. %% EXCLUDE out slow and out fast trials
  199. ECD_MEP_amplitudes(exclude) = nan;
  200. %% exclude trials with background contraction
  201. ECD_MEP_amplitudes(union(MEP_excl_ECD_idx, MEP_excl_FDS_idx)) = nan;
  202. ECD_MEP_amplitudes(MEP_excl2_ECD_idx) = nan;
  203. %% median amplitude based on color trigger
  204. summary.ECD_press{s,t} = nanmedian((ECD_MEP_amplitudes(CF_colors_order == 1)));
  205. summary.ECD_lift{s,t} = nanmedian((ECD_MEP_amplitudes(CF_colors_order == -1)));
  206. %%
  207. cfg = [];
  208. cfg.start_time = -0.015;
  209. cfg.end_time = 0.15;
  210. cfg.SR = SR;
  211. cfg.sp_trigger = sp_TMS;
  212. [bbb.ECD_sweep_PLOT_CHECKS, bbb.ECD_sweep_time_PLOT_CHECKS, bbb.ECD_sample_points_PLOT_CHECKS] = gb_cutting_sweeps(cfg, ECD_right_filt);
  213. %%
  214. trial = (1:length(CF_colors_order))';
  215. subject_rep = repmat(subjects(s), [length(CF_colors_order),1]);
  216. TMS_timings_rep = repmat(TMS_timings(t), [length(CF_colors_order),1]);
  217. subject_table{t} = table(subject_rep,trial, ECD_MEP_amplitudes,...
  218. ECD_MEP_bkgrnd,...
  219. FDS_MEP_bkgrnd,...
  220. TMS_timings_rep,...
  221. CF_colors_order)
  222. clearvars -except s t subjects TMS_timings threshold subject_table general_directory final_table summary
  223. %%
  224. end
  225. final_subject_table = vertcat(subject_table{:})
  226. output_filename = strcat('final_subject_table_',subjects{s},'_' , '.mat');
  227. save(output_filename,'final_subject_table')
  228. final_table = vertcat(final_table, final_subject_table)
  229. clearvars -except s t subjects TMS_timings threshold general_directory final_table summary
  230. end
  231. %%
  232. cd(general_directory)
  233. filename = 'final_table_for_R_10-2500_def_o1_rep.xlsx';
  234. writetable(final_table,filename,'Sheet',1)