# Path: rtlsdr_scanner/constants.py
# SAMPLE_RATE = 2e6
# 
# BANDWIDTH = 500e3
# 
# WINFUNC = ["Bartlett", numpy.bartlett,
#            "Blackman", numpy.blackman,
#            "Hamming", numpy.hamming,
#            "Hanning", numpy.hanning]
# 
# Path: rtlsdr_scanner/events.py
# class EventThread(wx.PyEvent):
#     def __init__(self, status, arg1=None, arg2=None):
#         wx.PyEvent.__init__(self)
#         self.SetEventType(EVENT_THREAD)
#         self.data = Status(status, arg1, arg2)
# 
# class Event(object):
#     STARTING, STEPS, INFO, DATA, STOPPED, ERROR, FINISHED, PROCESSED, \
#         CAL, LEVEL, UPDATED, DRAW, \
#         DELAY_COUNT, DELAY_START, \
#         LOC, LOC_RAW, LOC_WARN, LOC_ERR, LOC_SAT = range(19)
# 
# def post_event(destination, status):
#     if isinstance(destination, Queue.Queue):
#         destination.put(status)
#     elif isinstance(destination, wx.EvtHandler):
#         wx.PostEvent(destination, status)
# 
# Path: rtlsdr_scanner/rtltcp.py
# class RtlTcp(object):
#     def __init__(self, host, port, notify):
#         self.host = host
#         self.port = port
# 
#         self.threadBuffer = None
#         self.tuner = 0
#         self.rate = 0
# 
#         self.__setup(notify)
# 
#     def __setup(self, notify):
#         self.threadBuffer = ThreadBuffer(self.host, self.port, notify)
#         self.__get_header()
# 
#     def __get_header(self):
#         header = self.threadBuffer.get_header()
#         if len(header) == 12:
#             if header.startswith('RTL0'):
#                 self.tuner = ((ord(header[4]) << 24) |
#                               (ord(header[5]) << 16) |
#                               (ord(header[6]) << 8) |
#                               ord(header[7]))
# 
#     def __send_command(self, command, data):
#         send = array.array('c', '\0' * 5)
# 
#         struct.pack_into('>l', send, 1, data)
#         send[0] = struct.pack('<b', command)
# 
#         self.threadBuffer.sendall(send)
# 
#     def __read_raw(self, samples):
#         return self.threadBuffer.recv(samples * 2)
# 
#     def __raw_to_iq(self, raw):
#         iq = numpy.empty(len(raw) / 2, 'complex')
#         iq.real, iq.imag = raw[::2], raw[1::2]
#         iq /= (255 / 2)
#         iq -= 1
# 
#         return iq
# 
#     def set_sample_rate(self, rate):
#         self.__send_command(RtlTcpCmd.SET_SAMPLE_RATE, rate)
#         self.rate = rate
# 
#     def set_manual_gain_enabled(self, mode):
#         self.__send_command(RtlTcpCmd.SET_GAIN_MODE, mode)
# 
#     def set_gain(self, gain):
#         self.__send_command(RtlTcpCmd.SET_GAIN, gain * 10)
# 
#     def set_center_freq(self, freq):
#         self.__send_command(RtlTcpCmd.SET_FREQ, freq)
#         self.__read_raw(int(self.rate * 2 * 0.1))
# 
#     def get_tuner_type(self):
#         return self.tuner
# 
#     def read_samples(self, samples):
# 
#         raw = self.__read_raw(samples)
#         return self.__raw_to_iq(raw)
# 
#     def close(self):
#         self.threadBuffer.abort()
#         self.threadBuffer.join()


