qcount_binned.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function [Y, bin_time] = qcount_binned(ts,ref,bins,varargin)
  2. % [Y] = qcount_binned(ts,from_t,to_t,bins,varargin)
  3. % Counts spikes in a fixed number of bins between two events.
  4. % This can be used to "warp" different nunmber of trials to be the same length.
  5. % Inputs
  6. % ts spike times (or any other sorted vector)
  7. % ref A matrix of times. At least 2 columns.
  8. % bins How many bins do you want to divide up the time into? A scalar
  9. % or a vector with as many elements as columns of ref-1;
  10. % Optional Input
  11. % normalize [true] If true instead of counts/bin the function returns counts/second.
  12. % e.g.
  13. % You have a list of spike times:
  14. % ts = sort([normrnd(1,1,[1,100]) normrnd(3,2,[1,200]) normrnd(10,1,[1,100]) normrnd(12,2,[1,200])]);
  15. % % It's like two trials.
  16. % % You want to take 10 bins between 0-2 and from 9-11 and 5 bins between 2-5 and 11-14;
  17. % ref = [0 2 5; 9 11 14]; bins = [10, 5];
  18. iod = @utils.inputordefault;
  19. normalize = iod('normalize',true,varargin);
  20. num_chunks = size(ref,2)-1;
  21. num_trials = size(ref,1);
  22. if numel(bins)==1
  23. bins = repmat(bins, 1, num_chunks);
  24. end
  25. Y = nan(num_trials,sum(bins));
  26. bin_time = nan(num_trials,num_chunks);
  27. cbins = cumsum(bins);
  28. for sx = 1:num_trials
  29. for rx = 1:num_chunks
  30. if rx==1
  31. chunk_ind = 1:cbins(rx);
  32. else
  33. chunk_ind = cbins(rx-1)+1:cbins(rx);
  34. end
  35. bin_edges = linspace(ref(sx,rx), ref(sx,rx+1), bins(rx)+1);
  36. bin_time(sx,rx) = (ref(sx,rx+1)-ref(sx,rx)) / bins(rx);
  37. Y(sx,chunk_ind) = arrayfun(@(a,b)stats.qcount(ts,a,b),bin_edges(1:end-1),bin_edges(2:end));
  38. if normalize
  39. Y(sx,chunk_ind) = Y(sx,chunk_ind)/bin_time(sx,rx);
  40. end
  41. end
  42. end
  43. end