Ejemplo n.º 1
0
    def __init__(self,
                 in_url,
                 samp_rate,
                 channel_bw,
                 freq=0,
                 gain=None,
                 fft_nbins=None,
                 decimation=None,
                 threshold=None,
                 out_url='stdout://',
                 rounds=-1,
                 spectrum_out=None):
        gr.top_block.__init__(self, "RSTT Signal Detector")
        threshold = threshold or (4, 6)

        if fft_nbins is None:
            # set FFT size to get 10 or more bins per channel
            fft_nbins = 10. * samp_rate / channel_bw
            fft_nbins = 2**int(math.ceil(math.log(fft_nbins, 2)))

        if decimation is None:
            # average samples for at least one second
            decimation = int(math.ceil(float(samp_rate) / fft_nbins))

        ntaps = int(2. * fft_nbins / samp_rate * channel_bw)

        if isinstance(threshold, (float, int)):
            threshold = (threshold, )

        self.in_url = in_url
        self.channel_bw = channel_bw
        self.samp_rate = samp_rate
        self.fft_nbins = fft_nbins
        self.decimation = decimation
        if spectrum_out:
            self.spectrum_out = open(spectrum_out, 'w')
        else:
            self.spectrum_out = None
        self.threshold_lo = min(threshold)
        self.threshold_hi = max(threshold)
        self.rounds = rounds
        self.freq = freq
        self.signals = []
        # TODO: parametrize
        self.ttl = 1

        args = in_url.split('://', 1)
        if len(args) == 2:
            prefix, args = args
        else:
            prefix, args = "", args[0]
        if prefix == 'osmo-sdr':
            self.src = osmosdr.source(args="numchan=1 " + args)
            self.src.set_sample_rate(samp_rate)
            self.src.set_center_freq(freq, 0)
            if gain is None:
                self.src.set_gain_mode(True, 0)
            else:
                self.src.set_gain_mode(False)
                self.src.set_gain(gain)
                #self.src.set_if_gain(37)
        elif prefix == 'file' or prefix == "":
            self.src = blocks.file_source(gr.sizeof_gr_complex, args, False)
        elif prefix == 'udp':
            host, port = args.split(':', 1)
            self.src = blocks.udp_source(gr.sizeof_gr_complex, host, int(port),
                                         65536, True)
        else:
            raise ValueError('Unsupported schema: "%s"' % in_url)

        self.signal_detector = signal_detector(samp_rate,
                                               decimation,
                                               mk_cos_taps(ntaps),
                                               threshold=self.threshold_lo,
                                               fft_size=fft_nbins,
                                               center_freq=freq)
        self.signal_detector.set_signals_detected_handler(
            self.on_signals_detected)

        self.connect((self.src, 0), (self.signal_detector, 0))

        args = out_url.split('://')
        if len(args) == 2:
            prefix, args = args
        else:
            prefix, args = "", args[0]
        if prefix == "file" or prefix == "":
            self.emit_signals = self._emit_signals_file
            self._out = open(args, 'w')
        elif prefix == "stdout":
            self.emit_signals = self._emit_signals_stdout
        else:
            raise ValueError('Unsupported schma: "%s"' % out_url)
