function effective_corr_K = find_best_k(cv2_local,fir,bin_kass_length,max_K)
%% max_cor_length in (ms)
a = 1/cv2_local;
b = (1e3)/(fir*a);
sample_count = 1e5;
min_cor = 0.01;
proc = cumsum(gamrnd(a,b,1,sample_count));
proc_bins = histcounts(proc,1:bin_kass_length:proc(end));
acr = autocorr(proc_bins,'NumLags',max_K);
acf = abs(acr(2:end));
%plot(acr(2:end))
K = find(acf<min_cor,1,'first');
if isempty(K)
    effective_corr_K= max_K;
else
   effective_corr_K = K+1;
end
