Showing posts with label MATLAB GNU Radio. Show all posts
Showing posts with label MATLAB GNU Radio. Show all posts

Saturday, October 20, 2012

Generating Echo

Step-1 Taking a recorded data of wcdma signal with following specs

sample rate - 12.5MSPS
center frequency - 2.15149GHz
power level - (-65dB)
number of complex samples - 1e7

File is located in /home/sumit/wcdma_data_dump

Step-2 Reading the data in matlab with the following process

cd /home/sumit/wcdma_data_dump/ % go to current directory

fid = fopen('/home/sumit/wcdma_data_dump/wcdma_65','rb'); % open file descriptor

wcdma_matlab_65 = fread(fid,20000000,'float32'); % read the binary file as 32 bit complex float
% i.e. 32 bits for I and 32 bits for Q

fclose(fid); % close the file descriptor essentially
save('wcdma_matlab_65.mat','wcdma_matlab_65'); % save the current data in .mat format
% now we will ignore first 400000 samples because there was a peak there
% because of the LO leakage



wcdma_noleak_65 = wcdma_matlab_65(800000:20000000);
% I have taken 800000 because the samples are here in the format of I and Q
% cascaded


save('wcdma_noleak_65.mat','wcdma_noleak_65');
% creating the data with no LO leakage


temp1 = wcdma_noleak_65; 

cd /home/sumit/wcdma_data_dump/


fid = fopen('temp1.bin','wb');


fwrite(fid,temp1,'float32'); % created binary file for the no LO leakage data


fclose(fid);


Step-3 Echo generation

Xecho(t-delay) = X(t-0) + X(t-delay)

I made following flow graph
Step-4 Echo Visualization 

Echo affected signal looks like this





While the original signal looked like this




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.

I1
Q1
I2
Q2
I3
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.

I1
Q1
I2
Q2
I3
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');

>>fclose(fid)

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




 

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