function get_sp( num_std ) if (~exist('z_data')) load('z_data'); end if (~exist('num_std')) num_std = 5; end sp_T = 1/20e3; sp_t = 0 : sp_T : 10-sp_T; for channel = 1:64 z = z_data(:,channel); threshold(channel) = num_std*std(z); erroneous_indices = find( abs( z(1:8) ) > 7*std(z) ); z( erroneous_indices ) = z( erroneous_indices ) / 7; z_data(:,channel) = z; s4std_indices = find( abs(z) > threshold(channel) ); s4std = sp_T * s4std_indices - sp_T; % save these peak times in s4std k=0; m=0; mi=0; sp=[]; for i=1:length(s4std) if (s4std(i)-k <= 0.4e-3) % 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 sp_data{channel} = sp; disp( sprintf( 'Channel %d done.', channel ) ); end save( sprintf( 'sp%d_data_ext', num_std ), 'sp_data', 'z_data', 'threshold', 'sp_t', 'sp_T', 'num_std' ); save( sprintf( 'sp%d_data', num_std ), 'sp_data', 'num_std' ); return;