Using the UHD Python API to control USRPs: Spectrum analyzer example



I recently discovered the UHD Python API for controlling USRPs. It is really nice, and the performance isn't too shabby, when you use numpy for signal processing. You have nearly all of the UHD C API available on Python, and some additional high level calls, such as recv_num_samples.

We're currently in the process of implementing an open source software defined ionosonde using just the UHD Python API. Stay tuned.

Here's a simple example, which implements a spectrum analyzer. We've been using this to test that our system meets the frequency licensing requirements:


#!/usr/bin/env python3
import numpy as n
import uhd
import scipy.signal as ss
import time
import matplotlib.pyplot as plt
import h5py

def acquire_spectrum(freq=12.5e6,

                     sample_rate=25e6,
                     N=250000, 
                     N_windows=10000, 
                     subdev="A:A",
                     ofname="spec.h5"):


    usrp = uhd.usrp.MultiUSRP("recv_buff_size=500000000")
    subdev_spec=uhd.usrp.SubdevSpec(subdev)
    usrp.set_rx_subdev_spec(subdev_spec)

    # 100 Hz frequency resolution
    N=250000
    w=ss.blackmanharris(N)
    freqv=n.fft.fftshift(n.fft.fftfreq(N,d=1/25e6))+freq
    S=n.zeros(N)
    Nw=N_windows
    for i in range(Nw):
        print("%d/%d"%(i,Nw))
        samps = usrp.recv_num_samps(N, freq, 25000000, [0], 0)
 
        if len(samps[0]) == N:
            z=samps[0]
            z=z-n.mean(z)
            S+=n.abs(n.fft.fftshift(n.fft.fft(z*w)))**2.0
        else:
            print(len(samps[0]))

    h=h5py.File(ofname,"w")
    h["spec"]=S
    h["freq"]=freqv
    h.close()
    plt.plot(freqv/1e6,10.0*n.log10(S))
    plt.show()


if __name__ == "__main__":
    acquire_spectrum()

Comments

Post a comment

Popular posts