function [processed_sd,sd_ratio,sd_ratio_diff_time]=analyze_sd_over_time(time_axis,pscmat,critz,current_expt,minutes_per_trial,patchtitle,array_of_charges,use_psc_instead_of_charge,...
    sd_ratio_windows,current_sd_ratio_window)
%the purpose is to carefully examine the second (or third or fourth) pulse values, both absolute and relative to first pulse values, as they evolve over time
%it is difficult to normalize to first pulse size due to the many failures of the first pulse, so the closest preceding (or temporally closest) successful first pulse(s) can be used as the normalizing factor
%the values of PSCa following, for example, a 25 ms IPI, will occur on everage only once every fifth trial, so the values can be interpolated to fill in the intervening time steps and extrapolated to fill any empty ends
%these interpolated values can then be smoothed
%IPIs can be separated into classes such that all events following IPIs of a given class are pooled
%called by analyze_across_sd_stdp_expts and by simple_sd_solutions
%calls class_membership_assigner, mean_with_preference_for_precedents, interpolate_with_smoothing, cumulative_histogram

if size(pscmat,1)==5
    IPI_of_interest=1;
    ordinal_val_of_numerator=2;
    ordinal_val_of_denominator=1;
elseif size(pscmat,1)==6
    IPI_of_interest=2;
    ordinal_val_of_numerator=3;
    ordinal_val_of_denominator=1;
end
number_of_numerator_vals_to_average=2;  %average numerator values following the correct IPI regardless of success
interpolate_numerator_blanks=1;
    smooth_interpolation=1;
normalize_to_denominator=0;
	number_of_denominator_vals_to_average=2;    %average only successful denominators
analyze_by_IPI_class=1;
    IPI_class1=[25 50];    %100 ms is omitted due to the variable behavior (12.5 is ommitted b/c it occurs only in one expt)
    IPI_class2=[150 200 400 800];
linewidth=1.5;
y_lim=[-.5 10];
hist_colors=['r' 'g' 'b' 'c' 'm' 'y' 'k'];

if use_psc_instead_of_charge
    measurements_for_denominator=pscmat(ordinal_val_of_denominator+2,:);measurements_for_numerator=pscmat(ordinal_val_of_numerator+2,:);
else
    measurements_for_denominator=array_of_charges(ordinal_val_of_denominator,:);measurements_for_numerator=array_of_charges(ordinal_val_of_numerator,:);    
end
crits_of_denominator=critz(ordinal_val_of_denominator,:);
postinduc_time_axis=time_axis(find(~time_axis):end);
figure(100+current_expt),title(patchtitle),hold on
if analyze_by_IPI_class,
    IPI_class_membership=class_membership_assigner(IPI_class1,IPI_class2,pscmat(IPI_of_interest,:));
    clr=colormap(jet(max(IPI_class_membership)));
    numerator_loop_length=max(IPI_class_membership);
else
    possible_IPIs=unique(pscmat(IPI_of_interest,:));
    clr=colormap(jet(length(possible_IPIs)));
    numerator_loop_length=length(possible_IPIs);
end

if normalize_to_denominator %normalizing to the most recent, or, if insufficient values precede, most temporally proximal successful denominator value(s)
	for iii=1:length(measurements_for_denominator)
        if crits_of_denominator(iii)==0
            trials_till_now_with_good_denominator=find(crits_of_denominator(1:iii));
           if length(trials_till_now_with_good_denominator)>=number_of_denominator_vals_to_average
                mean_nearest_good_denoms=mean(measurements_for_denominator(trials_till_now_with_good_denominator((end-(number_of_denominator_vals_to_average-1)):end)));
           else
                trials_from_now_with_good_denominator=(iii-1)+find(crits_of_denominator(iii:end));
                if ~isempty(trials_till_now_with_good_denominator)
                    nearest_good_denoms=measurements_for_denominator(trials_till_now_with_good_denominator);
                    mean_nearest_good_denoms=mean([nearest_good_denoms measurements_for_denominator(trials_from_now_with_good_denominator(1:(number_of_denominator_vals_to_average-length(nearest_good_denoms))))]);
                else
                    mean_nearest_good_denoms=measurements_for_denominator(trials_from_now_with_good_denominator(1));%mean(measurements_for_denominator(trials_from_now_with_good_denominator(1:number_of_denominator_vals_to_average))); 
                end
            end
            corrected_measurements_for_denominator(iii)=mean_nearest_good_denoms;
        end
	end
    xlabl=strcat('value', num2str(ordinal_val_of_numerator), 'div by value', num2str(ordinal_val_of_denominator), 'by IPI', num2str(IPI_of_interest));
else
    xlabl=strcat('value', num2str(ordinal_val_of_numerator), 'by IPI', num2str(IPI_of_interest));
end

