cm_outlier2nan_20140311.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. function data = cm_outlier2nan_20140311(data,comparator,criterion,recursive)
  2. %
  3. % data = cm_outlier2nan_20140311(data,comparator,criterion,recursive)
  4. %
  5. % marks outlier in data as NaN
  6. % input: data = [N x 1] vector
  7. % comparator = '>', '<', '>=', '<=', '>/<'
  8. % criterion = criterion*SD as the outlier criterion
  9. % recursive = recursive outlier exclusion (1)
  10. %
  11. % output: data = [N x 1] vector; outliers set to NaN
  12. % THG 11.03.2014
  13. %% find epochs
  14. % make sure data orientation is ok (i.e. N X 1 data points)
  15. sz = size(data);
  16. if sz(1) == 1 && sz(2) > 1
  17. data = data';
  18. elseif sz(2) == 1 && sz(1) > 1
  19. data = data;
  20. end
  21. % temporary z values
  22. z = cm_nanzscore_20140302(data);
  23. % initialize index variable
  24. index = [];
  25. % find indices to exclude
  26. if strcmp(comparator,'>/<')
  27. index = find( abs(z) > criterion);
  28. elseif strcmp(comparator,'>') || strcmp(comparator,'>=')
  29. eval(['index = find( z ' comparator ' criterion );']);
  30. elseif strcmp(comparator,'<') || strcmp(comparator,'<=')
  31. eval(['index = find( z ' comparator ' criterion*-1 );']);
  32. end
  33. % replace outliers with NaNs
  34. data(index) = NaN;
  35. z(index) = NaN;
  36. % recursive exclusion
  37. if recursive
  38. if ~isempty(index)
  39. check = 0;
  40. while check == 0
  41. % number of excluded outliers
  42. Nex = length(index);
  43. % new zscore calculation after outlier exclusion
  44. z = cm_nanzscore_20140302(z);
  45. % find channels to exclude
  46. if strcmp(comparator,'>/<')
  47. index_2 = find( abs(z) > criterion);
  48. elseif strcmp(comparator,'>') || strcmp(comparator,'>=')
  49. eval(['index_2 = find( z ' comparator ' criterion );']);
  50. elseif strcmp(comparator,'<') || strcmp(comparator,'<=')
  51. eval(['index_2 = find( z ' comparator ' criterion*-1 );']);
  52. end
  53. % update index
  54. index = [index; index_2];
  55. % update data
  56. data(index) = NaN;
  57. z(index) = NaN;
  58. % check if additional channel excluded
  59. if Nex == length(index)
  60. check = 1;
  61. end
  62. % clear variables
  63. clear Nex index_2
  64. end; clear check
  65. end
  66. end