plot_FI.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function plot_FI(files, varargin)
  2. %% Create a classic FI curve (with spike count instead of spike frequency)
  3. % Create a plot which shows the spike count of the T cell and the
  4. % interneuron during a stimulation plotted against the stimulation current.
  5. % -------------------------------------------------------------------------
  6. % Input [additional arguments are positional,
  7. % ----- optional arguments are name-value pairs]
  8. %
  9. % files: ID-number(s) of the '.mat' data-file(s),
  10. % Type: integer or vector
  11. % trials: number(s) of the trials to be analysed, type: integer or
  12. % (additional) vector, default: 'all'
  13. % Filter: select between three filter options:
  14. % (optional) 'hn' applys a hum-noise-remove filter
  15. % 'hnhfn' additonally applys a low pass filter to
  16. % remove high frequency noise
  17. % If not set, no filter will be applyed (default).
  18. % Tolerance: set tolerance in seconds that is added to the stimulus
  19. % (optional) time frame on both ends. Default: 0.05 s)
  20. % MinPeakPromT: set the value for 'MinPeakProminence' which is used in
  21. % (optional) the 'findpeaks' function to find T-cell peaks.
  22. % Default: 17
  23. % StimCell: set whether the data for the stimulated T-cell or the
  24. % (optional) stimulated interneuron should be used.
  25. % 'INT' for the interneuron
  26. % 'T' for the T-cell (default)
  27. % ErrorBars: set whether error bars (standard deviation)
  28. % (optional) are plotted (1 or 0). Default: 1 (yes)
  29. % ------
  30. % Output
  31. % ------
  32. % visually output of the plot(s)
  33. % -------------------------------------------------------------------------
  34. % Program by: Bjarne Schultze [last modified 02.03.2022]
  35. % -------------------------------------------------------------------------
  36. %% Preperations
  37. % parse input using a function (checking for vaild inputs included)
  38. [file_num, trials, filter_choice, tol, MinPeakPromT, stimCell, ...
  39. error_bars, ~,~] = parse_input_peakfun(files, varargin{:});
  40. % access the data and applying filters if required
  41. recData = extract_data(file_num, filter_choice);
  42. % iterate through all requested files
  43. for file = 1:length(files)
  44. % get the number of trials
  45. if isequal(trials,'all')
  46. trials = 1:size(recData{file,1}.recordingT,2);
  47. end
  48. % differentiate between a stimulated T-cell or interneuron
  49. if isequal(stimCell, "INT")
  50. stimulated = setdiff(trials, recData{file,1}.stimulatedT(:),...
  51. 'stable');
  52. plot_title = sprintf("File: %02.f - Interneuron stimulated", ...
  53. file_num(file));
  54. else
  55. stimulated = recData{file,1}.stimulatedT(:);
  56. plot_title = sprintf("File: %02.f - T cell stimulated", ...
  57. file_num(file));
  58. if ~isequal(trials,'all')
  59. stimulated = intersect(stimulated,trials);
  60. end
  61. end
  62. %% Calculation of spiking frequencies
  63. % find the points, where the positive stimuli start
  64. [stim_peaks, stim_locs] = findpeaks(recData{file,1}.stimulus, 10000, ...
  65. 'SortStr', 'ascend');
  66. % length of a stimulus (the same for each stimulus)
  67. stim_len = 0.5;
  68. % pre-define vectors for the calculation results
  69. peak_countT = zeros(length(stim_peaks), length(stimulated));
  70. peak_countINT = peak_countT;
  71. % iterate through the trails calculating the spiking frequency
  72. for exp = 1:length(stimulated)
  73. % determine the locations of spikes in T cell and interneuron
  74. [~, spike_locsT] = ...
  75. findpeaks(recData{file,1}.recordingT(:,stimulated(exp)), ...
  76. 10000, 'MinPeakProminence', MinPeakPromT);
  77. [~, spike_locsINT] = ...
  78. findIntSpikes(recData{file,1}.recordingINT(:,stimulated(exp)));
  79. spike_locsINT = spike_locsINT/10000;
  80. % iterate through the stimuli counting the spikes for both cells
  81. for stimulus = 1:length(stim_peaks)
  82. % select the time frame (plus tolerance) where one cell was stimulated
  83. search_indexT = spike_locsT >= stim_locs(stimulus)- tol & ...
  84. spike_locsT <= stim_locs(stimulus) + ...
  85. stim_len + tol;
  86. % calculate the spiking frequency in the stimulus time frame
  87. peak_countT(stimulus,exp) = ...
  88. length(spike_locsT(search_indexT));
  89. % the same calculations for the interneuron
  90. search_indexINT = spike_locsINT >= stim_locs(stimulus) - ...
  91. tol & spike_locsINT <= stim_locs(stimulus) + ...
  92. stim_len + tol;
  93. peak_countINT(stimulus,exp) = ...
  94. length(spike_locsINT(search_indexINT));
  95. end
  96. end
  97. %% Averaging of the spiking frequencies
  98. % find the average and standard deviation of the spiking frequency for
  99. % each stimulus
  100. m_peak_countT = mean(peak_countT, 2);
  101. sd_peak_countT = std(peak_countT, 0, 2);
  102. m_peak_countINT = mean(peak_countINT, 2);
  103. sd_peak_countINT = std(peak_countINT, 0, 2);
  104. %% Plotting the results
  105. % creating a figure handle
  106. figure();
  107. % Plotting the spiking frequency against the current
  108. if isequal(error_bars, 1)
  109. % plot with error bars
  110. errorbar(stim_peaks, m_peak_countT, sd_peak_countT);
  111. axis padded;
  112. hold on;
  113. errorbar(stim_peaks, m_peak_countINT, sd_peak_countINT);
  114. hold off;
  115. title(plot_title);
  116. xlabel("current [nA]"); ylabel("spike count");
  117. legend("T cell", "Interneuron", 'Location', 'northwest', ...
  118. 'Autoupdate', 'off');
  119. subtitle("- error bars representing standard deviation " + ...
  120. "between trials -");
  121. else
  122. % plot without error bars
  123. plot(stim_peaks, m_peak_countT, 'ro-', ...
  124. stim_peaks, m_peak_countINT, 'bo-','MarkerSize', 4);
  125. axis padded;
  126. % axis padded
  127. legend("T cell", "Interneuron", 'Location','northwest',...
  128. 'AutoUpdate','off');
  129. title(plot_title);
  130. xlabel("stimulation current [nA]"); ylabel("spike count");
  131. end
  132. % add a referece line at y=0
  133. yline(0, 'Color', [0.6 0.6 0.6])
  134. % end of file iteration
  135. end
  136. % end of function definition
  137. end