# Path: wildfind/harrier/collar.py
# CW, AM = range(2)
# MOD_DESC = ['CW', 'AM']
# class Collar(object):
#     def __init__(self, count, rate, level, width):
#     def get_modulation(self):
#     def get_description(self):
#     def get_dict(self, timeStamp):
# 
# Path: wildfind/harrier/constants.py
# SAMPLE_TIME = 4
# 
# Path: wildfind/harrier/utils.py
# class Utils(object):
#     # Print error and exit
#     @staticmethod
#     def error(error, fatal=True):
#         if fatal:
#             sys.exit(error)
# 
#         sys.stderr.write(error + '\n')
# 
#     # Return ranges based on tolerance
#     @staticmethod
#     def calc_tolerances(values, tolerance):
#         valuesMin = [value * (100 - tolerance) / 100. for value in values]
#         valuesMax = [value * (100 + tolerance) / 100. for value in values]
#         return zip(valuesMax, valuesMin)


import itertools
import operator
import numpy
from scipy import fftpack
from wildfind.harrier import collar
from wildfind.harrier.constants import SAMPLE_TIME
from wildfind.harrier.utils import Utils
                # Constant rate?
                maxDeviation = pulseAvg * PULSE_RATE_DEVIATION / 100.
                if numpy.std(pulseRate) < maxDeviation:
                    # Calculate frequency
                    freq = length / (pulseAvg * float(SAMPLE_TIME))
                    rate = freq * 60
                    # Limit to PULSE_RATES
                    closest = min(PULSE_RATES,
                                  key=lambda x: abs(x - rate))
                    if (abs(closest - rate)) <= closest * PULSE_RATE_TOL / 100.0:
                        # Test for missing pulses
                        if pulseValid[0] - min(pulseRate) < 0 and pulseValid[-1] + min(pulseRate) > length:
                            # Get pulse levels
                            level = 0
                            for posValid in range(len(pulseValid)):
                                pos = pulseValid[posValid]
                                width = widths[posValid]
                                pulseSignal = signal[pos:pos + width - 1]
                                level += numpy.average(pulseSignal)
                            level /= len(pulseValid)
                            # Store valid pulse
                            pulse = collar.Collar(widths.size,
                                                  freq * 60.,
                                                  level,
                                                  width * SAMPLE_TIME * 1000. / length)
                            break
                        elif self._debug is not None and self._debug.verbose:
                            Utils.error('Missing pulses',
                                        False)
                    elif self._debug is not None and self._debug.verbose:

# Path: wildfind/harrier/collar.py
# CW, AM = range(2)
# MOD_DESC = ['CW', 'AM']
# class Collar(object):
#     def __init__(self, count, rate, level, width):
#     def get_modulation(self):
#     def get_description(self):
#     def get_dict(self, timeStamp):
# 
# Path: wildfind/harrier/constants.py
# SAMPLE_TIME = 4
# 
# Path: wildfind/harrier/utils.py
# class Utils(object):
#     # Print error and exit
#     @staticmethod
#     def error(error, fatal=True):
#         if fatal:
#             sys.exit(error)
# 
#         sys.stderr.write(error + '\n')
# 
#     # Return ranges based on tolerance
#     @staticmethod
#     def calc_tolerances(values, tolerance):
#         valuesMin = [value * (100 - tolerance) / 100. for value in values]
#         valuesMax = [value * (100 + tolerance) / 100. for value in values]
#         return zip(valuesMax, valuesMin)


# Path: wildfind/harrier/detect.py
import itertools
import operator
import numpy
from scipy import fftpack
from wildfind.harrier import collar
from wildfind.harrier.constants import SAMPLE_TIME
from wildfind.harrier.utils import Utils

                # Constant rate?
                maxDeviation = pulseAvg * PULSE_RATE_DEVIATION / 100.
                if numpy.std(pulseRate) < maxDeviation:
                    # Calculate frequency
                    freq = length / (pulseAvg * float(SAMPLE_TIME))
                    rate = freq * 60
                    # Limit to PULSE_RATES
                    closest = min(PULSE_RATES,
                                  key=lambda x: abs(x - rate))
                    if (abs(closest - rate)) <= closest * PULSE_RATE_TOL / 100.0:
                        # Test for missing pulses
                        if pulseValid[0] - min(pulseRate) < 0 and pulseValid[-1] + min(pulseRate) > length:
                            # Get pulse levels
                            level = 0
                            for posValid in range(len(pulseValid)):
                                pos = pulseValid[posValid]
                                width = widths[posValid]
                                pulseSignal = signal[pos:pos + width - 1]
                                level += numpy.average(pulseSignal)
                            level /= len(pulseValid)
                            # Store valid pulse
                            pulse = collar.Collar(widths.size,
                                                  freq * 60.,
                                                  level,
                                                  width * SAMPLE_TIME * 1000. / length)
                            break
                        elif self._debug is not None and self._debug.verbose:
                            Utils.error('Missing pulses',
                                        False)
                    elif self._debug is not None and self._debug.verbose:

                        Utils.error('Invalid rate {:.1f}PPM'.format(rate),