# findpeaks

## PURPOSE

Helper function to find peaks in a given continuous valued time series x

## SYNOPSIS

function xmax=findpeaks(data,threshold)

## DESCRIPTION

     Helper function to find peaks in a given continuous valued time series x
     Usage: xmax=findpeaks(data,threshold)
     Input:
          data     (data in time x channels/trials form or a single vector)
          threshold (if specified returns locations of peaks at which data exceeds threshold) - optional
     Output:
          xmax     (locations of local maxima of data in a structure array of dimensions channels/trials)

## CROSS-REFERENCE INFORMATION

This function calls:

- change_row_to_column Helper routine to transform 1d arrays into column vectors that are needed

This function is called by:

- fitlinesc fits significant sine waves to data (continuous data).

## SOURCE CODE

    0001 function xmax=findpeaks(data,threshold)
    0002 % Helper function to find peaks in a given continuous valued time series x
    0003 % Usage: xmax=findpeaks(data,threshold)
    0004 % Input:
    0005 %      data     (data in time x channels/trials form or a single vector)
    0006 %      threshold (if specified returns locations of peaks at which data exceeds threshold) - optional
    0007 % Output:
    0008 %      xmax     (locations of local maxima of data in a structure array of dimensions channels/trials)
    0009 if nargin < 1; error('Need data'); end;
    0010 data=change_row_to_column(data);
    0011 C=size(data,2);
    0012 pp1=[data(1,:);data(1:end-1,:)];
    0013 pp2=[data(2:end,:);data(end,:)];
    0014 xmax(1:C)=struct('loc',[]);
    0015 % for ch=1:C,
    0016 %    if nargin ==1
    0017 %      xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>0 & data(:,ch)-pp2(:,ch)>0)];
    0018 %    else
    0019 %      xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>0 & data(:,ch)-pp2(:,ch)>0 & data(:,ch)>threshold)];
    0020 %    end
    0021 % end
    0022
    0023 for ch=1:C,
    0024    if nargin ==1
    0025      xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>=0 & data(:,ch)-pp2(:,ch)>=0)];
    0026    else
    0027      xmax(ch).loc=[xmax(ch).loc; find(data(:,ch)-pp1(:,ch)>=0 & data(:,ch)-pp2(:,ch)>=0 & data(:,ch)>=threshold)];
    0028    end
    0029 end