PechePourPoisson.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function [T,tvec]=PechePourPoisson(rate,dt)
  2. % Rate-Varying Poisson-Process generator.
  3. %
  4. % T=PechePourPoisson(rate);
  5. % T=PechePourPoisson(rate,dt);
  6. % [T tvec]=PechePourPoisson(rate,dt);
  7. %
  8. % Example:
  9. % rate=conv(randn(1,10000),ones(1,1000),'same');
  10. % [T t]=PechePourPoisson(rate,.001);
  11. % plot(t,rate);
  12. % addline(T); addline(0,'h'); % You may not have the addline function
  13. %
  14. % This function spits out a poisson process with a rate that can vary with
  15. % time. Each sample of the "rate" vector is considered to be the beginning
  16. % of a time bin of width dt. The number of events occurring in each bin is
  17. % sampled from a Poisson Distribution. The timing of each event within the
  18. % bin is then randomized.
  19. %
  20. % Note: You can easily specify a constant-rate process running for a
  21. % certain time by just making "rate" a scalar rate and setting dt to your
  22. % desired time span.
  23. %
  24. % Inputs Description
  25. % "rate" Vector representing the rate as a function of time. Negative
  26. % rates are interpreted as zero rates.
  27. % ("dt") Optional scalar defining the time resolution of the rate
  28. % vector. If neglected, dt=1, and the rate is interpreted as
  29. % meaning "events per time-sample". If dt is set, rate will be
  30. % scaled by the value of dt. eg, say you want 90events/s, where
  31. % the rate vector represents the spiking rate as a function of
  32. % time with millisecond resolution. Then dt=0.001, and
  33. % rate(i)=90, where i is the particular instant with a 90event/s
  34. % rate.
  35. %
  36. % Outputs Description
  37. % "T" Vector of event times. The first sample of "rate" is
  38. % considered to be time 0. If dt is specified, times will be
  39. % scaled accordingly.
  40. % "tvec" If you're too lazy to make your own time vector for your rate
  41. % vector, this function can optionally do it for you.
  42. %
  43. % Enjoy
  44. % Peter
  45. % oconnorp _at_ ethz _dot_ ch
  46. % dt defaults to 1
  47. if nargin<2, dt=1; end
  48. % Determine number of events in each time bin
  49. p=poissrnd(max(rate(:),0)'*dt);
  50. % Find the times of non-empty bins
  51. ix=find(p);
  52. adds=diff([0 ix-1]);
  53. cum=cumsum([1 p(ix)]);
  54. % Term is same as sum(p(ix)), just avoids repeating calculation
  55. T=zeros(cum(end)-1,1);
  56. T(cum(1:end-1))=adds;
  57. six=find(~T);
  58. T=cumsum(T);
  59. % Sort the randoms (skipping bins with just 1 spike obviously to save time,
  60. % thought the time taken by the interpreter to parse out this comment is
  61. % probably even more than that, but I guess its the priniple that counts.)
  62. T=T+rand(size(T));
  63. sortable=sort([six(diff([0; six])>1)-1; six]);
  64. T(sortable)=sort(T(sortable));
  65. % Efficiency of this last chunk could definitely be improved but really I
  66. % don't care enough to do it now.
  67. % Scale times
  68. if dt~=1, T=T*dt; end
  69. % For the lazy...
  70. if nargout>1
  71. tvec=0:dt:(length(rate)-1)*dt;
  72. end
  73. end