function detection_criterion=template_based_mini_detector_onestep(data,template,templatelength)
%description: the core of the template-match detection algorithm (Bekkers and Clements, Biophys Journal 1997)
%called by: CDC_creator, which calls it only once/'trial'/standard criterion waveform (i.e., taking only one start point, the first sample)
%calls: no custom functions
%notes: data and template are vectors of the same length
%code confirmed date: dec 28, 2012

scale=(sum(template.*data)-(sum(template)*(sum(data)/templatelength)))/(sum(template.*template)-(sum(template)*(sum(template)/templatelength)));
offset=(sum(data)-(scale*sum(template)))/templatelength;
fitted_template=template*scale+offset;
sse=sum((data-fitted_template).^2);
standard_error=sqrt(sse/(templatelength-1));
detection_criterion=scale/standard_error;       %analogous to signal/noise

