spikeTrigAvg_presyn.m 8.0 KB

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