tsraster.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function [y,varargout]=tsraster(r,t,varargin)
  2. % [y,x]=tsraster(r,t,['pre',2,'post',2,'bin_size',0.01)
  3. % Computes the cross-correlation of 2 point processes, r(ef) and t(arget)
  4. % using a time w(indow) in sec.
  5. % either provide one window input and it will use +/- w or provide pre and
  6. % post.
  7. % r and t should be vectors of timestamps in sec
  8. %
  9. % bins are LEFT-ALIGNED. Meaning the bin at x==0 are the spikes from 0 to
  10. % bin_size
  11. %
  12. % note: instead of a list of option one can pass options as a struct:
  13. % opt.pre=2; opt.post=4; .....
  14. %% SETUP
  15. post=2;
  16. pre=2;
  17. bin_size=0.01;
  18. utils.overridedefaults(who, varargin);
  19. % if either are empty return empty
  20. if isempty(r)
  21. y=[];
  22. varargout{1}=[];
  23. return
  24. end
  25. if isempty(t)
  26. t=r(1)-pre-bin_size-1;
  27. end
  28. % if w is zero or negative, complain
  29. if (post+pre)<=0
  30. y=[];
  31. error('window is negative in size')
  32. return
  33. end
  34. % make sure r and t are column vectors.
  35. r=r(:);
  36. t=t(:);
  37. %% The meat of the code. Really brain dead simple.
  38. y=zeros(length(r),length(-pre:bin_size:post)-1);
  39. for i=1:length(r)
  40. % old slow way
  41. % cc=t-r(i);
  42. % cc=cc((cc>-pre)&(cc<post));
  43. s=r(i)-pre;
  44. f=r(i)+post;
  45. if isnan(s)
  46. y(i,:)=nan;
  47. else
  48. cc=stats.qbetween(t, s,f)-r(i);
  49. if ~isempty(cc)
  50. cc=histc(cc,-pre:bin_size:post);
  51. cc=cc(:)';
  52. y(i,:)=cc(1:end-1);
  53. end
  54. end
  55. end
  56. if nargout==2;
  57. x=-pre:bin_size:post;
  58. varargout{1}=x(1:end-1);
  59. end
  60. %% it might be worth returning an unbinned vector