function event_peaks=find_peaks_specifically_ends_of_peaks(threshold,event_jitter,data_that_crosses_threshold)
%description: similar to find_peaks_within_threshold_crossing_regions but different in that the last value of the peak is returned, rather than the first value
    %if action current is present within the voltage step and drops below threshold, the 'last value of the peak' should be the first value of the action current
    %any current that exceeds threshold after the action current should be removed by the jittered event removal step at the end
%called by: create_basic_text_file_for_asynch_rel_INa_onset
%calls: [no custom functions]
%notes:
%code confirmed date: jan 6 2013

above_threshold=data_that_crosses_threshold>threshold; 
beginnings_iftrue=~above_threshold(1:end-1)&above_threshold(2:end);
endings_iftrue=above_threshold(1:end-1)&~above_threshold(2:end);
event_start_indices=1+find(beginnings_iftrue);
event_end_indices=find(endings_iftrue);

if length(event_start_indices)>length(event_end_indices),event_start_indices=event_start_indices(1:end-1);  %when data_that_crosses_threshold ends "high," the last event has a beginning but no ending
elseif length(event_end_indices)>length(event_start_indices),event_end_indices=event_end_indices(2:end);      %when d_t_c_t ends "low," the first event has an ending but no beginning
end

event_peaks=event_end_indices;
 
%remove any events within event_jitter of a previous stim artifact
z=length(event_peaks);
if length(event_peaks)>1
	for bleb=2:length(event_peaks)
        if event_peaks(z)-event_peaks(z-1)<event_jitter
            event_peaks(z)=[];
        end
        z=z-1;
    end
end