disc.m 900 B

1234567891011121314151617181920212223242526272829
  1. function [spt, pk] = disc(unit,thres)
  2. %DISC Unit discrimination.
  3. % [SPT PK] = DISC(UNIT,THR) returns the location (SPT) and value (PK) of
  4. % UNIT peaks above THR.
  5. % Input argument check
  6. narginchk(2,2)
  7. unit = unit(:)'; % row vector
  8. % Segments above threshold
  9. dsc = find(unit>=thres); % find all points above threshold
  10. if isempty(dsc) % no spikes
  11. spt = [];
  12. pk = [];
  13. return
  14. end
  15. lendsc = length(dsc);
  16. df = diff(dsc); % distance of points above threshold
  17. gaps = find(df>1); % find gaps between segments above threshold
  18. gaps = [0 gaps lendsc]; % there's a spike between every two gaps
  19. % Find peaks for each segment
  20. NumSpk = length(gaps) - 1; % number of spikes
  21. [spt, pk] = deal(nan(1,NumSpk));
  22. for iS = 1:NumSpk
  23. [mx, mxloc] = max(unit(dsc(gaps(iS)+1:gaps(iS+1))));
  24. spt(iS) = dsc(1,gaps(iS)+mxloc);
  25. pk(iS) = mx; % value at peak
  26. end