Skip to content

Instantly share code, notes, and snippets.

@AlexFWulff
Created November 9, 2021 16:17
Show Gist options
  • Save AlexFWulff/9c821eb999e9da86404a163fb35b2656 to your computer and use it in GitHub Desktop.
Save AlexFWulff/9c821eb999e9da86404a163fb35b2656 to your computer and use it in GitHub Desktop.
import SoapySDR
from SoapySDR import *
import numpy as np
fsamp = 8e6
fcenter = 107.9e6
nsamp = 2**20
# Apparently it takes AGC a while to settle in?
throwaway_time = 0.02
args = dict(driver="bladerf")
sdr = SoapySDR.Device(args)
# Setup device
sdr.setSampleRate(SOAPY_SDR_RX, 0, fsamp)
sdr.setSampleRate(SOAPY_SDR_RX, 1, fsamp)
sdr.setFrequency(SOAPY_SDR_RX, 0, fcenter)
sdr.setFrequency(SOAPY_SDR_RX, 1, fcenter)
sdr.setGainMode(SOAPY_SDR_RX, 0, True)
sdr.setGainMode(SOAPY_SDR_RX, 1, True)
# Make stream and buffers
rx = sdr.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, [0, 1])
mtu = int(sdr.getStreamMTU(rx))
samps = np.zeros((nsamp,2), dtype=np.complex64)
buff0 = np.array([0]*mtu, np.complex64)
buff1 = np.array([0]*mtu, np.complex64)
sdr.activateStream(rx)
throwaway_samps = int(throwaway_time*fsamp)
throwaway_rec = 0
total_rec = 0
while total_rec < nsamp:
out = sdr.readStream(rx, [buff0, buff1], mtu)
fetched = out.ret
if fetched < 1:
print(out)
continue
throwaway_rec = throwaway_rec + fetched
if throwaway_rec < throwaway_samps:
continue
if total_rec+fetched < nsamp:
samps[total_rec:total_rec+fetched,0] = buff0[:fetched]
samps[total_rec:total_rec+fetched,1] = buff1[:fetched]
else:
extra_samps = total_rec+fetched-nsamp
samps[total_rec:,0] = buff0[:fetched-extra_samps]
samps[total_rec:,1] = buff1[:fetched-extra_samps]
total_rec = total_rec + fetched
sdr.deactivateStream(rx)
# Do stuff with samples here....
#f,t,spectro = SpectroUtils.get_spectro(samps[:,0], 4096, fsamp)
#plt.pcolormesh(spectro)
#plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment