%calculate histogram
sumtrace=sumtrace2;
[choice, sumtrace1b, histamp, histindex,temp3,sortedindex2,sortvarify2]=traceselectionp(trace2,m,data2,adjustedtrace2,adjustedsum2,sumtrace);
%needs (trace*,m*,data*n,adjustedtrace*n,sumtrace),
%returns(choice, sumtrace1b, histamp, histindex)
clear sumtrace;
%creates list of min12 time for the traces
%plots the averaged trace, versus peak time for those selected traces
for j=1:m;
sortemin34time(j)=min34(sortedindex2(j)+1);
end;
plot (sa2:sa2+5000,sumtrace2(sa2:sa2+5000))
hold on;
plot (sortemin34time(1:size(sortvarify2)),10*sortvarify2,'o')
hold off;
ginput(1);
%loops the selection parameters
count6=0;
while (count6==0)
var1=2/3;
%calculate differences
for n=2:m+1;
for a=1:len(1);
estb2(n,a)=(sumtrace1b(1,a)-adjustedtrace2(a,n))^2;
end;
end;
%calculate sum of squares
sumsqu34b=sum(estb2(:,d(1,1):d(2,1))');
datab2=log(sumsqu34b(2:m+1));
[a,testmin1]=min(sumtrace1(1,d(1,1):d(2,1)));
min34(1)=testmin1+d(1,1);
for n=2:m+1;
%adjustedsum2(:,n)= (sumtrace1b(1,:)')*trace2(min34(n),n)/sumtrace1b(1,min34(1));
adjustedsum2(:,n)= (sumtrace1b(1,:)')*mean(trace2((min34(n)-50):(min34(n)+50),n))/mean(sumtrace1b(1,(min34(1)-50):(min34(1)+50)));
end;
%calculate histogram
sumtrace=sumtrace1b;
[choice, sumtrace1b, histamp, histindex,temp3,sortedindex2,sortvarify2]=traceselectionp(trace2,m,datab2,adjustedtrace2,adjustedsum2,sumtrace); %needs (trace*,m*,data*n,adjustedtrace*n,sumtrace),
%needs (trace*,m*,data*,adjustedtrace*,sumtrace),
%returns(choice, sumtrace1b, histamp, histindex)
clear sumtraceb sumtrace;
count6=count6+1;
end;
%removing sets of variables from memory
clear sumtrace1b j n temp3 len;
clear histamp histindex choice data data2 datab datab2;
clear est est2 estb estb2 min12 sumsqu12 sumsqu12b sumsqu34 sumsqu34b;
clear var2 ptrace1 ptrace2 ptrace3 sa2 testmin1 testmin2
clear sa1 c d a b baseline blstd m
clear count1 count2 count3 count6 min34 sumtrace2 test;