Showing posts with label Quick and Dirty. Show all posts
Showing posts with label Quick and Dirty. Show all posts

Wednesday, October 17, 2012

Quick & Dirty Part-4 : Running two classes one by one

In the previous three "Quick & Dirty" sections I made only a single class i.e.
rx_cfile_block1 and ran that.

Today I will make two classes with different parameters and then run them one by one from a single flow graph.

Here I wont explain much in detail as I did in the last three posts, but I would like you to refer them

http://sumitgnuradio.blogspot.in/2012/10/quick-dirty-part-3-getting-data-from.html
http://sumitgnuradio.blogspot.in/2012/10/quick-dirty-part-2-data-dumping-into.html
http://sumitgnuradio.blogspot.in/2012/10/quick-dirty-getting-data-from-usrp.html

Step-1 Copy the class definition section from the program of the post
http://sumitgnuradio.blogspot.in/2012/10/quick-dirty-getting-data-from-usrp.html
and paste it in a separate file

Step-2 In that separate file rename the class as rx_cfile_block2

Step-3 Change the class properties as you want i.e. usrp(type,serial number)center frequency, gain, sampling rate, number of samples, subdevices, antennas etc

Step-4 paste this class definition just before the line if __name__ == '__main__':   in the program

Step-5 Write "import time" just after from gnuradio import uhd

# Time is a python module which will enable us to use the time funcitons

Step-6 Refer to the lines after if __name__ == '__main__':

Step-7 After tb.run(), put time.sleep(5)

# This will make the program sleep for 5 seconds before executing next lines

Step-8 Write following lines after time.sleep(5)

tb = rx_cfile_block2()
tb.run()

# So at the begining the class rx_cfile_block1 will called after 5 seconds of it end the another class i.e. rx_cfile_block2 will be called.

# For cross checking if the program is working properly or not, put the file names different in the two classes or change their path


Program :

http://www.2shared.com/file/LxYFf2YG/pgm_4.html

Quick & Dirty Part-3 : Getting data from two USRP at a time

Here I will show how to get data dumped simultaneously into two different files :



Step -1 Make a file named "foo.py" with your favourite text editor

Step-2 Insert following two lines in the beginning.

#!/usr/bin/python2.6
#!/usr/bin/env python

Please change the version number of python as per your system
These two lines are necessary to make this foo.py file executable.

Step-3 Import necessary ingredients by insert the following lines

from gnuradio import gr
from gnuradio import uhd

We are importing gr to get our canvas i.e. top_block.py and importing uhd to access the usrp

 Step-4 Making a class by insert the following lines

class rx_cfile_block1(gr.top_block):

