function dcz=template_based_mini_detector(data,template) %this is a matlab implementation of method of clements and bekkers (Biophys J, 1997) %tic % if size(data,1)>size(data,2),data=data';end % datalength=length(template); % % template=lsqfit_template_generator([3 8 52 225 0 0],[1:(datalength-1)]);template=[0 template]; %don't know (for detecting and assigning start time) whether it's better to include a zero at the beginning of the template dcz=[]; for i=1:length(data)-datalength ddata=data(i:i+(datalength-1)); scale=(sum(template.*ddata)-(sum(template)*(sum(ddata)/datalength)))/(sum(template.*template)-(sum(template)*(sum(template)/datalength))); offset=(sum(ddata)-(scale*sum(template)))/datalength; fitted_template=template*scale+offset; % plot(ddata,'k'),hold on, plot(fitted_template,'r'),plot(ddata-fitted_template,'g'), plot(((ddata-fitted_template).^2),'m') sse=sum((ddata-fitted_template).^2); standard_error=sqrt(sse/(datalength-1)); detection_criterion=scale/standard_error; dcz=[dcz detection_criterion]; % figure(1),clf,plot(ddata,'k'),hold on,plot(fitted_template,'r'),legend(num2str(scale),num2str(standard_error)),pause(.1) end %figure,plot(data,'k'), hold on, plot(dcz,'r') %toc