function [numshuffles_neededfor_largerdiffthanorigdata,timemaxoriginaldif]=finding_confidence_for_timeofchangeinSTSD(sd_ratio,fixed_window,orig_facilitating,orig_nonfacilitating)
%called by analyze_across_sd_stdp_expts, provides a confidence interval for the 'onset of window of length n of greatest net difference between the facilitating and nonfacilitating responses'
%the latter value, sd_ratio, is found within analyze_sd_over_time


for t=1:length(sd_ratio)-fixed_window
    dif(t)=sum(sd_ratio(t:(t+fixed_window)));
end
[maxoriginaldif,timemaxoriginaldif]=max(dif)

numshuffles_neededfor_largerdiffthanorigdata=0;
maxdif=0;
while maxdif<maxoriginaldif & numshuffles_neededfor_largerdiffthanorigdata<10000
	sd_ratio=shuffle_randomization(sd_ratio);
    for t=1:length(sd_ratio)-fixed_window
        new_dif(t)=sum(sd_ratio(t:(t+fixed_window)));    
    end
	maxdif=max(new_dif);
	numshuffles_neededfor_largerdiffthanorigdata=numshuffles_neededfor_largerdiffthanorigdata+1;	
end

% %----
% windoze=[1 5 10 15 20 25];  %allowed windows, in trials
% num_montys=1;            %number of loops for the shuffled data to be run
% 
% figure
% clr=colormap(jet(length(windoze)));
% 
% maxdifsacrossshuffles=[];
% indicesofmaxdifsacrossshuffles=[];
% 
% facilitating=orig_facilitating;nonfacilitating=orig_nonfacilitating;
% 
% for mont=1:num_montys
%     maxdifs=[];maxinds=[];
% 	for w=1:length(windoze)
%         dif=[];
%         for t=1:length(facilitating)-windoze(w)
%             dif(t)=sum(facilitating(t:(t+windoze(w)))-nonfacilitating(t:(t+windoze(w))));   %may be faster to take difference first then sum
%             %dif=dif/windoze(w);    
%         end
%         if mont==1,plot(dif,'color',clr(w,:)),hold on,end
%         [y,i]=max(dif);
%         maxdifs=[maxdifs;y];;maxinds=[maxinds;i];
% 	end
%     maxdifsacrossshuffles=[maxdifsacrossshuffles maxdifs];indicesofmaxdifsacrossshuffles=[indicesofmaxdifsacrossshuffles maxinds];
%     %facilitating=shuffle_randomization(facilitating);
%     %nonfacilitating=shuffle_randomization(nonfacilitating);   %can shuffle together, shuffle separately, or generate random data (monte carlo) with multiplication of f to make it as much greater than nonfacilitating, on the average, as is the real f
%     shuff=shuffle_columns([facilitating;nonfacilitating]);
%     facilitating=shuff(1,:);nonfacilitating=shuff(2,:);
%     %facilitating=rand(1,length(facilitating))*(mean(orig_facilitating')/mean(orig_nonfacilitating'));
%     %nonfacilitating=rand(1,length(nonfacilitating));
% end
% hold on, plot(orig_facilitating,'k','linewidth',2);
% plot(orig_nonfacilitating,'color',[.7 .7 .7],'linewidth',3.5);
% 
% 
% figure(2),clf,figure(3),clf
% for iii=4%1:size(maxdifsacrossshuffles,1)
%     [y,i]=sort(maxdifsacrossshuffles(iii,:));
%     figure(2),plot(y,'color',clr(iii,:)),hold on
%     figure(3),plot(indicesofmaxdifsacrossshuffles(iii,i),'color',clr(iii,:)),hold on
% end
% %hold on, plot(orig_facilitating,'k','linewidth',2);
% %plot(orig_nonfacilitating,'color',[.7 .7 .7],'linewidth',3.5);