findpeaks.m 1.2 KB

1234567891011121314151617181920212223242526272829
  1. function xmax=findpeaks(data,threshold)
  2. % Helper function to find peaks in a given continuous valued time series x
  3. % Usage: xmax=findpeaks(data,threshold)
  4. % Input:
  5. % data (data in time x channels/trials form or a single vector)
  6. % threshold (if specified returns locations of peaks at which data exceeds threshold) - optional
  7. % Output:
  8. % xmax (locations of local maxima of data in a structure array of dimensions channels/trials)
  9. if nargin < 1; error('Need data'); end;
  10. data=change_row_to_column(data);
  11. C=size(data,2);
  12. pp1=[data(1,:);data(1:end-1,:)];
  13. pp2=[data(2:end,:);data(end,:)];
  14. xmax(1:C)=struct('loc',[]);
  15. % for ch=1:C,
  16. % if nargin ==1
  17. % xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>0 & data(:,ch)-pp2(:,ch)>0)];
  18. % else
  19. % xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>0 & data(:,ch)-pp2(:,ch)>0 & data(:,ch)>threshold)];
  20. % end
  21. % end
  22. for ch=1:C,
  23. if nargin ==1
  24. xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>=0 & data(:,ch)-pp2(:,ch)>=0)];
  25. else
  26. xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>=0 & data(:,ch)-pp2(:,ch)>=0 & data(:,ch)>=threshold)];
  27. end
  28. end