function [smallesterrthisphasicwvform_wthispermutofclustersactive,smallesterrorforthisphasicwaveform_atthisclustering,bestfitsummedasynch_forthisphasic]=...
    compare_asynch_waveform_clusters_to_phasic_waveform(unjittered_waveforms,jittered_waveforms,waveform_to_fit,squared_peaksize_present_phasic,asynchfittingsegment,jit_range,...
    smallesterrthisphasicwvform_wthispermutofclustersactive,smallesterrorforthisphasicwaveform_atthisclustering,bestfitsummedasynch_forthisphasic)
%description: takes the input array of waveforms (which has one waveform per row) and adds each waveform at variable jitter to the next at variable jitter, and so on to the end
%the final summed waveform is then compared to the evoked waveform currently under investigation and the fit quality is recorded
%the best fit to the current phasic waveform and all phasic waveforms for this clustering are returned, and for the latter, 
%the whole jittered waveform is also returned
%called by: cluster_asynch_based_on_phasic
%calls: itself
%notes: recursive
%code confirmed date: sep 24, 2009

waveform_currently_jittering=unjittered_waveforms(1,:);
unjittered_waveforms(1,:)=[];

for dejitter=jit_range      %successive passes through the loop increasingly prepone the asynchronous waveform; hence the index variable name
    jittered_waveform=waveform_currently_jittering(dejitter+asynchfittingsegment);
    jittered_waveforms=[jittered_waveforms;jittered_waveform];
    if isempty(unjittered_waveforms)
        summed_event=sum(jittered_waveforms,1);
        fit_error=sum((summed_event-waveform_to_fit).^2)/squared_peaksize_present_phasic;  %scaling the goodness (or badness) of fit
        if fit_error<smallesterrthisphasicwvform_wthispermutofclustersactive,
            smallesterrthisphasicwvform_wthispermutofclustersactive=fit_error;
            if fit_error<smallesterrorforthisphasicwaveform_atthisclustering
                smallesterrorforthisphasicwaveform_atthisclustering=fit_error;
                bestfitsummedasynch_forthisphasic=summed_event;
            end
        end
    else
        [smallesterrthisphasicwvform_wthispermutofclustersactive,smallesterrorforthisphasicwaveform_atthisclustering,bestfitsummedasynch_forthisphasic]=...
            compare_asynch_waveform_clusters_to_phasic_waveform(unjittered_waveforms,jittered_waveforms,waveform_to_fit,squared_peaksize_present_phasic,asynchfittingsegment,jit_range,...
            smallesterrthisphasicwvform_wthispermutofclustersactive,smallesterrorforthisphasicwaveform_atthisclustering,bestfitsummedasynch_forthisphasic);
    end
    jittered_waveforms(end,:)=[];   %strip the most recently added waveform to be able to re-add it with one more step of dejitter on the next iteration of the loop
end