function [x,testflags,bad] = spiketest() x = [8 10 9 19 8 11 12 NaN NaN 45 10 11 NaN 23 NaN 23 20 17]; %what I should see, numbers are gibberish: %[1 1 1 1 1 1 1 2 1 1] %Here's the deal: %Any value that is a NaN, skip. Any val that is surrounded by NaNs or its %only comparison value is NaN, it automatically gets flagged as a spike. %Any edge value only compares existant values, by taking the average of %only one point (ie-only the point's value) %OLD STYLE % rowcnt = 0; % for i=1:size(x,2) % if (isnan(x(i))) % if ((rowcnt == 1) % testres(i-1) = 1; % end % rowcnt = 0; % continue; % else % rowcnt = rowcnt + 1; % %NOTE THIS IS ACTUALLY A GRADIENT TEST... % szx = size(x,2); % testflags = zeros(szx,1); % testflags(1) = abs(x(1) - x(2)) > 5; % testflags(szx) = abs(x(szx) - x(szx-1)) > 5; % for i=2:szx-1 % pred = x(i + 1); % lag = x(i - 1); % cur = x(i); % % if (isnan(pred) & isnan(lag)) % %automatically flag point. % testflags(i) = 1; % elseif (isnan(cur)) % %current element is NaN, flag it % testflags(i) = 1; % elseif (isnan(pred)) % %endpoint (last point will be tested outside the loop) % testflags(i) = abs(cur - lag) > 5; % elseif (isnan(lag)) % %startpoint (also the 1st point is done outside the loop) % testflags(i) = abs(cur - pred) > 5; % else % %normal point % testflags(i) = abs(cur - (lag + pred)/2) > 5; % end % end % bad = x(logical(testflags)); % bad = bad'; % % x = x'; %%GRADIENT TEST WORKS %START SPIKE TEST szx = size(x,2); testflags = zeros(szx,1); for i=2:szx-1 pred = x(i + 1); lag = x(i - 1); cur = x(i); if (isnan(pred) & isnan(lag)) %automatically flag point. testflags(i) = 1; elseif (isnan(cur)) %current element is NaN, flag it testflags(i) = 1; elseif (isnan(pred)) %endpoint (last point will be tested outside the loop) testflags(i) = abs(cur - lag) > 5; elseif (isnan(lag)) %startpoint (also the 1st point is done outside the loop) testflags(i) = abs(cur - pred) > 5; else %normal point testflags(i) = abs(cur - (lag + pred)/2) > 5; end end bad = x(logical(testflags)); bad = bad';