function spikes = lifspikes(trials, current, tmax, D)
% Generate spike times of a leaky integrate-and-fire neuron
% trials: the number of trials to be generated
% current: the stimulus either as a single value or as a vector
% tmax: duration of a trial if input is a single number
% D: the strength of additive white noise
    
    tau = 0.01;
    if nargin < 4
        D = 1e0;
    end
    vreset = 0.0;
    vthresh = 10.0;
    dt = 1e-4;
    
    n = length( current );
    if n <= 1
        n = ceil(tmax/dt);
        current = zeros( n, 1 ) + current;
    end
    spikes = cell(trials, 1);
    for k=1:trials
        times = [];
        j = 1;
        v = vreset + (vthresh-vreset)*rand();
        noise = sqrt(2.0*D)*randn(n, 1)/sqrt(dt);
        for i=1:n
            v = v + (- v + noise(i) + current(i))*dt/tau;
            if v >= vthresh
                v = vreset;
                times(j) = i*dt;
                j = j + 1;
            end
        end
        spikes{k} = times;
    end
end