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

  1. The article demonstrates how Python can be used to control USRPs through the UHD Python API while performing signal-processing tasks with NumPy and SciPy. The spectrum analyzer example combines hardware control, numerical processing, FFT-based frequency analysis, and signal-windowing techniques, showing how Python can support practical software-defined radio workflows. NumPy Online Training

    ReplyDelete
  2. The spectrum analyzer example shows how Python can be combined with software-defined radio hardware to acquire and analyze frequency-domain signals. Using the UHD API with numerical processing makes it possible to collect samples from a USRP, apply signal-processing operations, and examine the resulting spectrum for practical RF testing and measurement tasks. Audio Processing Projects

    ReplyDelete
  3. The use of NumPy, SciPy, and visualization tools also demonstrates how computational techniques can support signal analysis workflows. Although the example is focused on USRP-based spectrum measurement rather than conventional audio, the underlying processing concepts provide a useful foundation for projects involving digital signals and advanced computational analysis. Deep Learning Projects for Final Year

    ReplyDelete

Post a Comment

Popular Posts