sticklength=600;
t=(0:length(shearload)-1)*12000/333333;
ind=[];
ind2=[];
meanamp=[];
slipduration=[];
drop=[];
for i=1:sticklength:length(shearload)-sticklength;
    thre=(max(shearload(i:i+sticklength))-min(shearload(i:i+sticklength)));
    ind1=find((diff(shearload(i:i+sticklength)))<-max(thre*.5,.04));
    ind=[ind;ind1+i-1];
    meanamp=[meanamp;ones(size(ind1))*mean(shearload(i:i+sticklength))];
    for j=1:length(ind1)
        extractf=shearload(ind1(j)+i-3:ind1(j)+i+130);
        extractt=t(ind1(j)+i-3:ind1(j)+i+130);
        warning off
        p=polyfit([1:134]',extractf,9);
        warning on
%         plot([1:134],extractf,[1:134],polyval(p,[1:134]),'r')
        
        x0 = fminbnd(@(x) polyf(x,p),1,134);
        slipduration=[slipduration; x0-3];
        drop=[drop; extractf(3)-extractf(round(x0))];
    end
end

allevents=[ae;as;asd];
alltimes=[te;ts;tsd];
% allevents=ae;
% alltimes=te;
[alltimes i]=sort(alltimes);
allevents=allevents(i);


for i=1:length(ind)
    temp=find(alltimes>t(ind(i)+5));
    ind2=[ind2;temp(1)];
end

shelev=[];
for i=1:length(ind2)
    temp=find(t>alltimes(ind2(i)));
    shelev=[shelev,shearload(temp(1))];
end    


figure;
plot(t,shearload+4.5,t(ind),shearload(ind)+4.5,'*r',alltimes,allevents,'.g',alltimes(ind2),allevents(ind2),'+k');
% figure;
% plot(meanamp,alltimes(ind2)-t(ind)','.')
% xlabel('horizontal load')
% ylabel('time between slip and next AE event')
% figure;
% plot(meanamp(1:end-1),t(ind(2:end))-alltimes(ind2(1:end-1))','.')
% ylabel('time between first AE event of cluster and next slip')
% xlabel('horizontal load')
% 
% figure;
% plot(meanamp(1:end-1),(t(ind(2:end))-alltimes(ind2(1:end-1))')./(shearload(ind(2:end))-shelev(1:end-1)')','.')
% ylabel('time between first AE event of cluster and next slip/increase in stress untill next slip')
% xlabel('horizontal load')
% 
% 
% figure;
% 
% plot(meanamp,shelev,'.')
% xlabel('horizontal load')
% ylabel('stress at time of first AE after slip')
% 
% figure;
% plot(meanamp,shelev./drop','.')
% xlabel('horizontal load')
% ylabel('stress at time of first AE after slip/stress drop')
% 
% 
% 
% figure;
% plot(meanamp(1:end-1),(alltimes(ind2(1:end-1))-t(ind(1:end-1))')./(alltimes(ind2(2:end))-alltimes(ind2(1:end-1))),'.');
% ylabel('time between slip and next AE event/time between slips')
% xlabel('horizontal load')

figure;
plot(meanamp,slipduration*12000/333333,'.');
xlabel('horizontal load')
ylabel('slip duration')

figure;
plot(t(ind(2:end)),t(ind(2:end))-t(ind(1:end-1)),'.')
xlabel('time')
ylabel('recurence time')

figure;
plot(t(ind),drop,'.')
xlabel('time')
ylabel('stress drop')