width_delay_beam_experiment/DataAnalysis.py
def peak_detect(xarr, yarr, lookahead = 1, delta=0):
    '''
    Taken from :: sixtenbe (GitHub Gist)
    https://gist.github.com/sixtenbe

    Modified to be compatible with this file's structure.

    Converted from/based on a MATLAB script at:
    http://billauer.co.il/peakdet.html

    function for detecting local maximas and minmias in a signal.
    Discovers peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively

    keyword arguments:
    y_arr -- A list containg the signal over which to find peaks
    x_arr -- (optional) A x-axis whose values correspond to the y_axis list
        and is used in the return to specify the postion of the peaks. If
        omitted an index of the y_axis is used. (default: None)
    lookahead -- (optional) distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 200)
        '(sample / period) / f' where '4 >= f >= 1.25' might be a good value
    delta -- (optional) this specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak. Useful
        to hinder the function from picking up false peaks towards to end of
        the signal. To work well delta should be set to delta >= RMSnoise * 5.
        (default: 0)
            delta function causes a 20% decrease in speed, when omitted
            Correctly used it can double the speed of the function

    return -- ym, xm : the maximum values and corresponding x values
    '''
    ## I don't need the minimum peaks feature, but the algorithm
    ## doesn't work as expected if I comment out anything that deals
    ## with the minimum. And I'm too lazy to fix it heh.
    maxPeaks = []
    minPeaks = []

    # Used to pop the first hit which almost always is false
    dump = []

    # Store data length for later use
    length = len(yarr)

    # Perform some checks
    if lookahead < 1:
        raise ValueError, "Lookahead must be '1' or above in value"
    if not (np.isscalar(delta) and delta >= 0):
        raise ValueError, "delta must be a positive number"

    # Maxima and minima candidates are temporarily stored in
    # mx and mn respectively
    mn, mx = np.Inf, -np.Inf

    # Only detect peak if there is 'lookahead' amount of points after it
    for index, (x, y) in enumerate(zip(xarr[:-lookahead],
                                        yarr[:-lookahead])):
        if y > mx:
            mx = y
            mxpos = x
        if y < mn:
            mn = y
            mnpos = x

        #### LOOK FOR MAX ####
        if y < mx-delta and mx != np.Inf:

            # Maxima peak candidate found
            # Look ahead in signal to ensure that this is a peak and not jitter
            if yarr[index:index+lookahead].max() < mx:
                maxPeaks.append([mxpos, mx])
                dump.append(True)

                # Set algorithm to only find minima now
                mx = np.Inf
                mn = np.Inf
                if index+lookahead >= length:
                    # End is within lookahead no more peaks can be found
                    break
                continue

        #### LOOK FOR MIN ####
        if y > mn+delta and mn != -np.Inf:

            # Minima peak candidate found
            # Look ahead in signal to ensure that this is a peak and not jitter
            if yarr[index:index+lookahead].min() > mn:
                minPeaks.append([mnpos, mn])
                dump.append(False)

                # Set algorithm to only find maxima now
                mn = -np.Inf
                mx = -np.Inf
                if index+lookahead >= length:

                    # End is within lookahead no more peaks can be found
                    break

    # Remove the false hit on the first value of the y_axis
    try:
        if dump[0]:
            maxPeaks.pop(0)
        else:
            minPeaks.pop(0)
        del dump
    except IndexError:
        # No peaks were found, do nothing
        pass

    ## Return only the maximum, that's all I need from this method
    xm, ym =  (np.asarray([p[0] for p in maxPeaks]),
               np.asarray([p[1] for p in maxPeaks]))
    return xm, ym

    Taken from :: sixtenbe (GitHub Gist)
    https://gist.github.com/sixtenbe

    Modified to be compatible with this file's structure.

    Converted from/based on a MATLAB script at:
    http://billauer.co.il/peakdet.html

    function for detecting local maximas and minmias in a signal.
    Discovers peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively

    keyword arguments:
    y_arr -- A list containg the signal over which to find peaks
    x_arr -- (optional) A x-axis whose values correspond to the y_axis list
        and is used in the return to specify the postion of the peaks. If
        omitted an index of the y_axis is used. (default: None)
    lookahead -- (optional) distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 200)
        '(sample / period) / f' where '4 >= f >= 1.25' might be a good value
    delta -- (optional) this specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak. Useful
        to hinder the function from picking up false peaks towards to end of
        the signal. To work well delta should be set to delta >= RMSnoise * 5.
        (default: 0)
            delta function causes a 20% decrease in speed, when omitted
            Correctly used it can double the speed of the function

    return -- ym, xm : the maximum values and corresponding x values
    


Modified to be compatible with this file's structure.



function for detecting local maximas and minmias in a signal.
Discovers peaks by searching for values which are surrounded by lower
or larger values for maximas and minimas respectively



