/matlab/fieldtrip-20101107/fileio/ft_filter_event.m

http://github.com/yuval-harpaz/ft_BIU · MATLAB · 119 lines · 60 code · 9 blank · 50 comment · 19 complexity · 823a466dd9c5e0f5a6598e57b161f3d3 MD5 · raw file

  1. function event = ft_filter_event(event, varargin)
  2. % FT_FILTER_EVENT does what its name implies
  3. %
  4. % Use as
  5. % event = ft_filter_event(event, ...)
  6. %
  7. % The optional arguments should come in key-value pairs and determine the
  8. % filter characteristics:
  9. % type = cell-array with strings
  10. % value = numeric array
  11. % sample = numeric array
  12. % timestamp = numeric array
  13. % offset = numeric array
  14. % duration = numeric array
  15. % minsample = value
  16. % maxsample = value
  17. % minduration = value
  18. % maxduration = value
  19. % mintimestamp = value
  20. % maxtimestamp = value
  21. % minnumber = value, applies only if event.number is present
  22. % maxnmumber = value, applies only if event.number is present
  23. %
  24. % See also FT_READ_EVENT, FT_WRITE_EVENT
  25. % Copyright (C) 2007-2010 Robert Oostenveld
  26. %
  27. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  28. % for the documentation and details.
  29. %
  30. % FieldTrip is free software: you can redistribute it and/or modify
  31. % it under the terms of the GNU General Public License as published by
  32. % the Free Software Foundation, either version 3 of the License, or
  33. % (at your option) any later version.
  34. %
  35. % FieldTrip is distributed in the hope that it will be useful,
  36. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. % GNU General Public License for more details.
  39. %
  40. % You should have received a copy of the GNU General Public License
  41. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  42. %
  43. % $Id: ft_filter_event.m 944 2010-04-21 16:08:12Z roboos $
  44. % get the optional input arguments
  45. type = keyval('type', varargin);
  46. value = keyval('value', varargin);
  47. sample = keyval('sample', varargin);
  48. timestamp = keyval('timestamp', varargin);
  49. offset = keyval('offset', varargin);
  50. duration = keyval('duration', varargin);
  51. % the numeric fields can also be filtered on a range
  52. minsample = keyval('minsample', varargin);
  53. maxsample = keyval('maxsample', varargin);
  54. minduration = keyval('minduration', varargin);
  55. maxduration = keyval('maxduration', varargin);
  56. mintimestamp = keyval('mintimestamp', varargin);
  57. maxtimestamp = keyval('maxtimestamp', varargin);
  58. minnumber = keyval('minnumber', varargin);
  59. maxnumber = keyval('maxnumber', varargin);
  60. if ~isempty(type)
  61. % this can be specified as string or as cell-array, convert to cell-array
  62. if ~iscell(type)
  63. type = {type};
  64. end
  65. end
  66. % determine which filters to apply
  67. testtype = ~isempty(type) && isfield(event, 'type');
  68. testvalue = ~isempty(value) && isfield(event, 'value');
  69. testsample = ~isempty(sample) && isfield(event, 'sample');
  70. testtimestamp = ~isempty(timestamp) && isfield(event, 'timestamp');
  71. testoffset = ~isempty(offset) && isfield(event, 'offset');
  72. testduration = ~isempty(duration) && isfield(event, 'duration');
  73. testminsample = ~isempty(minsample) && isfield(event, 'sample');
  74. testmaxsample = ~isempty(maxsample) && isfield(event, 'sample');
  75. testminduration = ~isempty(minduration) && isfield(event, 'duration');
  76. testmaxduration = ~isempty(maxduration) && isfield(event, 'duration');
  77. testmintimestamp = ~isempty(mintimestamp) && isfield(event, 'timestamp');
  78. testmaxtimestamp = ~isempty(maxtimestamp) && isfield(event, 'timestamp');
  79. testminnumber = ~isempty(minnumber) && isfield(event, 'number');
  80. testmaxnumber = ~isempty(maxnumber) && isfield(event, 'number');
  81. if (~isempty(minnumber) || ~isempty(maxnumber)) && ~isfield(event, 'number')
  82. warning('the events are not numbered, assuming that the order corresponds to the original stream sequence');
  83. for i=1:length(event)
  84. event(i).number = i;
  85. end
  86. testminnumber = ~isempty(minnumber);
  87. testmaxnumber = ~isempty(maxnumber);
  88. end
  89. % apply the filters
  90. sel = true(length(event),1);
  91. for i=1:length(event)
  92. % test whether they match with the selected arrays
  93. if testvalue, sel(i) = sel(i) && any(event(i).value == value); end
  94. if testsample, sel(i) = sel(i) && any(event(i).sample == sample); end
  95. if testtimestamp, sel(i) = sel(i) && any(event(i).timestamp == timestamp); end
  96. if testoffset, sel(i) = sel(i) && any(event(i).offset == offset); end
  97. if testduration, sel(i) = sel(i) && any(event(i).duration == duration); end
  98. % test whether they lie within the specified range
  99. if testminsample, sel(i) = sel(i) && (event(i).sample >= minsample); end
  100. if testmaxsample, sel(i) = sel(i) && (event(i).sample <= maxsample); end
  101. if testminduration, sel(i) = sel(i) && (event(i).duration >= minduration); end
  102. if testmaxduration, sel(i) = sel(i) && (event(i).duration <= maxduration); end
  103. if testmintimestamp, sel(i) = sel(i) && (event(i).timestamp >= mintimestamp); end
  104. if testmaxtimestamp, sel(i) = sel(i) && (event(i).timestamp <= maxtimestamp); end
  105. if testminnumber, sel(i) = sel(i) && (event(i).number >= minnumber); end
  106. if testmaxnumber, sel(i) = sel(i) && (event(i).number <= maxnumber); end
  107. % this is potentially the slowest test, hence do it the last
  108. if testtype, sel(i) = sel(i) && any(strcmp(event(i).type, type)); end
  109. end
  110. event = event(sel);