function bump_indices_and_values=bump_counter(data_with_bumps,samples_per_ms,smoothing_binwidth,network_event_detection_mode,consecutive_ups,consecutive_downs,...
    asynchronous_event_detection_mode,event_detection_threshold,event_jitter,orig_baseline_postsynaptic,bump_skip_portion,plot_bumps,delete_artifactual_event,patchlog_name,...
    trial,pulse_num)
%this guy works with a sliding window to smooth the curve
%then assigns a value to each bin based on whether its value is greater or lesser than the bin after it
%then asks whether enough consecutive decreases are followed by enough consecutive increases to say we have found a bump

%initial processing, common to both modes of bump/event detection
data_with_bumps=data_with_bumps-orig_baseline_postsynaptic;
data_with_bumps=data_with_bumps(bump_skip_portion:end);
data_with_bumps=data_with_bumps';
    
if network_event_detection_mode
    
    %smoothing
	mean_smooth=[];
	smoothing_matrix=[];
	for i=1:smoothing_binwidth
        endpoint=length(data_with_bumps)-(smoothing_binwidth-(i-1));
        smoothing_matrix(i,:)=data_with_bumps(i:endpoint);
	end
	mean_smooth=mean(smoothing_matrix);
	
	%convert to binary -- 1 means that bin is greater than the bin after it, 0 means it is less than the bin after it (if equal to bin after it?)
	binary_converter=[];
	mean_bin=[];
	binary_converter(1,:)=mean_smooth(1:length(mean_smooth)-1);
	binary_converter(2,:)=mean_smooth(2:end);
	binary_converter(3,:)=binary_converter(1,:)>binary_converter(2,:);
	mean_bin=binary_converter(3,:);
	%subplot(3,1,2)
	%plot(mean_bin)
	%aaa=mean_bin(1:100)'
	
	%find the bumps
	spotter=zeros(1,length(mean_bin));
	for k=consecutive_downs+1:length(mean_bin)-consecutive_ups
        if mean_bin(k-consecutive_downs:k-1)
            if ~mean_bin(k:k+consecutive_ups)
                spotter(k)=1;
            end
        end
	end
	bump_indices_and_values=[]%repmat(NaN,2,length(find(spotter)));   %note that now working with columns
    bump_indices=find(spotter)+bump_skip_portion;
%     thresh=.5*min(data_with_bumps(bump_indices))
%     a=data_with_bumps(bump_indices)
%     b=find(data_with_bumps(bump_indices)<thresh)
%     bump_indices(b)=[]
    %bump_indices(find(diff(bump_indices)<500)+1)=[];
    if ~isempty(bump_indices)
	    bump_indices_and_values(1,:)=bump_indices;  %-(smoothing_binwidth/2)+20;
	    bump_indices_and_values(2,:)=data_with_bumps(bump_indices);
    else
	    bump_indices_and_values=[];
    end
        
	if plot_bumps
		figure(51)
		clf
		hold on
		plot(mean_smooth)
		%shifting the original data (linearly) to plot it for comparison purposes
		shifted_data_with_bumps=data_with_bumps+min(data_with_bumps);
		plot(shifted_data_with_bumps(1:length(shifted_data_with_bumps-smoothing_binwidth)),'g')
        spotter(bump_indices)=data_with_bumps(bump_indices);
		%plot(data_with_bumps(bump_indices),'r');
		plot(spotter, 'r')
        title(strcat(patchlog_name,'__',num2str(trial),'__',num2str(pulse_num)))
	end
%-----------------------
elseif asynchronous_event_detection_mode
    
     length(data_with_bumps)
    if plot_bumps
        figure(52)
        subplot(3,2,[1:2])
        plot(data_with_bumps)
        title(strcat(patchlog_name,'__',num2str(trial),'__',num2str(pulse_num)))
    end
        
	mean_smooth=[];
	smoothing_matrix=[];
