Sunday, October 14, 2012

.bin to .mat and vice versa for GNU Radio and MATLAB

In my previous post under the label MATLAB GNURadio I told about How to move data between GNU Radio and MATLAB using Octave.

Today I will tell the same without using Octave

.bin to .mat

Say you have collected complex data using uhd_rx_cfile and saved it in a file say my_data and number of samples you collected is 'N'

Now open MATLAB :

1. Change the directory to the directory where your collected data my_data is there

2.  Type and enter the following on the MATLAB prompt :

>> fid = fopen('my_data','rb')

>> my_data_matlab  = fread(fid,N,'float32')

>> fclose(fid)

By doing the above all the binary data will e converted to symbol and saved in my_data_matlab named variable in its columns. Please wait still something has to be done.

If you see the data content of my_data_matlab , you will find that there are only float values, no complex data !!

Actually , in the columns of my_data_matlab the format of data is something like I and Q parts are alternatively cascaded i.e.

Q3 and so on.

Hence to get the complex values data you just need to type and enter the following

>> my_data_complex = my_data_matlab(1:2:N-1) + j*my_data_matlab(2:2:N);

By doing this your complex data will be saved in my_data_complex variable in the I + J*Q format.

You can also notice that the size(my_data_matlab) = 2* size(my_data_complex)
which is quite obvious

.mat to .bin

 Say the variable in the .mat file is "mat"

To convert it into a bin file first you need to give it the cascaded I and Q form i.e.

Q3 and so on.

For this do the following with your data i.e. "mat"

>> mat_casc(1:2:N-1) = real(mat);

>> mat_casc(2:2:N) = imag(mat);

Doing the above will give you the cascaded I and Q format

Now do the following on the MATLAB prompt :

>> fid = fopen('mat.bin','wb');

>> fwrite(fid,mat_casc,'float32');


By doing the above your all data will get saved in a .bin file named mat.bin


No comments:

Post a Comment