function event_peaks=find_peaks_within_threshold_crossing_regions(threshold,event_jitter,data_that_crosses_threshold) %description: finds segments of a vector that exceed a threshold, finds the index of the maximum value within each segment (the peak), and removes peaks that occur within event_jitter of the previous peak %called by: patchfile_analysis, create_basic_text_file, mini_waveform_extractor, careful_asynch_measurer, patchfile_analysis_specialforplateaus, rrp_timesinceAP_versus_timewithintrial, create_basic_text_file_forrrp, create_basic_text_file_justforrealtime, dwnrawreader_core_simplified2(deprecated) %calls: [no custom functions] %notes: %code confirmed date: sep 19 2009 %find suprathreshold segments 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); %remove suprathreshold segments not fully captured in the recording 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 beinning end %find the index of the max value within each suprathreshold segment event_peaks=[]; for ttt=1:length(event_start_indices) suprathresh_max=find(data_that_crosses_threshold(event_start_indices(ttt):event_end_indices(ttt))==max(data_that_crosses_threshold(event_start_indices(ttt):event_end_indices(ttt)))); event_peaks(ttt)=suprathresh_max(1)+event_start_indices(ttt)-1; end %remove any peaks within event_jitter of a previous peak z=length(event_peaks); if length(event_peaks)>1 for bleb=2:length(event_peaks) if event_peaks(z)-event_peaks(z-1)