binspikes.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function [dN,t]=binspikes(data,Fs,t)
  2. % bin spikes at a specified frequency sampling i.e. sampling rate 1/sampling
  3. % eg: 1ms accuracy use sampling = 1000
  4. % Usage: [dN,t]=binspikes(data,Fs,t)
  5. % Inputs:
  6. % data (data as a structure array of spike times; or as a single
  7. % vector of spike times)
  8. % Fs (binning frequency)
  9. % t (the minimum and maximum times to be used to form the bins - [mint maxt]
  10. % - optional. Default use the spike times themselves to
  11. % determine the location of the bins.
  12. % Note: the times in data can be in any units. However, it is important
  13. % that all units are chosen consistently. So, if spike times are in secs,
  14. % Fs and t (if present) have to be in Hz and secs respectively. If spike
  15. % times are in number of samples, Fs has to be 1, and t has to be in number
  16. % of samples.
  17. % Outputs:
  18. % dN (output binned spike counts as a matrix defined on bins starting with the
  19. % earliest spike across all channels and ending with the latest spike)
  20. % t (lower limit of each bin)
  21. if nargin < 2; error('Need at least two input arguments'); end;
  22. dt=1/Fs;
  23. dtmp='';
  24. if isstruct(data);
  25. C=length(data);
  26. fnames=fieldnames(data);
  27. if nargin <3 || isempty(t);
  28. mintime=zeros(1,C);
  29. maxtime=zeros(1,C);
  30. for ch=1:C
  31. eval(['dtmp=data(ch).' fnames{1} ';'])
  32. mintime(ch)=min(dtmp);
  33. maxtime(ch)=max(dtmp);
  34. end
  35. mintime=min(mintime);
  36. maxtime=max(maxtime);
  37. else
  38. % maxtimech=zeros(1,C);
  39. % for ch=1:C
  40. % eval(['dtmp=data(ch).' fnames{1} ';'])
  41. % % mintimech(ch)=min(dtmp);
  42. % maxtimech(ch)=max(dtmp);
  43. % end
  44. mintime=t(1);
  45. maxtime=t(end);
  46. % mintimech=min(mintimech);
  47. % maxtimech=max(maxtimech);
  48. % if maxtimech > max(t); t=[t maxtimech+dt]; end;
  49. end
  50. t=linspace(mintime,maxtime,1+(maxtime-mintime)/dt);
  51. for ch=1:C;
  52. eval(['dtmp=data(ch).' fnames{1} ';'])
  53. x=histc(dtmp,t);
  54. dN(:,ch)=x(:);
  55. end
  56. else
  57. dtmp=data;
  58. if nargin < 3;
  59. mintime=min(dtmp);
  60. maxtime=max(dtmp);
  61. else
  62. mintime=t(1);
  63. maxtime=t(end);
  64. end
  65. t=linspace(mintime,maxtime,1+(maxtime-mintime)/dt);
  66. if max(dtmp)>max(t); t=[t maxtime+dt]; end;
  67. x=histc(dtmp,t);
  68. dN=x(:);
  69. end