This makes a class named "rx_cfile_block1" which is a derived class of gr.top_block (don't worry about this .. just do it)

Step-5 Defining a function of the class by inserting the follwing line

def __init__(self):

Every python class has a member function named __init__ which is passed a parameter "self"(don't worry about this .. just do it)

Step-6 Constructor for the derived class by inserting the following line

gr.top_block.__init__(self)

(don't worry about this .. just do it)

Step-7 Instantiating a uhd source by inserting following line

self.uhd_usrp_source_1 = uhd.usrp_source(device_addr="serial=1R270DU1",\
stream_args=uhd.stream_args('fc32'))

self.uhd_usrp_source_2 = uhd.usrp_source(device_addr="serial=1R270DU2",\
stream_args=uhd.stream_args('fc32'))



# If you want to record complex data then leave the line above as it is. If you want to record short data, put sc16 instead of fc32

# Here you have to essentially put the serial number of the two devices 

Step-8 Instantiating two file sinks by inserting following line

self.gr_file_sink_1 = gr.file_sink(gr.sizeof_gr_complex,"/home/username/first_app_data1")
self.gr_file_sink_2 = gr.file_sink(gr.sizeof_gr_complex,"/home/username/first_app_data2")


** Replace username with your username

#  If you want to record complex data then leave the line above as it is. If you want to record short data, gr.sizeof_short*2 instead of gr.sizeof_gr_complex.

# For collecting short type data perform the above step after modification mentioned in step-7

Step-9 Specify the subdevice by inserting following line

self.uhd_usrp_source_1.set_subdev_spec("A:0", 0)
self.uhd_usrp_source_2.set_subdev_spec("A:0", 0)

       
# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B


# You can set different subdevices for each of them

Step-10 Specify antenna by inserting following line

self.uhd_usrp_source_1.set_antenna("RX2", 0)
self.uhd_usrp_source_2.set_antenna("RX2", 0)
       
# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B
# You can set different receiving antenna for each of them

Step-11 Set the sampling rate by inserting following line

self.uhd_usrp_source_1.set_samp_rate(1000000)
self.uhd_usrp_source_2.set_samp_rate(1000000)

#This sets the sampling rate to 1000000 i.e. 1MSPS
# You can set different sampling rates for each of them

 Step-12 Set the gain by inserting following lines

self.uhd_usrp_source_1.set_gain(45)
self.uhd_usrp_source_2.set_gain(45)

#This sets the gain to 45dB
# You can set different gains to each of them

Step-13 Set the center frequency by inserting following lines

treq = uhd.tune_request(2450000000)

self.uhd_usrp_source_1.set_center_freq(treq)
self.uhd_usrp_source_2.set_center_freq(treq)

#This sets the center frequency to 2450000000 i.e. 2.45GHz

# You can set different center frequency for each of them, in such case youneed to make two different "treq" parameters with different frequencies


i.e. treq1 = uhd.tune_request(frequency_1)
      treq2 = uhd.tune_request(frequency_2)
And then use these treq1 and treq2 to set the center frequencies

Step-14 Set the number of samples to be collected by inserting following line

self.head_1 = gr.head(gr.sizeof_gr_complex, int(1000000))
self.head_2 = gr.head(gr.sizeof_gr_complex, int(1000000))


#This sets the number of samples to be recorded equals 1000000
# You can set different sample number for each of them

Step-15 Connect everything by inserting following lines

self.connect(self.uhd_usrp_source_1,self.head_1,self.gr_file_sink_1)
self.connect(self.uhd_usrp_source_2,self.head_2,self.gr_file_sink_2)

# It connects the uhd source, the head and the sink

Step-16 Finishing touch by inserting following lines of code

if __name__ == '__main__':    
       tb = rx_cfile_block1()
       tb.run()

Now go to the terminal and type chmod +x foo.py

Now the python you just created has become executable so run it  by typing

./foo.py at the terminal

Now check the destination directory for the collected data.

Now you can plot the data by typing


gr_plot_psd_c /home/username/first_app_data1 # for PSD
gr_plot_psd_c /home/username/first_app_data2 # for PSD
gr_plot_fft_c /home/username/first_app_data1 # for FFT
gr_plot_fft_c /home/username/first_app_data2 # for FFT


Program :

http://www.2shared.com/file/DpZVT12A/pgm_3.html

                                                                                                                          
                                                                                                                                                                                             
                                                                                                                                                             
                    

Quick & Dirty part-2 : Data dumping into two different sinks simultaneously

Here I will show how to get data dumped simultaneously into two different files from a single usrp :



Step -1 Make a file named "foo.py" with your favourite text editor

Step-2 Insert following two lines in the beginning.

#!/usr/bin/python2.6
#!/usr/bin/env python

Please change the version number of python as per your system
These two lines are necessary to make this foo.py file executable.

Step-3 Import necessary ingredients by insert the following lines

from gnuradio import gr
from gnuradio import uhd

We are importing gr to get our canvas i.e. top_block.py and importing uhd to access the usrp

 Step-4 Making a class by insert the following lines

class rx_cfile_block1(gr.top_block):

This makes a class named "rx_cfile_block1" which is a derived class of gr.top_block (don't worry about this .. just do it)

Step-5 Defining a function of the class by inserting the follwing line

def __init__(self):

Every python class has a member function named __init__ which is passed a parameter "self" (don't worry about this .. just do it)

Step-6 Constructor for the derived class by inserting the following line

gr.top_block.__init__(self)

(don't worry about this .. just do it)

Step-7 Instantiating a uhd source by inserting following line

self.uhd_usrp_source = uhd.usrp_source(device_addr="serial=1R270DU1",\
stream_args=uhd.stream_args('fc32'))

# If you want to record complex data then leave the line above as it is. If you want to record short data, put sc16 instead of fc32

# Either replace the serial number by your usrp's serial number or just put type=usrp1 or type=usrp2 (according to availability) inside the quotes 

Step-8 Instantiating two file sinks by inserting following line

self.gr_file_sink_1 = gr.file_sink(gr.sizeof_gr_complex,"/home/username/first_app_data1")
self.gr_file_sink_2 = gr.file_sink(gr.sizeof_gr_complex,"/home/username/first_app_data2")


** Replace username with your username

#  If you want to record complex data then leave the line above as it is. If you want to record short data, gr.sizeof_short*2 instead of gr.sizeof_gr_complex.

# For collecting short type data perform the above step after modification mentioned in step-7   

Step-9 Specify the subdevice by inserting following line

self.uhd_usrp_source.set_subdev_spec("A:0", 0)
       
# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B

Step-10 Specify antenna by inserting following line

self.uhd_usrp_source.set_antenna("RX2", 0)
       
# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B

Step-11 Set the sampling rate by inserting following line

self.uhd_usrp_source.set_samp_rate(1000000)

#This sets the sampling rate to 1000000 i.e. 1MSPS

 Step-12 Set the gain by inserting following lines

self.uhd_usrp_source.set_gain(45)

 #This sets the gain to 45dB

Step-13 Set the center frequency by inserting following lines

treq = uhd.tune_request(2450000000)

self.uhd_usrp_source.set_center_freq(treq)

#This sets the center frequency to 2450000000 i.e. 2.45GHz

Step-14 Set the number of samples to be collected by inserting following line

self.head_1 = gr.head(gr.sizeof_gr_complex, int(1000000))
self.head_2 = gr.head(gr.sizeof_gr_complex, int(1000000))


#This sets the number of samples to be recorded equals 1000000

Step-15 Connect everything by inserting following lines

self.connect(self.uhd_usrp_source,self.head_1,self.gr_file_sink_1)
self.connect(self.uhd_usrp_source,self.head_2,self.gr_file_sink_2)

# It connects the uhd source, the head and the sink

Step-16 Finishing touch by inserting following lines of code

if __name__ == '__main__':    
       tb = rx_cfile_block1()
       tb.run()

Now go to the terminal and type chmod +x foo.py

Now the python you just created has become executable so run it  by typing

./foo.py at the terminal

Now check the destination directory for the collected data.

Now you can plot the data by typing


gr_plot_psd_c /home/username/first_app_data1 # for PSD
gr_plot_psd_c /home/username/first_app_data2 # for PSD
gr_plot_fft_c /home/username/first_app_data1 # for FFT
gr_plot_fft_c /home/username/first_app_data2 # for FFT

program :

http://www.2shared.com/file/Lrj95l69/pgm_2.html




Friday, October 12, 2012

Quick & Dirty: Part-1 : Getting data from USRP

So Lets start the quick and dirty guide on "How to get data from usrp"


Step -1 Make a file named "foo.py" with your favourite text editor

Step-2 Insert following two lines in the beginning.

#!/usr/bin/python2.6
#!/usr/bin/env python

Please change the version number of python as per your system
These two lines are necessary to make this foo.py file executable.

Step-3 Import necessary ingredients by insert the following lines

from gnuradio import gr
from gnuradio import uhd

We are importing gr to get our canvas i.e. top_block.py and importing uhd to access the usrp

 Step-4 Making a class by inserting the following lines

class rx_cfile_block1(gr.top_block):

This makes a class named "rx_cfile_block1" which is a derived class of gr.top_block (don't worry about this .. just do it)

Step-5 Defining a function of the class by inserting the follwing line

def __init__(self):

Every python class has a member function named __init__ which is passed a parameter  "self"(don't worry about this .. just do it)

Step-6 Constructor for the derived class by inserting the following line

gr.top_block.__init__(self)

(don't worry about this .. just do it)

Step-7 Instantiating a uhd source by inserting following line

self.uhd_usrp_source = uhd.usrp_source(device_addr="serial=1R270DU1",
stream_args=uhd.stream_args('fc32'))

# If you want to record complex data then leave the line above as it is. If you want to record short data, put sc16 instead of fc32

# Instead of serial number you can put type=usrp1 or type=usrp2 (according to availability), inside the quotes

Step-8 Instantiating a file sink by inserting following line

self.gr_file_sink = gr.file_sink(gr.sizeof_gr_complex,"/home/username/first_app_data1")

** Replace username with your username


#  If you want to record complex data then leave the line above as it is. If you want to record short data, gr.sizeof_short*2 instead of gr.sizeof_gr_complex.

# For collecting short type data perform the above step after modification mentioned in step-7  

Step-9 Specify the subdevice by inserting following line

self.uhd_usrp_source.set_subdev_spec("A:0", 0)

# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B

Step-10 Specify antenna by inserting following line

self.uhd_usrp_source.set_antenna("RX2", 0)

# For details on subdevices and antenna see my tutorial on subdevices here
http://www.youtube.com/watch?v=BERxSmWlRZM&feature=BFa&list=PLE8D7641BBF6E849B

Step-11 Set the sampling rate by inserting following line

self.uhd_usrp_source.set_samp_rate(1000000)

#This sets the sampling rate to 1000000 i.e. 1MSPS

 Step-12 Set the gain by inserting following lines

self.uhd_usrp_source.set_gain(45)

 #This sets the gain to 45dB

Step-13 Set the center frequency by inserting following lines

treq = uhd.tune_request(2450000000)

self.uhd_usrp_source.set_center_freq(treq)

#This sets the center frequency to 2450000000 i.e. 2.45GHz

Step-14 Set the number of samples to be collected by inserting following line

self._head = gr.head(gr.sizeof_gr_complex, int(1000000))

#This sets the number of samples to be recorded equals 1000000

Step-15 Connect everything by inserting following lines

 self.connect(self.uhd_usrp_source, self._head, self.gr_file_sink)

# It connects the uhd source, the head and the sink

Step-16 Finishing touch by inserting following lines of code

if __name__ == '__main__':
      tb = rx_cfile_block1()
      tb.run()

Now go to the terminal and type chmod +x foo.py

Now the python you just created has become executable so run it  by typing

./foo.py at the terminal

Now check the destination directory for the collected data.

Now you can plot the data by typing


gr_plot_psd_c /home/username/first_app_data1 # for PSD

gr_plot_fft_c /home/username/first_app_data1 # for FFT

Program :

http://www.2shared.com/file/hSrMaEKF/pgm_1.html