%     forward_smoothing=0
%     if forward_smoothing
		for i=1:smoothing_binwidth
		    endpoint=length(data_with_bumps)-(smoothing_binwidth-(i-1));
		    smoothing_matrix(i,:)=data_with_bumps(i:endpoint);
		end
%     else %backward smoothing
%         i=smoothing_binwidth
%         while i>0
%             smoothing_matrix(i,:)=data_with_bumps(i:end-(smoothing_binwidth-i));   
%             i=i-1;
%         end
%     end
    data_with_bumps=mean(smoothing_matrix);
    
    if plot_bumps
        subplot(3,2,[3:4])
        plot(data_with_bumps)
    end
    
        
    %thresholding
    data_with_bumps_below_thresh=data_with_bumps<event_detection_threshold;
    data_with_bumps_beginnings_iftrue=~data_with_bumps_below_thresh(1:end-1)&data_with_bumps_below_thresh(2:end);
    data_with_bumps_endings_iftrue=data_with_bumps_below_thresh(1:end-1)&~data_with_bumps_below_thresh(2:end);
    event_start_indices=1+find(data_with_bumps_beginnings_iftrue)
    event_end_indices=find(data_with_bumps_endings_iftrue)
    num_events_counted=length(event_end_indices)
    
    if length(event_start_indices)>length(event_end_indices)
        event_end_indices=[event_end_indices,length(data_with_bumps)]
    elseif length(event_start_indices)<length(event_end_indices)
        event_start_indices=[1,event_start_indices]
    elseif (~isempty(event_start_indices) & ~isempty(event_end_indices)) & ((event_end_indices(1)<event_start_indices(1)) & (event_end_indices(end)<event_start_indices(end)))
         event_start_indices=[1,event_start_indices]
         event_end_indices=[event_end_indices,length(data_with_bumps)]
    end
    
    %finding the index of the bottom of each mini -- this is used for looking at event occurrence time
    event_mininds=0;
    for ttt=1:length(event_start_indices)
        local_min=find(data_with_bumps(event_start_indices(ttt):event_end_indices(ttt))==min(data_with_bumps(event_start_indices(ttt):event_end_indices(ttt))));
        event_mininds(ttt)=local_min(1)+event_start_indices(ttt)-1;
	end
    
    %making sure one event isn't counted more than once
    corrected_event_mininds=NaN;
    bump_indices_and_values=[];
    if event_mininds
        z=length(event_mininds);
		for bleb=2:length(event_mininds)
            if event_mininds(z)-event_mininds(z-1)<event_jitter
                event_mininds(z)=[];
            end
            z=z-1;
		end
		corrected_event_mininds=event_mininds;
        if delete_artifactual_event
            corrected_event_mininds(1)=[];
        end
        
        %another filter -- trying to get rid of artifactual bumps caused by a brief increase in the leak
        removers=[];
        for bg=1:length(corrected_event_mininds)
            if corrected_event_mininds(bg)>501
                if mean(data_with_bumps(corrected_event_mininds(bg)+[-500:-30]) )<-2 
                    removers=[removers bg];
                end
            else
                if mean(data_with_bumps(corrected_event_mininds(bg)+[-1*(corrected_event_mininds(bg)-1):-30]))<-2
                     removers=[removers bg];
                end
            end
                
        end
        gh=length(removers)
        corrected_event_mininds(removers)=[];
        
        if plot_bumps
%             subplot(3,2,[5:6])
            figure(2)
            subplot(4,4,trial-12)
            plot([1:length(data_with_bumps)],data_with_bumps,'b',corrected_event_mininds,data_with_bumps(corrected_event_mininds),'ks')
            title(strcat(patchlog_name,'__',num2str(trial),'__',num2str(pulse_num)))
        end
        bump_indices_and_values(1,:)=corrected_event_mininds+bump_skip_portion-(smoothing_binwidth/2);
        bump_indices_and_values(2,:)=data_with_bumps(corrected_event_mininds);
    end
end