Saving Signal to npy File

Having issues with the site, hardware, source code, or any other issues?
Post Reply
madrigal
Posts: 1
Joined: Thu Jun 15, 2023 11:07 am

Saving Signal to npy File

Post by madrigal »

I'm trying to receive a signal and save it into a npy file using the bladeRF in python. I've been using host/examples/python/txrx/txrx.py from the bladeRF github, and I've tried many different ways to modify the below code (from txrx.py) to save the data as npy, but I'm unable to convert from bytearray to np array, and when I converted into a python list, the values were all real numbers from 0-255.

Code: Select all

    # Create receive buffer
    bytes_per_sample = 4
    buf = bytearray(1024*bytes_per_sample)
    num_samples_read = 0

    # Tell TX thread to begin
    if( tx_start != None ):
        tx_start.set()

    # Save the samples
    with open(rxfile, 'wb') as outfile:
        while True:
            if num_samples > 0 and num_samples_read == num_samples:
                break
            elif num_samples > 0:
                num = min(len(buf)//bytes_per_sample,
                          num_samples-num_samples_read)
            else:
                num = len(buf)//bytes_per_sample

            # Read into buffer
            device.sync_rx(buf, num)
            num_samples_read += num

            # Write to file
            outfile.write(buf[:num*bytes_per_sample])
I also tried using np.fromfile to convert the bin file created by txrx.py into a npy file, but I get NaN values when I use that method.

So, how can I receive a signal from the bladeRF and write it to a npy file in python? Additionally, how do I save the metadata into the npy file? I know I'll need to use bladerf format SC16_Q11_META in device.sync_config, but again, I don't know how to properly save it into the npy file.

Thanks!
lezard
Posts: 4
Joined: Wed Sep 27, 2023 10:02 am

Re: Saving Signal to npy File

Post by lezard »

Here is a way to convert from the bytearray of SC16_Q11 to numpy array:

Code: Select all

to_int16 = np.frombuffer(buf, dtype=np.int16)
divided_values = to_int16 / 2048.0
complex_values =  divided_values.view(dtype=np.complex64)
Post Reply