function [times,extrems,amplitudes,mascenters2]=create_extremum_sequence2(y,maximumindexes,minimumindexes,window)
indexmin=1;
indexmax=1;
times=[];
extrems=[];
amplitudes=[];
mascenters2=0;
lenmaxtimes=length(maximumindexes);
lenmintimes=length(minimumindexes);

while (indexmin<=lenmintimes)&&(indexmax<=lenmaxtimes)
    if (minimumindexes(indexmin)<maximumindexes(indexmax))
        if (~isempty(extrems))&&(extrems(length(extrems))==-1) 
            indexmin=indexmin+1;
            continue;
        end
        if (~isempty(times))&&(abs(minimumindexes(indexmin)-times(length(times)))<window/3)
            indexmin=indexmin+1;
            continue;
        end
        times=[times,minimumindexes(indexmin)];
        extrems=[extrems,-1];
        indexmin=indexmin+1;
    else
        if (~isempty(extrems))&&(extrems(length(extrems))==1)
            indexmax=indexmax+1;
            continue;
        end
        if (~isempty(times))&&(abs(maximumindexes(indexmax)-times(length(times)))<window/3)
            indexmax=indexmax+1;
            continue;
        end
        times=[times,maximumindexes(indexmax)];
        extrems=[extrems,1];
        indexmax=indexmax+1;
    end
    if (length(times)==1)
        amplitude=abs(y(times(length(times)))-y(1));
        mascenters2=[mascenters2,(y(times(length(times)))+y(1))/2];
    else
        amplitude=abs(y(times(length(times)))-y(times(length(times)-1)));
        mascenters2=[mascenters2,(y(times(length(times)))+y(times(length(times)-1)))/2];
    end
    amplitudes=[amplitudes,amplitude];
end


while (indexmin<=lenmintimes)
    times=[times,minimumindexes(indexmin)];
    extrems=[extrems,-1];
    indexmin=indexmin+1;
    if (length(times)==1)
        amplitude=abs(y(times(length(times)))-y(1));
        mascenters2=[mascenters2,(y(times(length(times)))+y(1))/2];
    else
        amplitude=abs(y(times(length(times)))-y(times(length(times)-1)));
        mascenters2=[mascenters2,(y(times(length(times)))+y(times(length(times)-1)))/2];
    end
    amplitudes=[amplitudes,amplitude];
end
   
while (indexmax<=lenmaxtimes)
    times=[times,maximumindexes(indexmax)];
    extrems=[extrems,1];
    indexmax=indexmax+1;
    if (length(times)==1)
        amplitude=abs(y(times(length(times))));
        mascenters2=[mascenters2,(y(times(length(times)))+y(1))/2];
    else
        amplitude=abs(y(times(length(times)))-y(times(length(times)-1)));
        mascenters2=[mascenters2,(y(times(length(times)))+y(times(length(times)-1)))/2];
    end
    amplitudes=[amplitudes,amplitude];
end
