function [amplitudes,timessinceprevious]=event_detector(ds_datau_,mini_detection_threshold)

%mcc_output_gain=2
mini_jitter=30
%ds_datau_=ds_datau_*(2000/mcc_output_gain);
ds_datau_=ds_datau_-mean(ds_datau_(1:10000));
smoothing_binwidth=10;

%finding minis
% if spontaneous_trace_smoothing
	mean_smooth=[];
	smoothing_matrix=[];
	ds_datau_=ds_datau_';
	for i=1:smoothing_binwidth
	    endpoint=length(ds_datau_)-(smoothing_binwidth-(i-1));
	    smoothing_matrix(i,:)=ds_datau_(i:endpoint);
	end
	ds_datau_=mean(smoothing_matrix);
% end

%thresholding
channelone_below_thresh=ds_datau_<mini_detection_threshold;
channelone_beginnings_iftrue=~channelone_below_thresh(1:end-1)&channelone_below_thresh(2:end);
channelone_endings_iftrue=channelone_below_thresh(1:end-1)&~channelone_below_thresh(2:end);
ministart_indices_channelone=1+find(channelone_beginnings_iftrue);
miniend_indices_channelone=find(channelone_endings_iftrue);
num_minis_channelone=length(miniend_indices_channelone)
b=length(ministart_indices_channelone)

%accounting for cases where a threshold crossing toward zero, without the preceding threshold crossing away from zero, occurs (presumably at the beginning of the recording)
%or the analogous thing happens at the end of the recording
%or both occur in the same recording
if length(ministart_indices_channelone)>length(miniend_indices_channelone)
    miniend_indices_channelone=[miniend_indices_channelone,length(ds_datau_)];
elseif length(ministart_indices_channelone)<length(miniend_indices_channelone)
    ministart_indices_channelone=[1;ministart_indices_channelone];
elseif (~isempty(ministart_indices_channelone) & ~isempty(miniend_indices_channelone)) & ((miniend_indices_channelone(1)<ministart_indices_channelone(1)) & (miniend_indices_channelone(end)<ministart_indices_channelone(end)))
     ministart_indices_channelone=[1,ministart_indices_channelone];
     miniend_indices_channelone=[miniend_indices_channelone,length(ds_datau_)];
end

%finding the index of the bottom of each mini -- this is used for looking at mini frequency (inter-event interval)
mini_mininds_chanone=[];
for ttt=1:length(ministart_indices_channelone)
    local_min=find(ds_datau_(ministart_indices_channelone(ttt):miniend_indices_channelone(ttt))==min(ds_datau_(ministart_indices_channelone(ttt):miniend_indices_channelone(ttt))));
    mini_mininds_chanone(ttt)=local_min(1)+ministart_indices_channelone(ttt)-1;
end
length(mini_mininds_chanone)

if ~isempty(mini_mininds_chanone)
	z=length(mini_mininds_chanone);
	for bleb=2:length(mini_mininds_chanone)
        if mini_mininds_chanone(z)-mini_mininds_chanone(z-1)<mini_jitter
            mini_mininds_chanone(z)=[];
        end
        z=z-1;
	end
	corrected_mini_mininds_chanone=mini_mininds_chanone';
end
     
amplitudes=(ds_datau_(corrected_mini_mininds_chanone));
timessinceprevious=diff(corrected_mini_mininds_chanone);

figure(1)
plot([.0001:.0001:(length(ds_datau_)/10000)],ds_datau_)
hold on
plot(corrected_mini_mininds_chanone./10000,amplitudes,'gs','linewidth',3)