function s5std(channel,num_std) if (~exist('z_data')) load('z_data'); end if (~exist('num_std')) num_std = 5; end fig = channel+500; z = z_data(:,channel); figure(fig);clf; subplot(2,1,1); plot(0:.05:9999.95,z); threshold = num_std*std(z); s4std_indices = find( abs(z) > threshold ); % finds +ve and -ve peaks s4std = 0.05 * s4std_indices - 0.05; % save these peak times in s4std hold on; plot(s4std,zeros(length(s4std),1),'k.'); AX = axis; plot( [AX(1) AX(2)], [threshold threshold], 'g' ); plot( [AX(1) AX(2)], [-threshold -threshold], 'g' ); k=0; m=0; mi=0; sp=[]; for i=1:length(s4std) if (s4std(i)-k <= 0.4) % if we haven't jumped past a 0.4ms vicinity in this step if (z(s4std_indices) < m) m=z(s4std_indices); % accumulates local minimum z(t) mi=s4std(i); % gets actual time of minimum end else sp(end+1)=mi; % saves the min peak time in sp m=z(s4std_indices); % load in a new minimum amplitude mi=s4std(i); % and a new minimum time end k=s4std(i); % store the previous time in k end sp(end+1)=mi; sp=sp(2:end); % ignore the first point plot(sp,zeros(length(sp),1),'rx');