def main(argv):
    import ctypes
    import os

    global freq_min, freq_max, freq_step, freq_scan_interval, threshold, threshold_noise, result_dir, use_list, bandwidth, auto_recog, read_database_file, compare_bw

    help_string = (
        "DVB-T Signal Detector by A. Rahmadhani\n"
        + "--------------------------------------\n\n"
        + "Usage: signal_detector_main.py [options]\n\n"
        + "[options]:\n"
        + "  -h : help\n"
        + "  -r : try to recognize signal automatically\n"
        + "  -q : use known DVB-T frequency list\n"
        + "  -c : compare bandwidth to identify signal\n"
        + "  -o <file_name> : read known frequency from file\n"
        + "  -l <min_freq> : minimum frequency (Hz)\n"
        + "  -u <max_freq> : maximum frequency (Hz)\n"
        + "  -s <freq_step> : frequency_step \n"
        + "  -i <interval> : interval or waiting time (s)\n"
        + "  -t <threshold> : threshold value (dB)\n"
        + "  -p <auto_threshold_diff> : percentage of threshold stdev\n"
        + "  -b <bandwidth> : specify DVB-T bandwidth (Hz)h\n"
    )

    # Read command line arguments
    try:
        opts, args = getopt.getopt(
            argv,
            "hrqcl:u:s:i:t:p:o:b:",
            [
                "help",
                "auto",
                "lfreq=",
                "ufreq=",
                "step=",
                "interval=",
                "threshold=",
                "threshold_diff=",
                "open_data=",
                "bandwidth=",
                "compare",
            ],
        )

    except getopt.GetoptError:
        print help_string
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print help_string
            sys.exit()
        elif opt in ("-r", "--auto"):
            auto_recog = 1
        elif opt in ("-q", "--uselist"):
            use_list = 1
        elif opt in ("-l", "--lfreq"):
            freq_min = float(arg)
        elif opt in ("-u", "--hfreq"):
            freq_max = float(arg)
        elif opt in ("-s", "--step"):
            freq_step = float(arg)
        elif opt in ("-i", "--interval"):
            freq_scan_interval = float(arg)
        elif opt in ("-t", "--threshold"):
            threshold = float(arg)
        elif opt in ("-p", "--threshold_diff"):
            threshold_noise = float(arg)
        elif opt in ("-o", "--open_data"):
            read_database_file = str(arg)
        elif opt in ("-b", "--bandwidth"):
            bandwidth = float(arg)
        elif opt in ("-c", "--compare"):
            compare_bw = 1

    # GUI from GNUradio companion
    if os.name == "posix":
        try:
            x11 = ctypes.cdll.LoadLibrary("libX11.so")
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

    # Create result dir if not exists
    try:
        os.makedirs(result_dir)
    except OSError:
        pass

    app = signal_detector()
    app.Start(True)
    # Start new thread to scan frequency
    thread.start_new_thread(scan_freq, (app, freq_min, freq_max, freq_step, freq_scan_interval))

    # Wait for GUI thread
    app.Wait()
    # Exit properly when GUI closed
    sys.exit(0)
Ejemplo n.º 3
0
    def __init__(self, in_url, samp_rate, channel_bw, freq=0, gain=None,
            fft_nbins=None, decimation=None, threshold=None,
            out_url='stdout://', rounds=-1, spectrum_out=None):
        gr.top_block.__init__(self, "RSTT Signal Detector")
        threshold = threshold or (4,6)

        if fft_nbins is None:
            # set FFT size to get 10 or more bins per channel
            fft_nbins = 10. * samp_rate / channel_bw
            fft_nbins = 2**int(math.ceil(math.log(fft_nbins, 2)))

        if decimation is None:
            # average samples for at least one second
            decimation = int(math.ceil(float(samp_rate) / fft_nbins))

        ntaps = int(2. * fft_nbins / samp_rate * channel_bw)

        if isinstance(threshold, (float, int)):
            threshold = (threshold, )

        self.in_url = in_url
        self.channel_bw = channel_bw
        self.samp_rate = samp_rate
        self.fft_nbins = fft_nbins
        self.decimation = decimation
        if spectrum_out:
            self.spectrum_out = open(spectrum_out, 'w')
        else:
            self.spectrum_out = None
        self.threshold_lo = min(threshold)
        self.threshold_hi = max(threshold)
        self.rounds = rounds
        self.freq = freq
        self.signals = []
# TODO: parametrize
        self.ttl = 1

        args = in_url.split('://', 1)
        if len(args) == 2:
            prefix, args = args
        else:
            prefix, args = "", args[0]
        if prefix == 'osmo-sdr':
            self.src = osmosdr.source( args="numchan=1 " + args )
            self.src.set_sample_rate(samp_rate)
            self.src.set_center_freq(freq, 0)
            if gain is None:
                self.src.set_gain_mode(True, 0)
            else:
                self.src.set_gain_mode(False)
                self.src.set_gain(gain)
                #self.src.set_if_gain(37)
        elif prefix == 'file' or prefix == "":
            self.src = blocks.file_source(gr.sizeof_gr_complex, args, False)
        elif prefix == 'udp':
            host, port = args.split(':', 1)
            self.src = blocks.udp_source(gr.sizeof_gr_complex, host, int(port), 65536, True)
        else:
            raise ValueError('Unsupported schema: "%s"' % in_url)

        self.signal_detector = signal_detector(samp_rate, decimation,
                mk_cos_taps(ntaps), threshold=self.threshold_lo, fft_size=fft_nbins,
                center_freq=freq)
        self.signal_detector.set_signals_detected_handler(self.on_signals_detected)

        self.connect((self.src, 0), (self.signal_detector, 0))

        args = out_url.split('://')
        if len(args) == 2:
            prefix, args = args
        else:
            prefix, args = "", args[0]
        if prefix == "file" or prefix == "":
            self.emit_signals = self._emit_signals_file
            self._out = open(args, 'w')
        elif prefix == "stdout":
            self.emit_signals = self._emit_signals_stdout
        else:
            raise ValueError('Unsupported schma: "%s"' % out_url)