Example #1
0
def receive_data_spectral(x):
    sr = spectral_recorder.SpectralRecorder(load=False)
    time.sleep(1)
    global socket_spectral_remote_network
    global all_nodes
    reading_time = 1000
    reading_time_sec = 1

    # init ZMQ socket
    poller = zmq.Poller()
    poller.register(socket_spectral_remote_network, zmq.POLLIN)
    # init figure
    plt.ioff()
    fig = plt.figure()
    fig.set_size_inches(1400 / 100.0, 580 / 100.0)
    #init canvas
    canvas1 = FigureCanvasTkAgg(fig, w.LabelframeWaterFall)
    canvas1.show()
    canvas1.get_tk_widget().configure(background='white', highlightcolor='white', highlightbackground='white')
    canvas1.get_tk_widget().grid(column=0, row=0, columnspan=1, sticky='nesw')
    canvas1.get_tk_widget().grid(column=0, row=0)

    while True:

        socks = dict(poller.poll(2000))
        if socket_spectral_remote_network in socks:
            #parsed_json = socket_spectral_remote_network.recv_json()
            spectral_raw = socket_spectral_remote_network.recv()
            f = open('/tmp/demo.tlv', 'wb')
            f.write(np.array(spectral_raw))
            f.close()

            try:

                [measurements, spectrum_features, duration_energy_det_features, duration_features, freq,power_features] = sr.get_spectrum_scan_features( "/tmp/demo.tlv", T=500e3)
                exp_name = "WaterFall"
                dd = np.array(list(map(itemgetter('duration'), duration_features)))  # duration detection via correlation
                bb = np.array(list(map(itemgetter('bw'), spectrum_features)))  # bandwidth
                ff2 = np.array(list(map(itemgetter('freq'), spectrum_features)))  # frequency
                ed = np.array(list(map(itemgetter('duration'), duration_energy_det_features)))  # duration via energy detection
                ed_t_start = np.array(list(map(itemgetter('tsf'), duration_energy_det_features)))  # duration via energy detection
                ed_t_stop  = np.array(list(map(itemgetter('tsf_stop'), duration_energy_det_features)))  # duration via energy detection

                p_av = np.array(
                    list(map(itemgetter('p_av'), power_features)))  # duration via energy detection
                p_av_w = np.array(
                    list(map(itemgetter('p_av_w'), power_features)))  # duration via energy detection
                tsf_p = np.array(
                    list(map(itemgetter('tsf_p'), power_features)))  # duration via energy detection

                fig.clf()
                #!!!! SUBPLOT 1
                # fig.suptitle(exp_name)
                ax_energy_detection = fig.add_subplot(322)
                # ax_energy_detection = fig.add_subplot(222)

                dd_s = np.sort(ed)
                yvals = np.arange(len(dd_s)) / float(len(dd_s) - 1)
                ax_energy_detection.plot(dd_s, yvals)
                # ax_energy_detection.set_xlim([-130,-90])
                ax_energy_detection.set_title('energy detection CDF size={}'.format(len(dd_s)));
                ax_energy_detection.set_xlabel('Duration [us]')
                ax_energy_detection.set_ylabel('CDF')
                ax_energy_detection.set_xlim([0,max(ed)])
                ax_energy_detection.grid()

                # !!!! SUBPLOT 2
                # ax_cdf_bw = fig.add_subplot(224)
                # ax_cdf_bw.plot(tsf_p,p_av)
                # ax_cdf_bw.plot(tsf_p, p_av_w,'--')
                # P_thr_db = -75
                # ax_cdf_bw.plot(tsf_p,[10 ** (P_thr_db / 10.0)] * len(tsf_p),'b--')
                # ax_cdf_bw.plot(ed_t_start, [10 ** (P_thr_db / 10.0)] * len(ed_t_start), 'g^')
                # ax_cdf_bw.plot(ed_t_stop, [10 ** (P_thr_db / 10.0)] * len(ed_t_stop), 'r^')
                # ax_cdf_bw.set_xlabel('Time [us]')
                # ax_cdf_bw.set_ylabel('POW')
                # ax_cdf_bw.grid()
                # ax_cdf_bw.set_ylim([0,4e-8])

                ax_cdf_bw = fig.add_subplot(324)

                bb = [j for i in bb for j in i]
                dd_s = np.sort(bb)
                yvals = np.arange(len(dd_s)) / float(len(dd_s) - 1)
                #max_bw,k_bw,v_bw = get_occurrences(bb)
                ax_cdf_bw.plot(dd_s, yvals)
                #ax_cdf_bw.plot(k_bw, np.cumsum(v_bw)/sum(v_bw),'b.')
                #ax_cdf_bw.stem(k_bw, v_bw/max(v_bw), 'b.--')
                # ax_cdf_bw.set_title('Expected bw CDF size={}'.format(len(dd_s)));
                ax_cdf_bw.set_title('Expected bw CDF');
                ax_cdf_bw.set_xlabel('BW [MHz]')
                ax_cdf_bw.set_ylabel('CDF')
                ax_cdf_bw.set_xlim([0, 20])
                ax_cdf_bw.grid()


                # !!!! SUBPLOT 3

                ax_cdf_ff = fig.add_subplot(326)
                ff2 = [j for i in ff2 for j in i]

                dd_s = np.sort(ff2)
                yvals = np.arange(len(dd_s)) / float(len(dd_s) - 1)
                ax_cdf_ff.plot(dd_s, yvals)
                ax_cdf_ff.set_title('Expected freq CDF');
                ax_cdf_ff.set_xlabel('freq [MHz]')
                ax_cdf_ff.set_ylabel('CDF')
                ax_cdf_ff.grid()
                f_spec=sr.get_freq_list(freq)
                fmin=min(f_spec)
                fmax=max(f_spec)
                ax_cdf_ff.set_xlim([fmin, fmax])

                # !!!! SUBPLOT 4
                # ax_cdf_duration = fig.add_subplot(428)
                # # IGNORE duration < 300
                # #dd = [i for i in dd if i >= 300]
                # dd_s = np.sort(dd)
                # yvals = np.arange(len(dd_s)) / float(len(dd_s) - 1)
                # try:
                #     max_corr=get_occurrences(dd)
                # except Exception as e:
                #     max_corr = -1
                # ax_cdf_duration.plot(dd_s, yvals)
                # ax_cdf_duration.set_title('correlation CDF size={}'.format(len(dd)));
                # ax_cdf_duration.set_xlabel('Duration [us]')
                # ax_cdf_duration.set_ylabel('CDF')
                # ax_cdf_duration.grid()

                ax_waterfall = fig.add_subplot(121)
                ax_waterfall = sr.plot_waterfall(ax_waterfall, measurements, exp_name)

                #print("----------------------")
                #print("AVERAGE FEATURES:")
                #print("max_bw   ={}".format(max_bw[0:5]))
                #print("max_freq ={}".format(max_freq[0:5]))
                #print("max_ed   ={}".format(max_ed[0:5]))
                #print("max_corr ={}".format(max_corr[0:5]))
                #print("max_bw   ={}".format(max_bw))
                #print("k_bw   ={}".format(k_bw))
                #print("v_bw   ={}".format(v_bw))
                #print("max_freq ={}".format(max_freq))
                #print("max_ed   ={}".format(max_ed[0:5]))
                #print("max_corr ={}".format(max_corr[0:5]))
                #print("----------------------")
                plt.tight_layout()
                # fig.savefig("{}.png".format("spcetral_plot"),dpi=(150))
                fig.canvas.draw()
                #plt.close()

            except Exception as e:
                #print(e)
                continue