processed_sd=[];
for jjj=1:numerator_loop_length     %averaging, interpolating and extrapolating missing numerator values
    if analyze_by_IPI_class, trials_having_this_IPI=find(IPI_class_membership==jjj);else trials_having_this_IPI=find(pscmat(IPI_of_interest,:)==possible_IPIs(jjj));end
    if ~isempty(trials_having_this_IPI)
        averaged_numerator_measurements_with_nans=mean_with_preference_for_precedents(measurements_for_numerator,trials_having_this_IPI,number_of_numerator_vals_to_average);
        if normalize_to_denominator, averaged_numerator_measurements_with_nans=averaged_numerator_measurements_with_nans./corrected_measurements_for_denominator;end%else
            %averaged_numerator_measurements_with_nans=averaged_numerator_measurements_with_nans/mean(averaged_numerator_measurements_with_nans(trials_having_this_IPI));end
        if interpolate_numerator_blanks
            for mmm=1:length(trials_having_this_IPI)-1       %this will fail if there is only one such trial
				gulf=1:(trials_having_this_IPI(mmm+1)-trials_having_this_IPI(mmm)-1);
                if ~isempty(gulf)
                    increment=(averaged_numerator_measurements_with_nans(trials_having_this_IPI(mmm+1))-averaged_numerator_measurements_with_nans(trials_having_this_IPI(mmm)))/(trials_having_this_IPI(mmm+1)-trials_having_this_IPI(mmm));
                    adder=[gulf*increment+averaged_numerator_measurements_with_nans(trials_having_this_IPI(mmm)) averaged_numerator_measurements_with_nans(trials_having_this_IPI(mmm+1))];
                    if smooth_interpolation & length(adder)>3,adder=interpolate_with_smoothing(adder);end
					averaged_numerator_measurements_with_nans((trials_having_this_IPI(mmm)+1):(trials_having_this_IPI(mmm+1)-1))=adder(1:end-1);
                end
                if mmm==1&&trials_having_this_IPI(mmm)~=1    %taking care of the left end
                    averaged_numerator_measurements_with_nans(1:trials_having_this_IPI(mmm)-1)=repmat(averaged_numerator_measurements_with_nans(trials_having_this_IPI(mmm)),1,trials_having_this_IPI(mmm)-1);
                end
                if mmm==length(trials_having_this_IPI)-1&&trials_having_this_IPI(end)~=length(averaged_numerator_measurements_with_nans)     %taking care of the right end
                    averaged_numerator_measurements_with_nans(trials_having_this_IPI(end)+1:end)=repmat(averaged_numerator_measurements_with_nans(trials_having_this_IPI(end)),1,length(averaged_numerator_measurements_with_nans)-trials_having_this_IPI(end));
                end
            end
        end
       processed_sd(jjj,:)=averaged_numerator_measurements_with_nans/mean(measurements_for_denominator);
       plot(time_axis,processed_sd(jjj,:),'color',clr(jjj,:),'linewidth',linewidth)
       %plot(postinduc_time_axis,processed_sd(jjj,find(time_axis>=0)),'color',clr(jjj,:),'linewidth',linewidth)
       %figure(200+current_expt),hold on,[x_for_ecdf,y_for_ecdf]=cumulative_histogram(processed_sd(jjj,:),hist_colors(jjj));figure(100+current_expt)
	end
end

%sd_ratio=(processed_sd(1,:)-processed_sd(2,:))./(processed_sd(1,:)+processed_sd(2,:));
sd_ratio=processed_sd(1,:)-processed_sd(2,:);
if current_expt==10,sd_ratio=processed_sd(2,:)-processed_sd(1,:);end

smoothed_denom=movingwindowsmooth(measurements_for_denominator,4);
smoothed_denom=[smoothed_denom(1) smoothed_denom(1) smoothed_denom(1) smoothed_denom];
%plot(time_axis,smoothed_denom/mean(measurements_for_denominator),'k','linewidth',.01)

sd_ratio_diff_time=postinduc_time_axis(find_greatest_n_consecutive_vals(round(sd_ratio_windows(current_sd_ratio_window)*inv(minutes_per_trial)),sd_ratio(find(time_axis>=0))));    %finding the beginning of the n minute period with the largest sd_ratio
%sd_ratio_diff_time=time_axis(find_greatest_n_consecutive_vals(round(consecutive_sdratio_minutes*inv(minutes_per_trial)),sd_ratio));
clr=colormap(jet(length(sd_ratio_windows)));
plot([sd_ratio_diff_time sd_ratio_diff_time],[y_lim(1) y_lim(2)],'color',clr(current_sd_ratio_window,:),'linewidth',2)
hold on, plot([0 0],[y_lim(1) y_lim(2)],'k','linewidth',1.5)
%ylim([y_lim(1) y_lim(2)])
set(gca,'ytick',[0:y_lim(2)])
xlabel(xlabl)