Using the RedPitaya in a Jupyther Notebook

Using Jupyter notebooks for rapid prototyping, short proof of concepts and documentation is fun. Now I wanted to introduce some signal aquisition using the RedPitaya (+ http://pavel-demin.github.io/red-pitaya-notes/sdr-transceiver/).

Pretty simple task. Two years ago I’ve contributed a short python script to fetch samples directly from the trx project. Pretty simple to include it directly into a notebook:

fs_to_indexfs = {20000:0, 50000:1, 100000:2, 250000:3, 500000:4, 1250000:5}

class Transceiver(object):
    def __init__(self, ipaddr):
        self.p_ip_address = ipaddr
        self.control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def shutdown(self):
        print("RX Shutdown")        
        self.data_socket.close()
        self.control_socket.close()

    def start_rx(self, freq, fs_index, corr, n_bytes, iq_filename):
        # Setup control socket
        self.control_socket.connect((self.p_ip_address, 1001))
        self.control_socket.send(struct.pack('<I', 0))

        # Setup data socket
        self.data_socket.connect((self.p_ip_address, 1001))
        self.data_socket.send(struct.pack('<I', 1))

        # Set fc and fs
        self.control_socket.send(struct.pack('<I', 0<<28 | int((1.0 + 1e-6 * corr ) * freq)))
        self.control_socket.send(struct.pack('<I', 1<<28 | fs_index))
        
        fs_value = list(fs_to_indexfs.keys())[list(fs_to_indexfs.values()).index(fs_index)];

        # write aquisition parameters to console
        print(
            "...receiving fc: {0} Hz fs: {1}  / correction (ppm): {2}\n".format(freq, fs_value,
                                                                                 corr))       
        cnt_bytes = 0
        
        try:
            os.remove(iq_filename)
        except OSError:
            pass
        
        f_iq_raw=open (iq_filename, "wb")
        
        try:
            while cnt_bytes < n_bytes: f_iq_raw.write(self.data_socket.recv(1024)) cnt_bytes+=1024; print("fetched " + str(int(n_bytes)) + " bytes") f_iq_raw.close() self.shutdown() except Exception as ex: print(ex) sys.stderr.write(">>> rx force shutdown\n")
            self.shutdown()
            f_iq_raw.close()      

The acquisition looks like

trx = Transceiver(arg_addr)
start = time.time()
activeReceiver = trx;

trx.start_rx(arg_fc, fs_to_indexfs[arg_fs], 0,arg_bytes_to_fetch, arg_raw_iq_file)

end = time.time() - start
print(time.strftime("%H:%M:%S", time.gmtime(end)))


samples = np.fromfile(arg_raw_iq_file, dtype=np.complex64)
x1 = np.array(samples).astype("complex")
print(len(x1))

That’s it.
Now there is some numpy / matplotlib fun ahead. Let’s quickly render e spectrogram:

Leave a Reply

Your email address will not be published. Required fields are marked *