from gnuradio import gr, gru, usrp import usrp_dbid from gnuradio import audio from gnuradio import blks import os import time import struct fg=gr.flow_graph() ### Create the USRP ### u = usrp.source_c(decim_rate=32) # decimation is even values in range [4, 256]. # 4 can only be used with 4 bit sampling width = 8 shift = 8 format = u.make_format(width, shift) #r = u.set_format(format) # Uncomment to enable 8-bit sampling #print "set_format =", r # Uncomment to enable 8-bit sampling usrp_rate = u.adc_freq() / u.decim_rate() ### determine the daughterboard subdevice we're using ### rx_subdev_spec = usrp.pick_subdev(u, (usrp_dbid.TV_RX,)) u.set_mux(usrp.determine_rx_mux_value(u, rx_subdev_spec)) subdev = usrp.selected_subdev(u, rx_subdev_spec) ### Change the baseband lowpass filter on daughterboard to reduce bandwidth. ### Can be set anywhere from 4 to 33MHz ### #subdev.set_bw(1e6) #### set initial values # Set gain to max # Gain is 0 to 104, in steps of 1 g_range = subdev.gain_range() subdev.set_gain(0) # Set frequency to mid-point # Frequency is 500MHz to 2.6GHZ, in steps of 1MHz f_range = subdev.freq_range() freq = 100e6 # frequency to tune to r = u.tune(0, subdev, freq) # 0 is DDC channel #Complex to Float Conversion c2f=gr.complex_to_float() #Multiplier multiplier = gr.multiply_ff () multiplier1 = gr.multiply_ff () #Adder adder = gr.add_ff() #Vector sink sink = gr.file_sink(gr.sizeof_float, "temp_power.dat") #Wiring it all together fg.connect(u,c2f) fg.connect((c2f,0),(multiplier,0)) fg.connect((c2f,0),(multiplier,1)) fg.connect((c2f,1),(multiplier1,0)) fg.connect((c2f,1),(multiplier1,1)) fg.connect(multiplier,(adder,0)) fg.connect(multiplier1,(adder,1)) fg.connect(adder, sink) fg.start() #Set number of samples num_samples = 1000 repeat = 20 #repeat 20 times #Run graph until correct number of samples written to file while os.path.getsize("temp_power.dat") < num_samples * (repeat+1) * gr.sizeof_float: time.sleep(0.0001) #Stop the graph fg.stop() #Read in num_samples at a time, and display the average fin = open("temp_power.dat", 'rb') total_sum = 0 #Dump the first set of samples as they will be erroneous (bug in USRP) fin.read(num_samples * gr.sizeof_float) for i in range(repeat): # Read in first set of samples to a list data = struct.unpack(str(num_samples) + 'f' ,fin.read(num_samples * gr.sizeof_float)) #Now average out the data and print the result sum = 0 for val in data: sum += val print sum / num_samples * 1.0 total_sum += sum / num_samples * 1.0 #Now print average of all samples print "Overall Average: " , total_sum/repeat * 1.0