isi.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. function[N,B,E] = isi(data,T,err,Nbins,plt)
  2. % Calculate the inter-spike-interval histogram
  3. % Usage: [N,B,E] = isi(data,T,err,Nbins,plt)
  4. %
  5. % Input:
  6. % Note that all times have to be consistent.
  7. %
  8. % data - structure array of spike times (required)
  9. % T - time interval of interest (default all)
  10. % err - 0 for no error bars, 1 for jackknife errors
  11. %
  12. % Nbins - number of bins in the isi
  13. %
  14. % Output:
  15. %
  16. % N - count in bins
  17. % B - bin centres
  18. % E - errorbar (this is 2 sig deviation
  19. % calculated using a jackknife over trials)
  20. if nargin < 1; error('I need data!'); end
  21. data=padNaN(data); % create a zero padded data matrix from input structural array
  22. data=data'; % transposes data to get it in a form compatible with Murray's routine
  23. if nargin < 2; T = [min(data(:,1)) max(max(data))]; end
  24. if nargin < 3; err = 0;end
  25. if nargin < 4; Nbins = -1; end
  26. if nargin < 5; plt = 'r'; end
  27. if isempty(T); T = [min(min(data)) max(max(data))]; end
  28. if isempty(err); err = 0;end
  29. if isempty(Nbins); Nbins = -1; end
  30. if isempty(plt); plt = 'r'; end
  31. % get the number of intervals in each trial and the indices of spike times
  32. % that are kept
  33. NT = length(data(1,:)); % number of trials
  34. NI=zeros(1,NT);
  35. index(1:NT)=struct('keep',[]);
  36. for n=1:NT
  37. indx = find(data(:,n) >= T(1) & data(:,n) <= T(2) ...
  38. & ~isnan(data(:,n)));
  39. if isempty(indx)
  40. NI(n) = 0;
  41. else
  42. NI(n) = length(indx)-1;
  43. index(n).keep=indx;
  44. end
  45. end
  46. % calculate intervals...
  47. I = zeros(NT,max(NI));
  48. IT = [];
  49. for n=1:NT
  50. I(n,1:NI(n)) = diff(data(index(n).keep,n));
  51. IT = [IT I(n,1:NI(n))];
  52. end
  53. Mx = max(IT);
  54. if Nbins == -1
  55. Nbins = floor(sum(NI)/30);
  56. Med = median(IT);
  57. Nbins = max(floor(Nbins*Mx/Med),10);
  58. end
  59. B = linspace(0,Mx,Nbins);
  60. N = zeros(NT,Nbins);
  61. for n=1:NT
  62. N(n,:) = hist(I(n,1:NI(n)),B);
  63. end
  64. % answer...
  65. if NT > 1;Ns = sum(N)/NT;else Ns = N;end
  66. if ~strcmp(plt,'n')
  67. bar(B,NT*Ns);
  68. end
  69. % Jackknife iver trials to estimate std...
  70. if NT > 4 && err == 1
  71. MN = 0;
  72. SN = 0;
  73. for n=1:NT
  74. JK = (NT*Ns - N(n,:))/(NT-1);
  75. MN = MN + JK;
  76. SN = SN + JK.^2;
  77. end
  78. MN = MN/NT;
  79. SN = SN/NT;
  80. E = sqrt((NT-1)*(SN - MN.^2));
  81. if ~strcmp(plt,'n')
  82. hold on
  83. errorbar(B,NT*Ns,NT*2*E,'r-')
  84. hold off
  85. end
  86. end
  87. N = NT*Ns;