- ym, xm : the maximum values and corresponding x values
Modified to be compatible with this file ' s structure . function for detecting local maximas and minmias in a signal . Discovers peaks by searching for values which are surrounded by lower or larger values for maximas and minimas respectively - ym xm : the maximum values and corresponding x values
def peak_detect(xarr, yarr, lookahead = 1, delta=0):
    maxPeaks = []
    minPeaks = []
    dump = []
    length = len(yarr)
    if lookahead < 1:
        raise ValueError, "Lookahead must be '1' or above in value"
    if not (np.isscalar(delta) and delta >= 0):
        raise ValueError, "delta must be a positive number"
    mn, mx = np.Inf, -np.Inf
    for index, (x, y) in enumerate(zip(xarr[:-lookahead],
                                        yarr[:-lookahead])):
        if y > mx:
            mx = y
            mxpos = x
        if y < mn:
            mn = y
            mnpos = x
        if y < mx-delta and mx != np.Inf:
            if yarr[index:index+lookahead].max() < mx:
                maxPeaks.append([mxpos, mx])
                dump.append(True)
                mx = np.Inf
                mn = np.Inf
                if index+lookahead >= length:
                    break
                continue
        if y > mn+delta and mn != -np.Inf:
            if yarr[index:index+lookahead].min() > mn:
                minPeaks.append([mnpos, mn])
                dump.append(False)
                mn = -np.Inf
                mx = -np.Inf
                if index+lookahead >= length:
                    break
    try:
        if dump[0]:
            maxPeaks.pop(0)
        else:
            minPeaks.pop(0)
        del dump
    except IndexError:
        pass
    xm, ym =  (np.asarray([p[0] for p in maxPeaks]),
               np.asarray([p[1] for p in maxPeaks]))
    return xm, ym
def peak_detect ( xarr , yarr , lookahead = 1 , delta = 0 ) : maxPeaks = [ ] minPeaks = [ ] dump = [ ] length = len ( yarr ) if lookahead < 1 : raise ValueError , "Lookahead must be '1' or above in value" if not ( np . isscalar ( delta ) and delta >= 0 ) : raise ValueError , "delta must be a positive number" mn , mx = np . Inf , - np . Inf for index , ( x , y ) in enumerate ( zip ( xarr [ : - lookahead ] , yarr [ : - lookahead ] ) ) : if y > mx : mx = y mxpos = x if y < mn : mn = y mnpos = x if y < mx - delta and mx != np . Inf : if yarr [ index : index + lookahead ] . max ( ) < mx : maxPeaks . append ( [ mxpos , mx ] ) dump . append ( True ) mx = np . Inf mn = np . Inf if index + lookahead >= length : break continue if y > mn + delta and mn != - np . Inf : if yarr [ index : index + lookahead ] . min ( ) > mn : minPeaks . append ( [ mnpos , mn ] ) dump . append ( False ) mn = - np . Inf mx = - np . Inf if index + lookahead >= length : break try : if dump [ 0 ] : maxPeaks . pop ( 0 ) else : minPeaks . pop ( 0 ) del dump except IndexError : pass xm , ym = ( np . asarray ( [ p [ 0 ] for p in maxPeaks ] ) , np . asarray ( [ p [ 1 ] for p in maxPeaks ] ) ) return xm , ym
Taken from :: sixtenbe (GitHub Gist)
https://gist.github.com/sixtenbe
Taken from :: sixtenbe ( GitHub Gist ) https : // gist . github . com / sixtenbe
'''
    Taken from :: sixtenbe (GitHub Gist)
    https://gist.github.com/sixtenbe

    Modified to be compatible with this file's structure.

    Converted from/based on a MATLAB script at:
    http://billauer.co.il/peakdet.html

    function for detecting local maximas and minmias in a signal.
    Discovers peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively

    keyword arguments:
    y_arr -- A list containg the signal over which to find peaks
    x_arr -- (optional) A x-axis whose values correspond to the y_axis list
        and is used in the return to specify the postion of the peaks. If
        omitted an index of the y_axis is used. (default: None)
    lookahead -- (optional) distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 200)
        '(sample / period) / f' where '4 >= f >= 1.25' might be a good value
    delta -- (optional) this specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak. Useful
        to hinder the function from picking up false peaks towards to end of
        the signal. To work well delta should be set to delta >= RMSnoise * 5.
        (default: 0)
            delta function causes a 20% decrease in speed, when omitted
            Correctly used it can double the speed of the function

    return -- ym, xm : the maximum values and corresponding x values
    ''' ## I don't need the minimum peaks feature, but the algorithm ## doesn't work as expected if I comment out anything that deals ## with the minimum. And I'm too lazy to fix it heh. # Used to pop the first hit which almost always is false # Store data length for later use # Perform some checks # Maxima and minima candidates are temporarily stored in # mx and mn respectively # Only detect peak if there is 'lookahead' amount of points after it #### LOOK FOR MAX #### # Maxima peak candidate found # Look ahead in signal to ensure that this is a peak and not jitter # Set algorithm to only find minima now # End is within lookahead no more peaks can be found #### LOOK FOR MIN #### # Minima peak candidate found # Look ahead in signal to ensure that this is a peak and not jitter # Set algorithm to only find maxima now # End is within lookahead no more peaks can be found # Remove the false hit on the first value of the y_axis # No peaks were found, do nothing ## Return only the maximum, that's all I need from this method
xarr
yarr
lookahead
delta