def getInstantNoise(coordinator_id, node_id ,freq=2420e6): #return current noise measured at the specified node #define a Node object rx_node = Node(coordinator_id, node_id) #for how much time we will measure the spectrum sensing_duration = 2 try: #configure node rx_node.setSenseConfiguration(freq, freq, 400e3) #start sensing rx_node.senseStart(time.time()+1, sensing_duration, 5) except Exception: print "Error at sensing start" return #now we have the measured results in a file, we have to read the file and do the average #data returned has the following structure: [[frequency], [average power in Watts for that frequency]]. Normally there is only one frequency noise_power = GainCalculations.getAverageDataMeasurementsFromFile(coordinator_id ,rx_node.node_id)[1][0] print "Noise power %.6fE-12" %(noise_power * 1e12) return noise_power #import noise #powerNoise = getInstanceNoise(9451, 56) #getInstantNoise()
def calculateInstantGain(coordinator_id,tx_node_id, rx_node_id, measuring_freq = 2420e6, saveresults = True , transmitting_duration = 10): #this method measures the instant channel gain between tx_node_id and rx_node_id within the coordinator_id #calculate gain as the following: Gain_between_i_and_j = (Power received by j - Power noise)/(Power transmitted by i) #you can specify the frequency at which you want to measure the gain #you can save the results ( append ) in a file or you can just get instant gain without saving the results #you can specify transmitting_duration of the generated signal. Taking into account the programming times I recommend using at least 6 seconds #define two Node objects tx_node = Node(coordinator_id, tx_node_id) rx_node = Node(coordinator_id, rx_node_id) #a few measurement parameters #transmitting power [dBm] p_tx_dBm = 0 #transmitting duration [s]. For how long will the generator generate a signal. This parameter is given in the method parameters #sensing duration [s] . For how much time will the node sense the spectrum. sensing_duration = 2 #specify when to start the sensing (after 1-2 seconds). Note: the packet which I send to the node, includes a time (when to transmit), but, there takes some time until packet arrives at node. time_after = 1.5 #First we want to measure the noise power, for that it is really important that no signal is generated attempts = 0 while True: try: #configure rx_node rx_node.setSenseConfiguration(measuring_freq, measuring_freq, 400e3) #start sensing rx_node.senseStart(time.time()+time_after, sensing_duration, 5) #Observation: Computer clock is not synchronized with the node clock. This is a reason why we choose to start the sensing only after a few seconds, otherwise the are cases when node report that "Start time cannot be in the past" #if everything is fine, break this loop break except: if attempts<3: attempts+=1 print "Calculate gain between %d and %d: Noise measurement error, retry %d" %(tx_node_id, rx_node_id, attempts) continue else: print "Calculate gain between %d and %d: Noise measurement error, return nothing" %(tx_node_id, rx_node_id) return #now we have the measured results in a file, we have to read the file and do the average. #data returned has the following structure: [[frequency], [average power in W for that frequency]]. Normally there is only one frequency because we've set the nodes to sense and generate signal only on a single channel noise_power = GainCalculations.getAverageDataMeasurementsFromFile(coordinator_id ,rx_node.node_id)[1][0] #now we have to generate a signal and measure the received power #configure and start the signal generation try: tx_node.setGeneratorConfiguration(measuring_freq, p_tx_dBm) tx_node.generatorStart(time.time(), transmitting_duration) except: print "Calculate gain between %d and %d : Error at generator. Return" %(tx_node_id, rx_node_id) return #get current time. I want to know when I started the signal generation start_gen_time = time.time() #wait a second just to be sure that receiver senses the signal generated. (With this we take into account other delays that can appear in testbed) time.sleep(0.5) #sense the spectrum attempts =0 while True: try: #start sense after time_after seconds from now rx_node.senseStart(time.time()+time_after, sensing_duration, 5) #if everything is fine, break this loop break except Exception: #try a few more times if something went wrong if attempts < 2: attempts += 1 print "Calculate gain between %d and %d : Receive power measurement error, retry %d" %(tx_node_id, rx_node_id, attempts) continue else: print "Calculate gain between %d and %d : Receive power measurement error, return nothing" %(tx_node_id, rx_node_id) #anyway, at this point there is a signal generated, so I don't want to affect other measurements, so we have to wait until signal generation stops GainCalculations.sleepUntilSignalGenerationStops(start_gen_time, transmitting_duration) return #now we have in file data with the measurements. Average data from the file: [[frequencies], [average power in W for a specific frequency]] received_power = GainCalculations.getAverageDataMeasurementsFromFile(coordinator_id,rx_node.node_id)[1][0] print "Gain calculation between tx_%d and rx_%d : Noise power: %.6fE-12 Received power: %.6fE-12" %(tx_node_id, rx_node_id,noise_power*1e12, received_power*1e12) #calculate gain gain = (received_power - noise_power) / (math.pow(10.00, p_tx_dBm/10.00) * 0.001) if gain<0: #in this case, something wrong happened, no signal was generated print "Calculate gain between %d and %d : Bad measurement, noise power is bigger than received_power, omit this measurement" %(tx_node_id, rx_node_id) #wait for the signal generation stops GainCalculations.sleepUntilSignalGenerationStops(start_gen_time, transmitting_duration) return None print "Gain between node %d and node %d: %.9fE-6 ( %.6f dB)" %(tx_node.node_id, rx_node.node_id, gain *1e6, 10.00*math.log10(gain)) #write this gain in a file if this is what we want results_list = [gain, received_power, noise_power, math.pow(10, p_tx_dBm/10.00) * 0.001, datetime.datetime.now()] #save results if(saveresults): GainCalculations.printResultsInAFile(results_list, coordinator_id ,tx_node.node_id, rx_node.node_id) #wait until signal generation stops GainCalculations.sleepUntilSignalGenerationStops(start_gen_time, transmitting_duration) #return gain return gain