spikeTrigAvg_postsyn_funtest.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. function spikeTrigAvg_postsyn_funtest(file, varargin)
  2. %% TEST-FUNCTION: Visual verification of the calculations of spikeTrigAvg_postsyn
  3. % In addition to the original function this function plots the data of each
  4. % trial and marks the times for spikes in the other cell as well as the
  5. % data gathered for the calculations. Green dot: start of time frame for
  6. % data selection. Orange line: selected data. Grey line: time of a spike in
  7. % the other cell.
  8. % Only one file is ananlysed at a time and no trials can be excluded.
  9. % -------------------------------------------------------------------------
  10. % Input [optional arguments are name-value pairs]
  11. % -----
  12. %
  13. % file: number of the '.mat' file in the directory,
  14. % type: integer
  15. % Filter: select between three filter options:
  16. % (optional) 'hn' applys a hum-noise-remove filter
  17. % 'hnhfn' additonally applys a low pass filter to
  18. % remove high frequency noise
  19. % If not set, no filter will be applyed (default).
  20. % MinPeakPromT: set the value for 'MinPeakProminence' which is used in
  21. % (optional) the 'findpeaks' function to find T cell peaks.
  22. % Type: float. Default: 17
  23. % StimCell: set whether the data for a stimulated T cell or a
  24. % (optional) stimulated interneuron should be used.
  25. % 'INT' for the interneuron
  26. % 'T' for the T cell (default)
  27. % ------
  28. % Output
  29. % ------
  30. % visually output of the plots (one plot per trial + one for the result)
  31. % -------------------------------------------------------------------------
  32. % Program by: Bjarne Schultze [last modified 02.03.2022]
  33. % -------------------------------------------------------------------------
  34. %% Preperations
  35. % parse the user input
  36. [file_num, ~, filter_choice, ~, MinPeakPromT, ...
  37. stimCell, ~,~,~] = parse_input_peakfun(file, varargin{:});
  38. % extract the requested data
  39. dataOut = extract_data(file_num, filter_choice);
  40. recData = dataOut{1,1};
  41. % select whether or not to plot the rebounce spikes seperately
  42. % ('Rebounce' for yes)
  43. select_spikes = "Rebounce";
  44. % get the number or trials
  45. trial_count = size(recData.recordingT,2);
  46. % set how long the time frame should be to gather the triggered data
  47. trig_len = 0.03; % in seconds
  48. trig_len_dp = trig_len * 10000; % in datapoints
  49. % distinguish between a stimulated T-cell and a stimulated interneuron
  50. if isequal(stimCell, "INT")
  51. % get the columns with data for a stimulated interneuron
  52. stimulated = setdiff(1:trial_count, recData.stimulatedT(:), 'stable');
  53. % set an appropriate plot title
  54. plot_title = ...
  55. sprintf("Postsynaptic Spike Response - File: %02.f - " + ...
  56. "Interneuron stimulated", file_num);
  57. % assign the data to working variables
  58. recDataNSTIM = recData.recordingT(:,stimulated);
  59. recDataSTIM = recData.recordingINT(:,stimulated);
  60. else
  61. % get the columns with data for a stimulated T-cell
  62. stimulated = recData.stimulatedT(:);
  63. % set an appropriate plot title
  64. plot_title = ...
  65. sprintf("Postsynaptic Spike Response - File: %02.f - " + ...
  66. "T-cell stimulated", file_num);
  67. % assign the data to working variables
  68. recDataSTIM = recData.recordingT(:,stimulated);
  69. recDataNSTIM = recData.recordingINT(:,stimulated);
  70. MinPeakPromSTIM = MinPeakPromT;
  71. end
  72. %% Main calculations
  73. % pre-define a variable to sum up the triggered membrane potentials
  74. triggered_sum = 0; rbs_triggered_sum = 0;
  75. spike_counter = 0; rbSpike_counter = 0;
  76. % iterate through the trials
  77. for exp = 1:length(stimulated)
  78. % search for spikes in the stimulated cell
  79. if isequal(stimCell, 'INT')
  80. [~,spike_indSTIM_all] = findIntSpikes(recDataSTIM(:,exp));
  81. else
  82. [~, spike_indSTIM_all] = findpeaks(recDataSTIM(:,exp), ...
  83. 'MinPeakProminence', MinPeakPromSTIM);
  84. end
  85. % exclude spikes at the very beginning and end to ensure a complete
  86. % time frame for the triggered data
  87. spike_indSTIM_sel = spike_indSTIM_all > 50 & ...
  88. spike_indSTIM_all < (size(recDataSTIM,1) - trig_len_dp);
  89. spike_indSTIM = spike_indSTIM_all(spike_indSTIM_sel);
  90. % if no spikes are found the next trial is examined
  91. if isempty(spike_indSTIM)
  92. continue
  93. else
  94. % -----------------------------------------------------------------
  95. % TEST PART:
  96. % plot the spike times over the membrane potentail of the
  97. % not-stimulated cell
  98. figure();
  99. plot(recData.timeVector,recDataNSTIM(:,exp), '-');
  100. xline((spike_indSTIM./10000), 'Color', [0.3,0.3,0.3]);
  101. title(plot_title); subtitle(sprintf("Trial: %02.f",stimulated(exp)));
  102. xlabel("time [s]"); ylabel("membrane potential [mV]");
  103. % -----------------------------------------------------------------
  104. % select the membrane potentail of the not-stimulated cell in
  105. % an index frame of the length 'trig_len_dp' datapoints for
  106. % each spike of the stimulated cell
  107. for spike = 1:length(spike_indSTIM)
  108. % select the data (triggered by a spike)
  109. triggered_data = ...
  110. recDataNSTIM((spike_indSTIM(spike)-50):...
  111. (spike_indSTIM(spike) + trig_len_dp - 1), exp);
  112. % -------------------------------------------------------------
  113. % TEST PART:
  114. % select an apropriate time vector for this data
  115. triggered_time = recData.timeVector((spike_indSTIM(spike)-50):...
  116. (spike_indSTIM(spike) + trig_len_dp - 1), 1);
  117. % add the selected data to the plot
  118. hold on;
  119. plot(triggered_time, triggered_data, '-', 'Color', ...
  120. [0.85,0.33,0.1]);
  121. plot(triggered_time(1), triggered_data(1), 'o', 'Color',...
  122. [0.4,0.83,0.07])
  123. hold off;
  124. % -------------------------------------------------------------
  125. % sum up the data
  126. triggered_sum = triggered_sum + triggered_data;
  127. end
  128. % count the spikes
  129. spike_counter = spike_counter + spike;
  130. % -----------------------------------------------------------------
  131. % TEST PART:
  132. % only analyse the data for rebounce spikes
  133. if isequal(select_spikes, "Rebounce")
  134. % select the spikes that are not on top of a passive response
  135. % (rebounce spikes)
  136. rebSpikes_search = ...
  137. (spike_indSTIM < 42000 & spike_indSTIM > 35000) | ...
  138. (spike_indSTIM < 122000 & spike_indSTIM > 115000) | ...
  139. (spike_indSTIM < 242000 & spike_indSTIM > 235000);
  140. rebounceSpikes = spike_indSTIM(rebSpikes_search);
  141. if isempty(rebounceSpikes)
  142. continue
  143. else
  144. % select the membrane potentail of the not-stimulated cell
  145. % in an index frame of the length 'trig_len_dp' datapoints
  146. % (-1 to get the requested search frame) for each spike of
  147. % the stimulated cell
  148. for rbSpike = 1:length(rebounceSpikes)
  149. % select the data (triggered by a spike)
  150. rbs_triggered_data = ...
  151. recDataNSTIM((rebounceSpikes(rbSpike)-50):...
  152. (rebounceSpikes(rbSpike) + trig_len_dp - 1), exp);
  153. % select an apropriate time vector for this data
  154. rb_triggered_time = ...
  155. recData.timeVector((rebounceSpikes(rbSpike)-50):...
  156. (rebounceSpikes(rbSpike) + ...
  157. trig_len_dp - 1), 1);
  158. % add the selected data to the plot
  159. hold on;
  160. plot(rb_triggered_time, rbs_triggered_data, '-m');
  161. hold off;
  162. % sum up the data
  163. rbs_triggered_sum = rbs_triggered_sum + ...
  164. rbs_triggered_data;
  165. end
  166. % count the spikes
  167. rbSpike_counter = rbSpike_counter + rbSpike;
  168. end
  169. end
  170. % -----------------------------------------------------------------
  171. end
  172. end
  173. % feedback about the number of spikes used for the calculations
  174. fprintf("\nSpikes in total: %0.f\nthereof rebounce spikes: %0.f\n",...
  175. spike_counter, rbSpike_counter);
  176. % calculate the averaged postsynaptic membrane potential
  177. afterSpkAvg = triggered_sum ./ spike_counter;
  178. afterRbSpkAvg = rbs_triggered_sum ./ rbSpike_counter;
  179. %% Plotting the result
  180. % create a time vector for the selected data
  181. timeVector = [sort(-recData.timeVector(1:50));...
  182. recData.timeVector(1:trig_len_dp)];
  183. timeVector = timeVector * 1000; % in ms
  184. % plot the averaged postsynaptic membrane potential
  185. figure();
  186. plot(timeVector, afterSpkAvg, timeVector, afterRbSpkAvg);
  187. legend("All spikes", "Only rebounce spikes", 'AutoUpdate', 'off');
  188. title(plot_title);
  189. xline(0, 'Color', [0.6,0.6,0.6]);
  190. xlabel("time after spike [ms]");
  191. ylabel("averaged membrane potentail [mV]");
  192. % end of function definition
  193. end