Skip to content

libbladeRF Python API missing data #1068

Description

@Todesstuhl

Hallo everyone,

For some reason when I try to get RX-data from my BladeRF 2.0 Ax9 via the Python API the first ~20.000 values are garbage.
But this only happens in RX_2 mode, if I get the RX-data from ch1 & ch2 separately everything works fine.

I tried to reproduce the error with the BladeRF CLI on Windows, but these RX-data are complete.
Therefore I thing the erreor must by somewhere in the Python API.

Can someone confirm the error?

Image

My Setup:

flowchart LR
    SG[Signal Generator] --> C["90° Coupler"]
    C --> PS[Phase Shifter]
    PS --> RX1[RX1]
     C --> RX2[RX2]
Loading

Python-Code:

# Source: https://pysdr.org/content/bladerf.html#further-reading
# Note: This is a simplified example for demonstration purposes.

from bladerf import _bladerf
import numpy as np
import matplotlib.pyplot as plt



# Parameters
sample_rate = 40e6 #61.44e6
center_freq = 2.5e9
rx_gain = 5 # -15 to 60 dB
num_samples = int(1e6)
buffer_size = 8192 * 4 # number of bytes in our RX/TX buffer

sdr = _bladerf.BladeRF()
rx_ch1 = sdr.Channel(_bladerf.CHANNEL_RX(0)) # Zuweisung des RX1 Kanals
rx_ch2 = sdr.Channel(_bladerf.CHANNEL_RX(1)) # Zuweisung des RX2 Kanals
rx_ch1.frequency = center_freq
rx_ch1.sample_rate = sample_rate
rx_ch1.bandwidth = sample_rate/2
rx_ch2.frequency = center_freq
rx_ch2.sample_rate = sample_rate
rx_ch2.bandwidth = sample_rate/2
rx_ch1.gain_mode = _bladerf.GainMode.Manual
rx_ch1.gain = rx_gain
rx_ch2.gain_mode = _bladerf.GainMode.Manual
rx_ch2.gain = rx_gain

# Setup synchronous stream
sdr.sync_config(layout = _bladerf.ChannelLayout.RX_X2, # or RX_X2
                fmt = _bladerf.Format.SC16_Q11, # int16s
                num_buffers    = 16,
                buffer_size    = 8192,
                num_transfers  = 8,
                stream_timeout = 3500)

# Enable module
print("Starting receive")
rx_ch1.enable = True
rx_ch2.enable = True
# Create receive buffer
bytes_per_sample = 4 # don't change this, it will always use int16s
buf = bytearray(1024 * bytes_per_sample * 2) # *2 for two channels
x = np.zeros(num_samples, dtype=np.complex64) # storage for IQ samples
y = np.zeros(num_samples, dtype=np.complex64) # storage for IQ samples

# Receive loop
num_samples_read = 0
while True:
    if num_samples > 0 and num_samples_read == num_samples:
        break
    elif num_samples > 0:
        num = min(len(buf) // bytes_per_sample, 2 * (num_samples - num_samples_read))
    else:
        num = len(buf) // bytes_per_sample
    sdr.sync_rx(buf, num) # Read into buffer
    samples = np.frombuffer(buf, dtype=np.int16)
    xtmp = samples[0::4] + 1j * samples[1::4] # Convert to complex type
    ytmp = samples[2::4] + 1j * samples[3::4] # Convert to complex type
    xtmp /= 2048.0 # Scale to -1 to 1 (its using 12 bit ADC)
    ytmp /= 2048.0 # Scale to -1 to 1 (its using 12 bit ADC)
    num_cplx = num // 2

    #samples = samples[0::2] + 1j * samples[1::2] # Convert to complex type
    #samples /= 2048.0 # Scale to -1 to 1 (it is using a 12-bit ADC)
    x[num_samples_read:num_samples_read+num_cplx] = xtmp[0:num_cplx] # Store buf in samples array
    y[num_samples_read:num_samples_read+num_cplx] = ytmp[0:num_cplx] # Store buf in samples array
    num_samples_read += num_cplx
    #print("y", ytmp[0:10])

print("Stopping")
rx_ch1.enable = False
rx_ch2.enable = False

print("Max Magnitude CH1 RX_2:", np.max(np.abs(x)))
print("Max Magnitude CH2 RX_2:", np.max(np.abs(y)))
print("RMS CH1 RX_2:", np.sqrt(np.mean(np.abs(x)**2)))
print("RMS CH2 RX_2:", np.sqrt(np.mean(np.abs(y)**2)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions