% Problem 4, Exercise 1 %
% Generate spikes for 10s (or more) using a Poisson spike generator...

lambda = 100; %in Hz
T = 50; %in seconds
dt = 0.001; %in seconds
t = [0:dt:T];
spktimes = [];

for i = 1:length(t)
    if lambda*dt >= rand(1)
        spktimes(1,end+1) = t(i);
    end
end

ISIs = diff(spktimes);
tau = mean(ISIs);
sigma = std(ISIs);
C_V = sigma/tau;

binwidth = 0.100; %in seconds
spkcount = hist(spktimes,[binwidth/2:binwidth:T-binwidth/2]);
fano = var(spkcount)/mean(spkcount);

binwidth = 0.005; %in seconds
max_ISI = round(max(ISIs)*100)/100;
hist(ISIs,[binwidth/2:binwidth:max_ISI-binwidth/2]);
set(gca,'FontSize',14,'XTick',[0:binwidth:max_ISI],'XTickLabel',[0:binwidth:max_ISI]*1000);
xlabel('binned time (ms)'); ylabel('number of ISIs');
gtext(['generated over ',num2str(T),' seconds',10,10,'C_{V} = ',num2str(C_V),10,'fano = ',num2str(fano)]);