function spikes = poissonspikes( trials, rate, tmax )
% Generate spike times of a homogeneous poisson process
% trials: number of trials that should be generated
% rate: the rate of the Poisson process in Hertz
% tmax: the duration of each trial in seconds
% returns a cell array of vectors of spike times

    dt = 3.33e-5;
    p = rate*dt; % probability of event per bin of width dt
    % make sure p is small enough:
    if p > 0.1
        p = 0.1
        dt = p/rate;
    end
    spikes = cell( trials, 1 );
    for k=1:trials
        x = rand( 1, round(tmax/dt) ); % uniform random numbers for each bin
        spikes{k} = find( x < p ) * dt;
    end
end
