function [below0segs,above0segs]=noise_segmenter(n_i,splisiterequirements,find_unique_segments,breakpoints_mustbe_consecutive)
%description: takes an input of noise, generally noise in the membrane current recorded during voltage clamp, visually inspected and found free of events
    %returns locations where the noise can be cut, shuffled and re-annealed
    %cut points are samples with a specified min # of consecutive values going towards or away from 0 (with 0 assumed to be the mean value of the input noise)
%called by: simulated_psc_in_noise
%calls: find_consecutive_breakpoints, segment_seeker
%notes: contemplated adding a check to prevent segments from being too short, but even w/ 15*max splitsiterequirements this has minimal effect on the number of segs found in either recorded or random noise, so commented out
%code_confirmed_date: july 17, 2012

required_consec_decreases_belowzero=splisiterequirements(1);
required_consec_increases_belowzero=splisiterequirements(2);
required_consec_increases_abovezero=splisiterequirements(3);
required_consec_decreases_abovezero=splisiterequirements(4);

%below0andgreaterthanimmedsuccessor would more accurately be called 'below0andknowntobegreaterthanimmedsuccessor'...
noisebelowzero=repmat(nan,1,length(n_i));noisebelowzero(find(n_i<0))=n_i(find(n_i<0));thisbelow=noisebelowzero(1:end-1);thatbelow=noisebelowzero(2:end);
    below0andgreaterthanimmedsuccessor=[thisbelow>thatbelow 0];below0andgreaterthanimmedpredecessor=[0 thatbelow>thisbelow];   
noiseabovezero=repmat(nan,1,length(n_i));noiseabovezero(find(n_i>0))=n_i(find(n_i>0));thisabove=noiseabovezero(1:end-1);thatabove=noiseabovezero(2:end);
    above0andlessthanimmediatesuccessor=[thisabove<thatabove 0];above0andlessthanimmedpredecessor=[0 thatabove<thisabove];

%finding beginnings and endings of each possible segment
bz_downgoing=[];for jjj=1:required_consec_decreases_belowzero,bz_downgoing(jjj,:)=below0andgreaterthanimmedsuccessor(jjj:end-(required_consec_decreases_belowzero-jjj));end     %aligned to the beginning
bz_upgoing=[];for iii=1:required_consec_increases_belowzero,bz_upgoing(iii,:)=below0andgreaterthanimmedpredecessor(required_consec_increases_belowzero-(iii-1):end-(iii-1));end    %aligned to the end

az_upgoing=[];for nnn=1:required_consec_increases_abovezero,az_upgoing(nnn,:)=above0andlessthanimmediatesuccessor(nnn:end-(required_consec_increases_abovezero-nnn));end
az_downgoing=[];for mmm=1:required_consec_decreases_abovezero,az_downgoing(mmm,:)=above0andlessthanimmedpredecessor(required_consec_decreases_abovezero-(mmm-1):end-(mmm-1));end

begin_segs_below=find(all(bz_downgoing));
    if length(begin_segs_below)>1,begin_segs_below(find(diff(begin_segs_below)==1)+1)=[];end%getting to the beginning of any streaks longer than the required # of consec vals
end_segs_below=find(all(bz_upgoing));end_segs_below=end_segs_below+required_consec_increases_belowzero-1;%re-align with the original values
    if length(end_segs_below)>1,end_segs_below=fliplr(end_segs_below);end_segs_below(find(diff(end_segs_below)==-1)+1)=[];end_segs_below=fliplr(end_segs_below);end
    
begin_segs_above=find(all(az_upgoing));
    if length(begin_segs_above)>1,begin_segs_above(find(diff(begin_segs_above)==1)+1)=[];end
end_segs_above=find(all(az_downgoing));end_segs_above=end_segs_above+required_consec_decreases_abovezero-1;
    if length(end_segs_above)>1,end_segs_above=fliplr(end_segs_above);end_segs_above(find(diff(end_segs_above)==-1)+1)=[];end_segs_above=fliplr(end_segs_above);end

if breakpoints_mustbe_consecutive
    [end_segs_above,begin_segs_below]=find_consecutive_breakpoints(end_segs_above,begin_segs_below);
    [end_segs_below,begin_segs_above]=find_consecutive_breakpoints(end_segs_below,begin_segs_above);
end
    
%find segments
if ~isempty(begin_segs_below) && ~isempty(end_segs_below) && ~isempty(begin_segs_above) && ~isempty(end_segs_above)
    below0segs=segment_seeker(begin_segs_below,end_segs_below,find_unique_segments);%below0segs(:,find(diff(below0segs)<15*max(splisiterequirements)))=[];
    above0segs=segment_seeker(begin_segs_above,end_segs_above,find_unique_segments);%above0segs(:,find(diff(above0segs)<15*max(splisiterequirements)))=[];
else
    error('segmentation not possible')
end