spikeTrigAvg_presyn_funtest.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. function spikeTrigAvg_presyn_funtest(files, varargin)
  2. %% Calculate and plot the spike triggered average
  3. % This function is identical to 'spikeTrigAvg' in most respects, but it
  4. % additionally creates a plot for every trial showing the data that is used
  5. % for the calculations. These plots can facilitate the verification of the
  6. % results. The code sections that differ from the main function are marked
  7. % as such.
  8. % -------------------------------------------------------------------------
  9. % Input [additional arguments are positional,
  10. % ----- optional arguments are name-value pairs]
  11. %
  12. % files: ID(s) of the datasets in the 'CleanDatasets' directory,
  13. % type: integer or vector
  14. % trials: number(s) of the trials to be analysed, type: integer
  15. % (additional) or vector, default: 'all'
  16. % Filter: select between three filter options:
  17. % (optional) 'hn' applys a hum-noise-remove filter
  18. % 'hnhfn' additonally applys a low pass filter to
  19. % remove high frequency noise
  20. % If not set, no filter will be applyed (default).
  21. % Tolerance: set tolerance in seconds that is added to the stimulus
  22. % (optional) time frame on both ends. Default: 0.05 s)
  23. % MinPeakPromT: set the value for 'MinPeakProminence' which is used in
  24. % (optional) the 'findpeaks' function to find T-cell peaks.
  25. % Default: 17
  26. % StimCell: set whether the data for the stimulated T-cell or the
  27. % (optional) stimulated interneuron should be used.
  28. % 'INT' for the interneuron
  29. % 'T' for the T-cell (default)
  30. % ------
  31. % Output
  32. % ------
  33. % visually output of the plot(s)
  34. % -------------------------------------------------------------------------
  35. % Program by: Bjarne Schultze [last modified 07.10.2021]
  36. % -------------------------------------------------------------------------
  37. %% Preperations
  38. % parse the user input
  39. [file_num, trials, filter_choice, tol, MinPeakPromT, stimCell, ...
  40. ~,~,~] = parse_input_peakfun(files, varargin{:});
  41. % extract the requested data
  42. recData = extract_data(file_num, filter_choice);
  43. % length of a stimulus (the same for each stimulus)
  44. stim_len = 0.5;
  45. % set the sampling rate
  46. samp_rate = 10000;
  47. % set how long the time frame should be to gather the triggering data
  48. trig_len = 0.03; % in seconds
  49. trig_len_dp = trig_len * samp_rate; % in datapoints
  50. % find the points, where the positive stimuli start
  51. [~, stim_locs] = findpeaks(recData{1,1}.stimulus, samp_rate, ...
  52. 'SortStr', 'ascend');
  53. % get the amplitudes for the cells in the stimulus time frame
  54. amplitudes = ampAtStim(file_num);
  55. for file = 1:length(file_num)
  56. % get the number or trials
  57. trial_num = size(recData{file,1}.recordingT,2);
  58. % distinguish between a stimulated T-cell and a stimulated interneuron
  59. if isequal(stimCell, "INT")
  60. % get the columns with data for a stimulated interneuron
  61. stimulated = setdiff(1:trial_num, recData{file,1}.stimulatedT(:), ...
  62. 'stable');
  63. % set an appropriate plot title
  64. plot_title = ...
  65. sprintf("Spike Triggered Average - File: %02.f - " + ...
  66. "Interneuron stimulated", file_num(file));
  67. % assign the data to working variables
  68. recordingDataNSTIM = recData{file,1}.recordingT(:,stimulated);
  69. recordingDataSTIM = recData{file,1}.recordingINT(:,stimulated);
  70. MinPeakPromNSTIM = MinPeakPromT;
  71. ampSTIM = amplitudes{file,1}.AmplitudeINT(:,stimulated);
  72. else
  73. % get the columns with data for a stimulated T-cell
  74. stimulated = recData{file,1}.stimulatedT(:);
  75. % set an appropriate plot title
  76. plot_title = ...
  77. sprintf("Spike Triggered Average - File: %02.f - T cell " + ...
  78. "stimulated", file_num(file));
  79. % assign the data to working variables
  80. recordingDataSTIM = recData{file,1}.recordingT(:,stimulated);
  81. recordingDataNSTIM = recData{file,1}.recordingINT(:,stimulated);
  82. ampSTIM = amplitudes{file,1}.AmplitudeT(:,stimulated);
  83. end
  84. if ~isequal(trials,'all')
  85. % select single trials if requested
  86. [~,experiments,~] = intersect(stimulated,trials);
  87. else
  88. experiments = 1:length(stimulated);
  89. end
  90. %% Main calculations
  91. % pre-define a variable to sum up the triggering membrane potentials
  92. trigger_sum = 0;
  93. spike_counter = 0;
  94. % iterate through the trials
  95. for exp = experiments
  96. % search for spikes in the not-stimulated cell
  97. if isequal(stimCell,'T')
  98. % use custom function to find interneuron spikes
  99. [spike_peaksNSTIM, spike_locsNSTIM] = ...
  100. findIntSpikes(recordingDataNSTIM(:,exp));
  101. spike_locsNSTIM = spike_locsNSTIM/samp_rate;
  102. else
  103. % use standard findpeaks function for T cell spikes
  104. [spike_peaksNSTIM, spike_locsNSTIM] = ...
  105. findpeaks(recordingDataNSTIM(:,exp), ...
  106. samp_rate, 'MinPeakProminence', MinPeakPromNSTIM);
  107. end
  108. % -----------------------------------------------------------------
  109. % TEST PART:
  110. figure();
  111. plot(recData{file,1}.timeVector, recordingDataSTIM(:,exp), ...
  112. 'Color', [0,0.45,0.75]);
  113. xlabel("time [s]"); ylabel("membrane potentail [mV]");
  114. title(sprintf("%s - Trial %02.f", plot_title, stimulated(exp)));
  115. hold on;
  116. plot(recData{file,1}.timeVector, recordingDataNSTIM(:,exp), ...
  117. 'Color', [1,0.33,0]);
  118. hold off;
  119. % -----------------------------------------------------------------
  120. for stimulus = 1:length(stim_locs)
  121. % search for spikes in the stimulus time frame (plus tolerance)
  122. % Notice: Not using the whole stimulus time frame to not take
  123. % the increase of membrane potentail at the stimulus
  124. % onset into account.
  125. search_indexNSTIM = spike_locsNSTIM >= stim_locs(stimulus) + ...
  126. tol & spike_locsNSTIM <= stim_locs(stimulus) + stim_len;
  127. % select the spikes in the stimulus time frame
  128. spike_locsNSTIM_select = spike_locsNSTIM(search_indexNSTIM);
  129. if isempty(spike_locsNSTIM_select)
  130. continue
  131. else
  132. % iterate through the spikes creating a search box for each
  133. for spike = 1:length(spike_locsNSTIM_select)
  134. % get the index of the spike in the time vector
  135. spike_ind = uint32(spike_locsNSTIM_select(spike) * ...
  136. samp_rate);
  137. % create the index frame of the length of 'trig_len_dp'
  138. % datapoints to select the data (+1 to get the requested
  139. % search frame)
  140. search_box = (spike_ind - trig_len_dp + 1):spike_ind;
  141. % select the data of the stimulated cell and subtract the
  142. % passive response (stimulus+3, because here only the
  143. % positive stimuli are examined)
  144. trigger_data = recordingDataSTIM(search_box,exp) ...
  145. - ampSTIM(stimulus+3,exp);
  146. % -----------------------------------------------------
  147. % TEST PART:
  148. trigger_data_time = ...
  149. recData{file,1}.timeVector(search_box);
  150. hold on;
  151. spike_peaksNSTIM_select = ...
  152. spike_peaksNSTIM(search_indexNSTIM);
  153. plot(spike_locsNSTIM_select(spike), ...
  154. spike_peaksNSTIM_select(spike), ...
  155. 'o', 'Color',[0.8,0.3,0]);
  156. text(spike_locsNSTIM_select(spike), ...
  157. spike_peaksNSTIM_select(spike),...
  158. num2str(spike));
  159. plot(trigger_data_time, trigger_data, 'Color', [0,0.64,0]);
  160. text(trigger_data_time(1), trigger_data(1), ...
  161. num2str(spike));
  162. hold off;
  163. % -----------------------------------------------------
  164. % select the data of the stimulated cell and sum it up
  165. trigger_sum = trigger_sum + trigger_data;
  166. end
  167. end
  168. % add the number of spikes to the spike counter
  169. spike_counter = spike_counter + spike;
  170. % ---------------------------------------------------------------------
  171. % TEST PART:
  172. legend('Stimulated cell', 'Not stimulated cell',...
  173. 'Used spikes','Data used as trigger data',...
  174. 'AutoUpdate', 'off');
  175. % ---------------------------------------------------------------------
  176. end
  177. end
  178. % calculate the spike triggered average
  179. spTrAvg = trigger_sum ./ spike_counter;
  180. % give back the number of spikes used for the calculations
  181. fprintf("Number of spikes used: %0.f\n\n", spike_counter);
  182. %% Plotting the result
  183. if spike_counter > 0
  184. % create a time vector for the trigger data
  185. timeVector = ...
  186. sort(-recData{file,1}.timeVector(1:length(trigger_sum)));
  187. timeVector = timeVector * 1000; % in ms
  188. % plot the spike triggered average
  189. figure();
  190. plot(timeVector, spTrAvg);
  191. title(plot_title);
  192. xlabel("time before spike [ms]");
  193. ylabel("average trigger stimulus [mV]")
  194. else
  195. fprintf("No spikes were found for the not stimulated cell in " + ...
  196. "dataset %g!\n", file_num(file));
  197. end
  198. % end of iteration through the files
  199. end
  200. % end of function definition
  201. end