function [percentage_in_pw,mean_abs_in_pw,mean_in_pw,largest_dt_in_pw,mean_size_nonPW_events,largest_event_not_in_PW]=sim_psc_prox_window(dt,ts,yjitter,group1length)
%description: takes vectors of dt (delta time) and true event size (ts), finds the bounds of the proximal window (pw), within which events actually are detected
    %the time window for actual event detection is found as follows:  sort the absolute values of dt, ascending;  take the difference between successive elements; the first difference greater than 1
    %occurs when one has left the regime of actual detection and is now looking at spurious detection
    %this method finds the closer bound of the pw;  the bound in the other temporal direction may be further away
    %also, results with this method are determined by the number of repetitions
    %group1length is used when dt and ts come from two sets, e.g., from trials with shuffled and unshuffled noise, and is set at the size of the first group
    %for events in the proximal window plots dt (with a small amount of y jitter for clarity) as a function of true event size
    %returns the position of the dt value that indicated transition out of the proximal window as a pct, the mean of the absolute values of dt for events within the pw (a measure of accuracy),
    %the mean of the actual (non-absolute value) dt for those events (a measure of bias), the largest dt in the pw, the mean size of undetected (non-PW) events, and the largest event that was not detected (outside PW)
%called by: command line
%calls:
%notes: dt values must be manually corrected for differences in taper between embedded and detected events; ts should be manually changed to positive values
%code confirmed date

[a,ind]=sort(abs(dt),'ascend');
b=diff(a);
c=find(b>1);
lastval_in_pw=c(1);

percentage_in_pw=100*(lastval_in_pw/length(dt));
mean_abs_in_pw=mean(a(1:lastval_in_pw));
mean_in_pw=mean(dt(ind(1:lastval_in_pw)));
largest_dt_in_pw=dt(ind(lastval_in_pw));
mean_size_nonPW_events=mean(ts(ind((lastval_in_pw+1):end)));
largest_event_not_in_PW=max(ts(ind((lastval_in_pw+1):end)));

jit4=rand(1,length(dt)); jit4=jit4*yjitter; jit4=jit4-0.5*yjitter;

%aaa=ind(1:100)

d=ind(1:lastval_in_pw); %dd=d(1:100), ddd=max(ind(1:lastval_in_pw))
e=d((d<=group1length)); %ee=e(1:100)
if length(dt)>group1length
    f=d((d>group1length));  %ff=f(1:100)
end

close all
plot(ts(e),dt(e)+jit4(e),'k.')
hold on
if length(dt)>group1length
    plot(ts(f),dt(f)+jit4(f),'r.')
end
box off
xlabel('true size (pA)')
ylabel('delta time (samples)')
%hold on, plot([0:1],[largest_dt_in_pw largest_dt_in_pw],'r')