from collections import OrderedDict
from rtlsdr_scanner.constants import SAMPLE_RATE, BANDWIDTH, WINFUNC
from rtlsdr_scanner.events import EventThread, Event, post_event
from rtlsdr_scanner.rtltcp import RtlTcp
import itertools
import math
import threading
import time
import matplotlib
import rtlsdr
    with lock:
        updated = False
        if average:
            if len(spectrum) > 0:
                timeStamp = min(spectrum)
            else:
                timeStamp = data[0]
        else:
            timeStamp = data[0]

        freqCentre = data[1]
        scan = data[2]

        upperStart = freqCentre + offset
        upperEnd = freqCentre + offset + BANDWIDTH / 2
        lowerStart = freqCentre - offset - BANDWIDTH / 2
        lowerEnd = freqCentre - offset

        if timeStamp not in spectrum:
            spectrum[timeStamp] = OrderedDict()

        for freq in scan:
            if start <= freq < stop:
                power = 10 * math.log10(scan[freq])
                if upperStart <= freq * 1e6 <= upperEnd or \
                   lowerStart <= freq * 1e6 <= lowerEnd:
                    if freq in spectrum[timeStamp]:
                        spectrum[timeStamp][freq] = \
                            (spectrum[timeStamp][freq] + power) / 2
                        if alertLevel is not None and (spectrum[timeStamp][freq] >

# Path: rtlsdr_scanner/constants.py
# SAMPLE_RATE = 2e6
# 
# BANDWIDTH = 500e3
# 
# WINFUNC = ["Bartlett", numpy.bartlett,
#            "Blackman", numpy.blackman,
#            "Hamming", numpy.hamming,
#            "Hanning", numpy.hanning]
# 
# Path: rtlsdr_scanner/events.py
# class EventThread(wx.PyEvent):
#     def __init__(self, status, arg1=None, arg2=None):
#         wx.PyEvent.__init__(self)
#         self.SetEventType(EVENT_THREAD)
#         self.data = Status(status, arg1, arg2)
# 
# class Event(object):
#     STARTING, STEPS, INFO, DATA, STOPPED, ERROR, FINISHED, PROCESSED, \
#         CAL, LEVEL, UPDATED, DRAW, \
#         DELAY_COUNT, DELAY_START, \
#         LOC, LOC_RAW, LOC_WARN, LOC_ERR, LOC_SAT = range(19)
# 
# def post_event(destination, status):
#     if isinstance(destination, Queue.Queue):
#         destination.put(status)
#     elif isinstance(destination, wx.EvtHandler):
#         wx.PostEvent(destination, status)
# 
# Path: rtlsdr_scanner/rtltcp.py
# class RtlTcp(object):
#     def __init__(self, host, port, notify):
#         self.host = host
#         self.port = port
# 
#         self.threadBuffer = None
#         self.tuner = 0
#         self.rate = 0
# 
#         self.__setup(notify)
# 
#     def __setup(self, notify):
#         self.threadBuffer = ThreadBuffer(self.host, self.port, notify)
#         self.__get_header()
# 
#     def __get_header(self):
#         header = self.threadBuffer.get_header()
#         if len(header) == 12:
#             if header.startswith('RTL0'):
#                 self.tuner = ((ord(header[4]) << 24) |
#                               (ord(header[5]) << 16) |
#                               (ord(header[6]) << 8) |
#                               ord(header[7]))
# 
#     def __send_command(self, command, data):
#         send = array.array('c', '\0' * 5)
# 
#         struct.pack_into('>l', send, 1, data)
#         send[0] = struct.pack('<b', command)
# 
#         self.threadBuffer.sendall(send)
# 
#     def __read_raw(self, samples):
#         return self.threadBuffer.recv(samples * 2)
# 
#     def __raw_to_iq(self, raw):
#         iq = numpy.empty(len(raw) / 2, 'complex')
#         iq.real, iq.imag = raw[::2], raw[1::2]
#         iq /= (255 / 2)
#         iq -= 1
# 
#         return iq
# 
#     def set_sample_rate(self, rate):
#         self.__send_command(RtlTcpCmd.SET_SAMPLE_RATE, rate)
#         self.rate = rate
# 
#     def set_manual_gain_enabled(self, mode):
#         self.__send_command(RtlTcpCmd.SET_GAIN_MODE, mode)
# 
#     def set_gain(self, gain):
#         self.__send_command(RtlTcpCmd.SET_GAIN, gain * 10)
# 
#     def set_center_freq(self, freq):
#         self.__send_command(RtlTcpCmd.SET_FREQ, freq)
#         self.__read_raw(int(self.rate * 2 * 0.1))
# 
#     def get_tuner_type(self):
#         return self.tuner
# 
#     def read_samples(self, samples):
# 
#         raw = self.__read_raw(samples)
#         return self.__raw_to_iq(raw)
# 
#     def close(self):
#         self.threadBuffer.abort()
#         self.threadBuffer.join()


# Path: rtlsdr_scanner/scan.py
from collections import OrderedDict
from rtlsdr_scanner.constants import SAMPLE_RATE, BANDWIDTH, WINFUNC
from rtlsdr_scanner.events import EventThread, Event, post_event
from rtlsdr_scanner.rtltcp import RtlTcp
import itertools
import math
import threading
import time
import matplotlib
import rtlsdr

    with lock:
        updated = False
        if average:
            if len(spectrum) > 0:
                timeStamp = min(spectrum)
            else:
                timeStamp = data[0]
        else:
            timeStamp = data[0]

        freqCentre = data[1]
        scan = data[2]

        upperStart = freqCentre + offset
        upperEnd = freqCentre + offset + BANDWIDTH / 2
        lowerStart = freqCentre - offset - BANDWIDTH / 2
        lowerEnd = freqCentre - offset

        if timeStamp not in spectrum:
            spectrum[timeStamp] = OrderedDict()

        for freq in scan:
            if start <= freq < stop:
                power = 10 * math.log10(scan[freq])
                if upperStart <= freq * 1e6 <= upperEnd or \
                   lowerStart <= freq * 1e6 <= lowerEnd:
                    if freq in spectrum[timeStamp]:
                        spectrum[timeStamp][freq] = \
                            (spectrum[timeStamp][freq] + power) / 2
                        if alertLevel is not None and (spectrum[timeStamp][freq] >

                                                       alertLevel):