# 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

class ThreadProcess(threading.Thread):
    def __init__(self, notify, freq, scan, cal, levelOff, nfft, overlap, winFunc):
        threading.Thread.__init__(self)
        self.name = 'ThreadProcess'
        self.notify = notify
        self.freq = freq
        self.scan = scan
        self.cal = cal
        self.levelOff = math.pow(10, levelOff / 10.0)
        self.nfft = nfft
        self.overlap = overlap
        self.winFunc = winFunc
        self.window = matplotlib.numpy.hamming(nfft)

    def run(self):
        spectrum = {}
        timeStamp = self.scan[0]
        samples = self.scan[1]
        pos = WINFUNC[::2].index(self.winFunc)
        function = WINFUNC[1::2][pos]

        powers, freqs = matplotlib.mlab.psd(samples,
                                            NFFT=self.nfft,
                                            Fs=SAMPLE_RATE / 1e6,
                                            window=function(self.nfft))
        for freqPsd, pwr in itertools.izip(freqs, powers):
            xr = freqPsd + (self.freq / 1e6)
            xr = xr + (xr * self.cal / 1e6)
            spectrum[xr] = pwr * self.levelOff

# 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


class ThreadProcess(threading.Thread):
    def __init__(self, notify, freq, scan, cal, levelOff, nfft, overlap, winFunc):
        threading.Thread.__init__(self)
        self.name = 'ThreadProcess'
        self.notify = notify
        self.freq = freq
        self.scan = scan
        self.cal = cal
        self.levelOff = math.pow(10, levelOff / 10.0)
        self.nfft = nfft
        self.overlap = overlap
        self.winFunc = winFunc
        self.window = matplotlib.numpy.hamming(nfft)

    def run(self):
        spectrum = {}
        timeStamp = self.scan[0]
        samples = self.scan[1]
        pos = WINFUNC[::2].index(self.winFunc)
        function = WINFUNC[1::2][pos]

        powers, freqs = matplotlib.mlab.psd(samples,
                                            NFFT=self.nfft,
                                            Fs=SAMPLE_RATE / 1e6,
                                            window=function(self.nfft))
        for freqPsd, pwr in itertools.izip(freqs, powers):
            xr = freqPsd + (self.freq / 1e6)
            xr = xr + (xr * self.cal / 1e6)
            spectrum[xr] = pwr * self.levelOff

        post_event(self.notify, EventThread(Event.PROCESSED,