コード例 #1
0
 def getStandardDeviation(coordinator_id ,tx_node_id, rx_node_id, year=None, month=None, day=None):    
     #use this method when you want to calculate the standard deviation from the saved gain measurements
     #read from file (if it exists)
     
     #first get data from the file
     gains = GainCalculations.getFileResults(coordinator_id, tx_node_id, rx_node_id, year=year, month=month, day=day)
     #gains will have the following form : [ [gain - linear , received_power[w] , noise_power[w], transmitted_power[w], date ], [gain - linear , received_power[w] , noise_power[w], transmitted_power[w], date ], .. ]
     
     if gains is None:
         aux = tx_node_id
         tx_node_id = rx_node_id
         rx_node_id = aux
         print "There are no values available for this combination. Trying with gain_between_tx_%d_and_rx_%d.dat? (yes or no)" %(tx_node_id, rx_node_id)
         choice = raw_input("")
         
         if choice.lower() == "no":
             print "You have chosen no"
             return None
         elif choice.lower() == "yes":
             gains = GainCalculations.getFileResults(coordinator_id, tx_node_id, rx_node_id)
             if gains is None:
                 print "Sorry, there are no measurements for this combination at all"
                 return None
         else:
             print "Invalid input, you were supposed to enter yes or no"
             return None
        
     #we are interested only in gains, not other data
     gain_measurements = []
     for i in gains:
         gain_measurements.append(i[0])
     
     return kalmanImplementation.getStandardDeviation(gain_measurements)
