ampAtStim.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. function varargout = ampAtStim(files, varargin)
  2. %% Function to obtain the neurons amplitude during a stimulation
  3. % The function calculates the amplitude for both cells during a stimulation
  4. % out of the perspective of the stimulated cell. The stimuli for which the
  5. % calculation should be performed can be set, as well as which cell should
  6. % be the stimulated one. The amplitudes can either be plotted against the
  7. % amplitude of the current pulses (with error bars if needed) or the values
  8. % are returned. The function can handle multiple files at a time.
  9. % -------------------------------------------------------------------------
  10. % Input [additional arguments are positional,
  11. % ----- optional arguments are name-value pairs]
  12. %
  13. % files: ID-number(s) of the '.mat' data-file(s),
  14. % Type: integer or vector
  15. % trials: number(s) of the trials to be analysed, type: integer or
  16. % (additional) vector, default: 'all'
  17. % Filter: select between three filter options:
  18. % (optional) 'hn' applys a hum-noise-remove filter
  19. % 'hnhfn' additonally applys a low pass filter to
  20. % remove high frequency noise
  21. % If not set, it will be set to "no" (default).
  22. % StimCell: set whether to use the T cell ('T') or the interneuron
  23. % (optional) ('INT') as the stimulated cell. Default: 'T'
  24. % ErrorBars: set whether error bars (standard deviation) should be
  25. % (optional) plotted (1 or 0). Default: 1 (yes)
  26. % Stimuli: set for which stimuli the data should be analyzed,
  27. % (optional) Type: integer or vector or 'all', default: 'all'
  28. % ------
  29. % Output
  30. % ------
  31. % varargout{1}: structure(s), containing the calculated values wrapped in
  32. % a cell array, amplitudes for the stimuli are sorted
  33. % from smallest stimulus to the biggest stimulus
  34. % - MeanAmplitudeT/MeanAmplitudeINT: averaged amplitudes
  35. % for the trials where the 'stimCell' was stimulated
  36. % - AmplitudeT/AmplitudeINT: amplitudes for the cells
  37. % for all trials (not averaged)
  38. % - sdT/sdINT: standard deviation for all amplitudes
  39. % over all trials for each stimulus
  40. % - stimulatedT: trials with a stimulated T cell
  41. %
  42. % visually output of the plot(s), if no output variable is set, new
  43. % figure window(s) is/are created
  44. % -------------------------------------------------------------------------
  45. % Program by: Bjarne Schultze [last modified 02.03.2022]
  46. % -------------------------------------------------------------------------
  47. %% Preparations
  48. % parse the user input
  49. [file_nums, trials, filter_choice, tol, ~, stimCell, error_bars, ...
  50. stimuli, ~] = parse_input_peakfun(files, varargin{:});
  51. % preallocate a cell array for the output of the calculated data
  52. fun_output = cell(length(file_nums),1);
  53. % get the necessary data with the extract_data function
  54. recData = extract_data(file_nums, filter_choice);
  55. %% Determine the stimuli onsets
  56. % find the onsets for the positive stimuli
  57. [stim_peaks, stim_locs] = findpeaks(recData{1,1}.stimulus, 10000);
  58. % find the onsets for the negativ stimuli (therefor inverting the
  59. % stimulus data)
  60. [min_stim_peaks, min_stim_locs] = ...
  61. findpeaks(-recData{1,1}.stimulus, 10000, 'MinPeakHeight', 0);
  62. % invert the peak values back
  63. min_stim_peaks = -min_stim_peaks;
  64. % concatenating positive and nagative stimlus peak data
  65. all_stim_locs = [min_stim_locs; stim_locs];
  66. all_stim_peaks = [min_stim_peaks; stim_peaks];
  67. % sort the values for the current in ascending order
  68. [all_stim_peaks_sort, sort_order] = sort(all_stim_peaks);
  69. all_stim_locs_sort = all_stim_locs(sort_order);
  70. % select single stimuli if requested
  71. if ~isequal(stimuli, 'all')
  72. [~, stim_indices] = intersect(all_stim_peaks_sort, stimuli);
  73. all_stim_locs_sort = all_stim_locs_sort(stim_indices);
  74. all_stim_peaks_sort = all_stim_peaks_sort(stim_indices);
  75. end
  76. %% Calculation of the amplitude and its standard deviation
  77. % repeat all calculations for every file requested by the user-input
  78. for file = 1:length(file_nums)
  79. timeVector = recData{file,1}.timeVector;
  80. % length of stimulus (it is the same for all stimuli)
  81. stim_len = 0.5; % in ms
  82. stim_num = length(all_stim_locs_sort); % number of stimuli
  83. trial_num = size(recData{file,1}.recordingT,2);
  84. % pre-define vectors for the results of the calculations
  85. ampT = zeros(stim_num,trial_num);
  86. ampINT = ampT;
  87. % iterate through all trials of an experiment
  88. for exp = 1:trial_num
  89. % iterate through the stimuli calculating amplitude and sd for both
  90. % cells
  91. for stim = 1:length(all_stim_locs_sort)
  92. % select the time frame in which one cell was stimulated +
  93. % tolerance
  94. search_index_amp = timeVector >= all_stim_locs_sort(stim) ...
  95. + tol & timeVector <= all_stim_locs_sort(stim) + stim_len;
  96. % select a time frame before the stimulus onset with the same
  97. % length as the stimulus for a reference
  98. search_index_preamp = timeVector <= all_stim_locs_sort(stim) ...
  99. & timeVector >= all_stim_locs_sort(stim) - stim_len + tol;
  100. % calculate the amplitude of the membrane potential in the T
  101. % cell in relation to the amplitude in the reference time frame
  102. ampT(stim,exp) = ...
  103. mean(recData{file,1}.recordingT(search_index_amp,...
  104. exp)) - mean(recData{file,1}.recordingT(...
  105. search_index_preamp,exp));
  106. % if the amplitude is smaller than 3 sd in the reference time
  107. % frame it is considered as zero
  108. if abs(ampT(stim,exp)) < ...
  109. 3 * std(recData{file,1}.recordingT...
  110. (search_index_preamp,exp))
  111. ampT(stim,exp) = 0;
  112. end
  113. % same calculations for the interneuron
  114. ampINT(stim,exp) = ...
  115. mean(recData{file,1}.recordingINT(search_index_amp,...
  116. exp)) - mean(recData{file,1}.recordingINT(...
  117. search_index_preamp,exp));
  118. if abs(ampINT(stim,exp)) < ...
  119. 3 * std(recData{file,1}.recordingINT...
  120. (search_index_preamp,exp))
  121. ampINT(stim,exp) = 0;
  122. end
  123. end
  124. end
  125. %% Averaging of the amplitudes
  126. % pre-define vectors for the mean and standard deviation of the
  127. % amplitudes over all trials
  128. m_ampT = zeros(stim_num,1); m_ampINT = zeros(stim_num,1);
  129. sd_ampT = zeros(stim_num,1); sd_ampINT = zeros(stim_num,1);
  130. % distinguish which cell should be the stimulated
  131. if isequal(stimCell, "INT")
  132. if isequal(trials,'all')
  133. stimulated = setdiff(1:trial_num, ...
  134. recData{file,1}.stimulatedT(:), 'stable');
  135. else
  136. stimulated = setdiff(trials, ...
  137. recData{file,1}.stimulatedT(:), 'stable');
  138. end
  139. plot_title = sprintf("File: %02.f - Interneuron stimulated", ...
  140. file_nums(file));
  141. else
  142. stimulated = recData{file,1}.stimulatedT;
  143. plot_title = sprintf("File: %02.f - T cell stimulated", ...
  144. file_nums(file));
  145. if ~isequal(trials,'all')
  146. stimulated = intersect(trials,1:trial_num);
  147. end
  148. end
  149. % calculate the average and standard deviation of the amplitude for
  150. % each stimulus
  151. for stim = 1:stim_num
  152. % mean and sd of trials where the T-cell was stimulated
  153. m_ampT(stim) = mean(ampT(stim,stimulated));
  154. sd_ampT(stim) = std(ampT(stim,stimulated));
  155. % mean and sd for the interneurons
  156. m_ampINT(stim) = mean(ampINT(stim,stimulated));
  157. sd_ampINT(stim) = std(ampINT(stim,stimulated));
  158. end
  159. % define a threshold/minimum mean amplitude of 0.1 mV, everything
  160. % beneath is set to zero
  161. under_thresholdT = abs(m_ampT) < 0.1;
  162. m_ampT(under_thresholdT) = 0; sd_ampT(under_thresholdT) = 0;
  163. under_thresholdINT = abs(m_ampINT) < 0.1;
  164. m_ampINT(under_thresholdINT) = 0; sd_ampINT(under_thresholdINT) = 0;
  165. %% Return and/or plot the results
  166. % if one output is requested the plot will not be shown
  167. if nargout > 0
  168. % gather the calculated data in a structure for the output
  169. output_data = struct('MeanAmplitudeT',m_ampT, ...
  170. 'MeanAmplitudeINT', m_ampINT, ...
  171. 'AmplitudeT', ampT, ...
  172. 'AmplitudeINT', ampINT, ...
  173. 'sdT', sd_ampT, 'sdINT', sd_ampINT, ...
  174. 'stimulatedT', recData{file,1}.stimulatedT);
  175. fun_output{file,1} = output_data;
  176. % assign the calculated data to output variable 1
  177. varargout{1} = fun_output;
  178. else
  179. % create a new figure window for a visible plot
  180. figure();
  181. end
  182. if isequal(nargout, 0) || nargout > 1
  183. % plot the results either with or without error bars
  184. if isequal(error_bars, 1)
  185. errorbar(all_stim_peaks_sort, m_ampT, sd_ampT);
  186. axis padded
  187. hold on;
  188. errorbar(all_stim_peaks_sort, m_ampINT, sd_ampINT);
  189. hold off;
  190. title(plot_title);
  191. xlabel("current [nA]"); ylabel("amplitude [mV]");
  192. legend("T-cell", "Interneuron", 'Location', 'best', ...
  193. 'AutoUpdate', 'off');
  194. yline(0, 'Color', [0.6,0.6,0.6]);
  195. subtitle("- error bars representing standard deviation between trials -");
  196. else
  197. plot(all_stim_peaks_sort, m_ampT, 'o-',...
  198. all_stim_peaks_sort, m_ampINT, 'o-', 'MarkerSize', 4);
  199. axis padded
  200. title(plot_title);
  201. xlabel("stimulation current [nA]"); ylabel("amplitude [mV]");
  202. legend("T-cell", "Interneuron", 'Location', 'best',...
  203. 'AutoUpdate', 'off');
  204. yline(0, 'Color', [0.6,0.6,0.6]);
  205. end
  206. end
  207. % end of the iteration through the single files
  208. end
  209. %% end of function definition
  210. end