Example #2
0
from spectral_acquire import spectral_recorder
import numpy as np
import time
from time import sleep
import math

from operator import itemgetter

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

sr = spectral_recorder.SpectralRecorder(load=False)
time.sleep(1)
#sr.acquire("/tmp/data.tlv")
#out=sr.extract_samples("nodezotacc1.tlv","out.json")
# #print len(out)
# power_fim,ts_fim		= sr.analyze("experiments/flow_middle/flow-in-the-middle-FG-CA-D_l-1505384368.826742224_nodezotacg6.tlv")
# power_no_int,ts_no_int		= sr.analyze("experiments/no_interference/no_interference-CA-D_l-1505384897.699383675_nodezotacg6.tlv")
# power_hidden,ts_hidden  	= sr.analyze("experiments/hidden/hidden-AC-DC-C_l-1505384677.681075940_nodezotacd6.tlv")
# power_idle,ts_idle		= sr.analyze("experiments/no_interference/idle_D_l-1505398712.776407626_nodezotacg6.tlv")

measurements = []

measurements = sr.extract_samples(
    filename=
    "experiments/flow_middle/flow-in-the-middle-FG-CA-D_l-1505384368.826742224_nodezotacg6.tlv",
    out_file="not-in-use.json")
# measurements = sr.extract_samples(filename="experiments/no_interference/no_interference-CA-D_l-1505384897.699383675_nodezotacg6.tlv",out_file="not-in-use.json")
# measurements = sr.extract_samples(filename="experiments/hidden/hidden-AC-DC-C_l-1505384677.681075940_nodezotacd6.tlv",out_file="not-in-use.json")
# measurements = sr.extract_samples(filename="experiments/no_interference/idle_D_l-1505398712.776407626_nodezotacg6.tlv",out_file="not-in-use.json")
Example #3
0
from operator import itemgetter
import os

port = "8502"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)


context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)

#ACQUIRE SPECTRAL SCAN 
filename="/tmp/demo.tlv"


while True:
	sr = spectral_recorder.SpectralRecorder(load=True, offline=False,freq=2437e6)
	sr.acquire(filename,T_acquire=0.4)
	time.sleep(1)
	print "samples acquired"
	msg_size=os.stat(filename).st_size
	print "file size={}".format(msg_size)
	target = open(filename, 'rb')
	spectral_raw = target.read(msg_size)
	target.close()

	socket.send(spectral_raw)
	time.sleep(2)