コード例 #2
0
ファイル: plot.py プロジェクト: adobekan/logatec-games
    def plotGains(date_list, y_list, xlabel, ylabel, title, ion=False):
        #y_list in dB. date_list is a list of dates at which the measurements were made
        if ion:
            plot.ion()
        else:
            plot.ioff()
            
        plot.clf()
        plot.grid()
        
        plot.title(title)
        plot.xlabel(xlabel)
        plot.ylabel(ylabel)
        
        #determine minimum and maximum values of y list. Do this now because I will need it several times. 
        min_y_list = min(y_list)
        max_y_list = max(y_list)
        
        #change rc param
        plot.rcParams.update({'font.size': 22})
        
        #set yticks
        distance = math.fabs(math.fabs(max_y_list)-math.fabs(min_y_list))/16
        #round the distance to the nearest integer
        distance = round(distance) - .5
        distance = int(distance) + (distance>0)
        if distance <1:
            distance = 1
        
        plot.yticks(numpy.arange(min_y_list, max_y_list, distance))
        
        #plot values
        plot.plot(y_list , '-',color = "black", label = "Measured gain", linewidth = 1.3)
        plot.axis([0, len(y_list), min_y_list - 2, max_y_list +2])
        
        #plot average line
        k =0.00
        sum = 0.00
        for i in y_list:
            #do average in linear scale
            sum+= math.pow(10.00, float(i)/10.00)
            k+=1
        linear_average_gain = sum/k
        plot.axhline(10.00 * math.log10(linear_average_gain), color = 'r', linewidth = 2.2, label = "Average gain %.3f dB" %(10.00*math.log10(linear_average_gain)))
        
        #We want to shade the night measurements with a color, and the day measurements with other color
        #make 2 lists, which represents the intervals for day and night. day intervals : [ [a,b, day], [c,d, day] ,[e,f, day] ]
        day_intervals = []
        night_intervals = []
        
        #determine the night and day intervals
        for i in range(0, len(date_list)-1):
            #for day. We want to determine index interval y_list for which date_list has a day hours
            if (date_list[i].hour ==12 or date_list[i].hour == 13) and i==0 :
                first_index = i
            elif (date_list[i].hour == 12 or date_list[i].hour == 13) and (date_list[i-1].hour != 12 and date_list[i-1].hour != 13):
                first_index = i
                
            if (date_list[i].hour == 12 or date_list[i].hour == 13) and (date_list[i+1].hour != 12 and date_list[i+1].hour != 13):
                last_index = i
                day_intervals.append([first_index, last_index, date_list[i].day])
                
            if (date_list[i].hour == 12 or date_list[i].hour == 13) and (date_list[i+1].hour == 12 or date_list[i+1].hour == 13) and (i+1) == len(date_list)-1:
                last_index =i+1
                day_intervals.append([first_index, last_index, date_list[i].day])
            
            #same thing for night
            if (date_list[i].hour ==21 or date_list[i].hour == 22) and i==0 :
                first_index = i
            elif (date_list[i].hour == 21 or date_list[i].hour == 22) and (date_list[i-1].hour != 21 and date_list[i-1].hour != 22):
                first_index = i

            if (date_list[i].hour == 21 or date_list[i].hour == 22) and (date_list[i+1].hour != 21 and date_list[i+1].hour != 22):
                last_index = i
                night_intervals.append([first_index, last_index, date_list[i].day])
                
            if (date_list[i].hour == 21 or date_list[i].hour == 22) and (date_list[i+1].hour == 21 or date_list[i+1].hour == 22) and (i+1) == (len(date_list)-1):
                last_index =i+1
                night_intervals.append([first_index, last_index, date_list[i].day])
        
        #plot vertical span 
        for i in range(0, len(day_intervals)):
            plot.axvspan(day_intervals[i][0], day_intervals[i][1], color = "white", alpha = 0.1)
            
        for i in range(0, len(night_intervals)):
            plot.axvspan(night_intervals[i][0], night_intervals[i][1], color = "gray", alpha = 0.2)

        #plot arrows from maximum to minimum value of gains
        arrow_length = math.fabs(math.fabs(max_y_list) - math.fabs(min_y_list))
        plot.arrow(len(y_list)/2, min_y_list, 0, arrow_length, head_width = 0.02 * len(y_list), length_includes_head = True, color = "blue",head_length = arrow_length*0.05, linewidth = 2)
        plot.arrow(len(y_list)/2, max_y_list, 0, -arrow_length, head_width = 0.02 * len(y_list), length_includes_head = True, color = "blue", head_length = arrow_length*0.05, linewidth = 2)
        plot.text(len(y_list)/2 +0.5, min_y_list + 0.1*arrow_length, "%.3f dB" %arrow_length, fontsize = 22, weight = 600)
        
        #plot maximum and minimum lines
        plot.axhline(max_y_list, color = 'black', linestyle = "--")
        plot.text(0.4 * len(y_list), max_y_list +0.5 , "%.3f dB" % max_y_list, color = "red", weight = 600, fontsize = 22)
        plot.axhline(min_y_list, color = 'black', linestyle = '--')
        plot.text(0.4 * len(y_list), min_y_list - 1, "%.3f dB" % min_y_list, color = "red", weight = 600, fontsize = 22)
        
        #I want to plot the predicted gain value using the KalmanFilter. For this I need linear values
        y_list_linear = []
        for i in range(0, len(y_list)):
            #dB -> linear
            y_list_linear.append(math.pow(10.00, float(y_list[i])/10.00))
         
    
        #plot predicted gain using  Kalman filter
        y_list_predictedVer2 = kalmanImplementation.getPredictedValuesVer2(y_list_linear)
        for i in range(0, len(y_list_predictedVer2)):
            y_list_predictedVer2[i] = 10.00 * math.log10(y_list_predictedVer2[i])
        
        #now plot y_list_predictedVer2
        plot.plot(y_list_predictedVer2, '-', color = "yellow", label = "Predicted gain", linewidth = 2.2)
        
        #plot deviations? standard deviation or % from maximum deviation?
        #get standard deviation
        standard_deviation = kalmanImplementation.getStandardDeviation(y_list_linear)    
        plot.axhspan(10.00*math.log10(linear_average_gain-standard_deviation),10.00*math.log10(linear_average_gain+standard_deviation), facecolor='black', alpha=0.20)  
        
        #plot arrow
        arrow_length = 10.00*math.log10(linear_average_gain+standard_deviation) - 10.00*math.log10(linear_average_gain-standard_deviation)
        plot.arrow(len(y_list)/2 - 0.3*len(y_list)/2, 10.00*math.log10(linear_average_gain-standard_deviation), 0, arrow_length, head_width =  0.6*arrow_length, length_includes_head = True, color = "green",head_length = arrow_length*0.09, linewidth = 3)
        plot.arrow(len(y_list)/2 - 0.3*len(y_list)/2, 10.00*math.log10(linear_average_gain+standard_deviation), 0, -arrow_length, head_width =  0.6*arrow_length, length_includes_head = True, color = "green",head_length = arrow_length*0.09, linewidth = 3)
        plot.text(len(y_list)/2 - 0.3*len(y_list)/2, 10.00*math.log10(linear_average_gain+standard_deviation)+0.5, "%.3f dB" %arrow_length, color = "green", weight=600, fontsize = 22)
        
        #plot a text with number of measurements
        plot.text(2, min(y_list)-1, "Number of\nmeasurements:\n%d" %len(y_list), color = "black", weight = 700, fontsize = 22)
        
        
        #determining the mse for average
        sum_tmp = 0
        for i in y_list_linear:
            sum_tmp+=math.pow((linear_average_gain-i), 2)
        mse_average = sum_tmp/len(y_list_linear)
         
        #determining the mse for kalman
        y_list_predictedVer2 = kalmanImplementation.getPredictedValuesVer2(y_list_linear)
        sum_tmp = 0
        for i in range(0, len(y_list_linear)):
            sum_tmp+=math.pow(y_list_predictedVer2[i]-y_list_linear[i], 2)
        mse_kalman = sum_tmp/len(y_list_linear)
        
        #plot.plot([],label = "Avg mse: %f" %(mse_average))
        #plot.plot([],label= "Kalman mse: %f" %(mse_kalman))
        print mse_average
        print mse_kalman
        
        
        leg = plot.legend(loc = 'upper right', bbox_to_anchor=(1.005, 1.01), fontsize = 22)
        leg.get_frame().set_alpha(0.7)
        
        #maximize the window
        mng = plot.get_current_fig_manager()
        mng.resize(*mng.window.maxsize())
         
        fig = plot.gcf()
        fig.set_size_inches( (19, 11) )
        try:
            os.mkdir("./gain plots")
        except:
            pass
        plot.savefig("./gain plots/%s.png" %(title), dpi=250)
        #return
        if ion:
            plot.draw()
        else:
            plot.show()