Saturday, October 13, 2012

MATLAB & GNU Radio

How to get the data collected from GNU Radio to MATLAB and vice versa

From GNU Radio to MATLAB :

1. Suppose you collected data using uhd_rx_cfile and the wire format is fc32 i.e. complex float (The file format of this data is .bin i.e binary) In complex float format the data is encoded with 64 bits, 32 bits for I and 32 bits for Q.

To import these data to MATLAB we need to save this data in .mat format

One of the good tools for this is GNU Octave

Get Octave installed using Synaptic. Now GNU radio provides several utilities to read these .bin files. These utilities are actually octave scripts and can be located in /gnuradio/gnuradio-core/src/utils

To open Octave type "octave" on the terminal

After that you need to tell the location of these scripts to the Octave, typing the following line on the octave prompt will do the work

addpath("/home/username/gnuradio/gnuradio-core/src/utils/")

say the name of the data file you collected using uhd_rx_cfile is data

type the following in octave now

data_oct = read_complex_binary("/home/username/data")

** When Octave is done with reading a ":" will appear at the bottom of the terminal, you need to press 'q' and the Ocatave prompt will return back (it will throw some 'broken pipe data lost' warning ... don't care about that )

The above command will read the bin file data (assuming that it is saved in /home/username ) and will save it in data_oct which is a user defined variable and shows you the data in I + j*Q format.

Now to save this data in .mat format type the following in the octave prompt

save("-v7","data_oct.mat","data_oct")

** v7 is the format of MATLAB's .mat file of common use

This will save the required data to data_oct.mat file

Now you can import this data in MATLAB :)

** Note that when you load the data_oct.mat in MATLAB the name of the variable through which you can access it is  'data_oct'

From MATLAB to GNU Radio :

Say you did some weird signal processing on data_oct.mat in MATLAB and now you want that processed data back to a .bin file for further processing in GNU Radio, here is what you need to do

say you want to save in a .bin file name data_bin.bin

type and enter following to the MATLAB prompt

fid = fopen('//data_bin.bin', 'w')

Now a bin named data_bin.bin file shall appear in your current directory

Say name of the file contained processed data is data_proc

now type the following

fwrite(fid, data_proc ,'float32')

Now your data is saved in data_bin :)

dont forget to type  fclose(fid) on MATLAB after you are done


 







1 comment:

  1. Hello Sumit,

    Thank you for sharing your experience.

    After step 1, that is setting path worked fine. But when i am trying to pass the second command that is data_oct=.... it is throwing error which is mentioned below.
    'read_complex_binary' undefined.

    Please let me know how can i resolve this issue.

    Thank you.

    ReplyDelete