% This file is licensed under the Allen Institute Terms of Use, which can be found at:
%http://www.alleninstitute.org/Media/policies/terms_of_use_content.html

function new_prob_slice=rebin_uniform_vector(v_vec,new_v_vec,prob_slice);
%Function to re-distribute probabilities from one voltage discretization to
%another
%Inputs:
%v_vec=initial voltage discretization vector
%new_v_vec=target voltage discretization vector
%prob_slice=vector of probability distribution p(v,t=t0) at a given time t0
%
%Outputs:
%new_prob_slice = new slice of probability distribution after re-binning

%remove probabilities at extrema (threshold and fully depleted)
prob_slice=prob_slice(1:end-1);

%vector of differences between bin-edges for re-distributing probability
dvs=v_vec(2:end)-v_vec(1:end-1);
left_ends_v=v_vec(1:end-1);
right_ends_v=v_vec(2:end);

%initialize output vector
new_prob_slice=zeros(size(new_v_vec));

%re-distribute probability at current instant of time
for vv=1:length(new_v_vec)-1
    vright=new_v_vec(vv+1);
    vleft=new_v_vec(vv);
    keepindices=find(left_ends_v<=vright & right_ends_v>=vleft);    %edge values overlap
    overlaps=zeros(length(keepindices),1);
    for keep=1:length(keepindices)
        index=keepindices(keep);
        overlaps=(min(vright,right_ends_v(index))-max(vleft,left_ends_v(index)))/dvs(index);
        new_prob_slice(vv)=new_prob_slice(vv)+overlaps*prob_slice(index);
    end;
end;
        

