function OptN = hist_np_v4( spike_time )
%Function 'hist_np_v4' returns the optimal number of bins of time-histogram.
%
%Input argument
%spike_time: a vector of spike times {t_i}, spike_time=[t_1,t_2,t_3,...,t_n] (t_12)=2.0*Lv/(3.0-Lv);
%Cost(i)= Cost(i) + ( mean(2.0*f.*k)-var(k,1))/ D(i)^2; % cost function for # of bins = N(i) (Step 3,4 in ref[1] pp 3129-3130)
w(p)=mean(2.0*f.*k)-var(k,1);
%2017/11/16 revised.
end
Cost(i)=sum(w)/D(i)^2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Minimizing the cost function (Step 5 in ref[1] pp 3129-3130)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~,OptN] = min(Cost);
%hist(spike_time,OptN);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function "Calc_Lv" returns the local variation Lv of the spike train
function Lv = Calc_Lv( spike_time )
sp1=spike_time(1:length(spike_time)-2);
sp2=spike_time(2:length(spike_time)-1);
sp3=spike_time(3:length(spike_time) );
Lv = 3.0*mean(((sp3-2.0*sp2+sp1)./(sp3-sp1)).^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Estimate the fano factor for each bin (Step 2 in ref[1] pp %3129-3130)
%this function is not used here.
%function f = est_fano( count , est_fano )
%if count<3
% f = 1.0;
%else
% f = est_fano;
%end
%end