Ejemplo n.º 1
0
    def rx_samples(self):
        """
        Reads and returns the specifed number 
        of IQ samples from the device.

        Returns
        -------
        * samples                       :  (np.array) A numpy array of samples of 
                                            complex type is returned. (Normalized)
        """
        frame_latency, raw_data = self.clib.py_rtlsdr_read_sync(
            self.__dev_ptr, self.num_recv_samples)
        iq = raw_data.astype(np.float64).view(np.complex128)
        iq /= 127.5
        iq -= (1 + 1j)

        empirical_sample_rate = (self.num_recv_samples / frame_latency) * 1000
        if empirical_sample_rate < self.sample_rate:
            if self.__logging_level < 4:
                print_warn_msg(
                    "Empirical sample rate: %.4f MSPS. Requested sample rate: %d MSPS."
                    % (empirical_sample_rate, self.sample_rate))

        if self.__logging_level < 3:
            print_info_msg(
                "Empirical sample rate: %.4f MSPS. Requested sample rate: %d MSPS."
                % (empirical_sample_rate, self.sample_rate))

        return iq
Ejemplo n.º 2
0
def signal_handler(sdr):
    """
    Used to handle SIGINT.
    """
    print_info_msg("Caught Ctrl-C. Clearing resources. Exiting.")
    sdr.close()
    exit(0)
Ejemplo n.º 3
0
 def print_device_configuration(self, ):
     """
     Prints the correct device configuration to
     the console.
     """
     device_config = 'Current device settings.'
     device_config += '\n\t1. Center Freq: %d Hz.' % (self.center_freq)
     device_config += '\n\t2. Sample Rate: %d MSPS.' % (self.sample_rate)
     device_config += '\n\t3. AGC Enabled: %s.' % (self.enable_agc)
     device_config += '\n\t4. Automatic tuner gain selection: %s.' % (
         self.enable_auto_tuner_gain)
     device_config += '\n\t5. Freq Correction: %d ppm' % (
         self.freq_correction)
     device_config += '\n\t6. Tuner gain: %s dB' % (self.tuner_gain)
     device_config += '\n\t7. Frame size (samples/frame): %d' % (
         self.num_recv_samples)
     print_info_msg(device_config)
Ejemplo n.º 4
0
    def __init_default(self):
        """
        Intializes the default values of the SDR.
        """
        if self.__logging_level < 3:
            print_info_msg("Intializing device with default values.")

        center_freq = 980e6  # 980 Mhz
        sample_rate = 2e6  # 2 MSPS
        agc = True  # Enable AGC
        auto_lna_gain = True  # Enable automatic lna gain selection
        auto_tuner_gain_mode = True  # Set tuner gain mode to auto.
        num_samples = 512  # Number of samples to read from SDR

        self.center_freq = center_freq
        self.sample_rate = sample_rate
        self.enable_agc = agc
        self.enable_auto_tuner_gain = auto_tuner_gain_mode
        self.num_recv_samples = num_samples
Ejemplo n.º 5
0
    def __init__(self, device_index, logging_level=3):
        self.__librtlsdr = librtlsdr()

        if int(device_index) != device_index:
            print_error_msg("Expected device index to be of int. Got: %s." %
                            (type(device_index)))
            raise TypeError

        device_index = int(device_index)

        self.__device_index = device_index

        if int(logging_level) != logging_level:
            print_error_msg("Expected logging level to be int. Got: %s" %
                            (type(logging_level)))
            raise TypeError

        logging_level = int(logging_level)

        if logging_level < 1 or logging_level > 4:
            print_error_msg("Invalid logging level %d." % (logging_level))
            raise ValueError

        # Setting the logging level.
        self.__logging_level = logging_level

        # Open a device pointer to the SDR.
        self.__dev_ptr = c_void_p(None)
        self.__dev_ptr = self.clib.py_rtlsdr_open(device_index)

        if self.__logging_level == 1:
            print_success_msg(
                "Successfully opened a libusb connection to the device.")

        # Get SDR details
        self.__mid, self.__vid, self.__serial = self.clib.py_rtlsdr_get_device_usb_strings(
            self.__device_index)
        if self.__logging_level < 3:
            device_strings = "Manufacturer: %s, Vendor ID: %s, Serial %s." % (
                self.__mid, self.__vid, self.__serial)
            print_info_msg(device_strings)

        # Attributes
        self.__center_freq = None
        self.__sample_rate = None
        self.__enable_agc = None
        self.__tuner_gain = None
        self.__enable_auto_tuner_gain = None
        self.__tuner_gains = self.clib.py_rtlsdr_get_tuner_gains(
            self.__dev_ptr)
        self.__freq_correction = self.clib.py_rtlsdr_get_freq_correction(
            self.__dev_ptr)
        self.__rtl_xo_freq, self.__tuner_xo_freq = self.clib.py_rtlsdr_get_xtal_freq(
            self.__dev_ptr)
        self.__num_recv_samples = None

        # Init defaults
        self.__init_default()

        # Reset libusb buffer
        self.clib.py_rtlsdr_reset_buffer(self.__dev_ptr)

        if self.__logging_level < 3:
            device_config = 'Intialized device with following default values.'
            device_config += '\n\t1. Center Freq: %d Hz.' % (self.center_freq)
            device_config += '\n\t2. Sample Rate: %d MSPS.' % (
                self.sample_rate)
            device_config += '\n\t3. AGC Enabled: %s.' % (self.enable_agc)
            device_config += '\n\t4. Automatic tuner gain selection: %s.' % (
                self.enable_auto_tuner_gain)
            device_config += '\n\t5. Freq Correction: %d ppm' % (
                self.freq_correction)
            device_config += '\n\t6. Tuner gain: %s dB' % (self.tuner_gain)
            device_config += '\n\t7. Frame size (samples/frame): %d' % (
                self.num_recv_samples)
            print_info_msg(device_config)
Ejemplo n.º 6
0
    """
    Used to handle SIGINT.
    """
    print_info_msg("Caught Ctrl-C. Clearing resources. Exiting.")
    sdr.close()
    exit(0)


if __name__ == "__main__":

    # Create an object of the librtlsdr
    lrtlsdr = librtlsdr()

    if args.query_device == 1:
        attached_devices = lrtlsdr.py_rtlsdr_get_device_count()
        print_info_msg("Number of RTL-SDR device(s) attached to the host: %d" %
                       (attached_devices))
        exit(0)

    sdr = Radio(device_index=args.device_index,
                logging_level=args.logging_level)

    # Check if querying for supported lna gain values.
    if args.query_gains == 1:
        print_info_msg("Supported Tuner/LNA gain values in dB: %s" %
                       (sdr.tuner_gains))
        exit(0)

    # set the radio properties
    sdr.center_freq = args.center_freq
    sdr.sample_rate = args.sample_rate
    if args.enable_agc == 1: