diff options
author | andreas128 <Andreas> | 2017-01-08 14:59:52 +0000 |
---|---|---|
committer | andreas128 <Andreas> | 2017-01-08 14:59:52 +0000 |
commit | 059eb97446a52b346a550d12f47478fc978b6001 (patch) | |
tree | 18590699a3291d3809de9420718a5872122fd0e9 /src | |
parent | 770048b409f7ba0636abc74cecc8efaeb863afb4 (diff) | |
download | ODR-StaticPrecorrection-059eb97446a52b346a550d12f47478fc978b6001.tar.gz ODR-StaticPrecorrection-059eb97446a52b346a550d12f47478fc978b6001.tar.bz2 ODR-StaticPrecorrection-059eb97446a52b346a550d12f47478fc978b6001.zip |
Add two_tone_lib and tested two_tone_tuner
Diffstat (limited to 'src')
-rw-r--r-- | src/tcp_async.py | 83 | ||||
-rw-r--r-- | src/tcp_sync.py | 108 | ||||
-rw-r--r-- | src/two_tone_lib.py | 75 |
3 files changed, 266 insertions, 0 deletions
diff --git a/src/tcp_async.py b/src/tcp_async.py new file mode 100644 index 0000000..05cadd7 --- /dev/null +++ b/src/tcp_async.py @@ -0,0 +1,83 @@ +"""Tcp client for asynchronous uhd message tcp port""" + +import threading +import Queue +import time +import socket + +class _TcpAsyncClient(threading.Thread): + """Thead for message polling""" + queue = Queue.Queue() + q_quit = Queue.Queue() + + ip_address = None + port = None + BUFFER_SIZE = 1 + + def __init__(self, ip_address, port): + super(_TcpAsyncClient, self).__init__() + self.ip_address = ip_address + self.port = port + + def __exit__(self): + self.stop() + + def run(self): + """connect and poll messages to queue""" + + #Establish connection + sock = None + print("Connecting to asynchronous uhd message tcp port " + str(self.port)) + while self.q_quit.empty(): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((self.ip_address, self.port)) + break + except socket.error: + print("connecting to asynchronous uhd message tcp port " + str(self.port)) + #traceback.print_exc() + sock.close() + time.sleep(0.5) + print("Connected to asynchronous uhd message tcp port " + str(self.port)) + + #Read messages + sock.settimeout(1) + while self.q_quit.empty(): + try: + data = sock.recv(self.BUFFER_SIZE) + self.queue.put(data) + except socket.timeout: + pass + + sock.close() + + def stop(self): + """stop thread""" + print("stop tcp_async uhd message tcp thread") + self.q_quit.put("end") + + +class UhdAsyncMsg(object): + """Creates a thread to connect to the asynchronous uhd messages tcp port""" + + def __init__(self, ip_address = "127.0.0.1", port = 47010): + self.tcpa = _TcpAsyncClient(ip_address, port) + self.tcpa.start() + + def __exit__(self): + self.tcpa.stop() + + def stop(self): + """stop tcp thread""" + self.tcpa.stop() + + def get_res(self): + """get received messages as string of integer""" + out = "" + while not self.tcpa.queue.empty(): + out += str(ord(self.tcpa.queue.get())) + return out + + def has_msg(self): + """Checks if one or more messages were received and empties the message queue""" + return self.get_res() != "" diff --git a/src/tcp_sync.py b/src/tcp_sync.py new file mode 100644 index 0000000..6a2e619 --- /dev/null +++ b/src/tcp_sync.py @@ -0,0 +1,108 @@ +"""Tcp client for synchronous uhd message tcp port""" + +import threading +import Queue +import time +import socket +import struct + +class _TcpSyncClient(threading.Thread): + """Thead for message polling""" + queue = Queue.Queue() + q_quit = Queue.Queue() + + ip_address = None + port = None + + def __init__(self, ip_address, port, packet_size, packet_type): + super(_TcpSyncClient, self).__init__() + self.ip_address = ip_address + self.port = port + self.packet_size = packet_size + self.packet_type = packet_type + + def __exit__(self): + self.stop() + + def run(self): + """connect and poll messages to queue""" + + #Establish connection + sock = None + print("Connecting to synchronous uhd message tcp port " + str(self.port)) + while self.q_quit.empty(): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((self.ip_address, self.port)) + break + except socket.error: + print("connecting to synchronous uhd message tcp port " + str(self.port)) + #traceback.print_exc() + sock.close() + time.sleep(0.5) + print("Connected to synchronous uhd message tcp port " + str(self.port)) + + #Read messages + sock.settimeout(None) + while self.q_quit.empty(): + try: + s = "" + + #concatenate to one package + while self.q_quit.empty(): + s += sock.recv(self.packet_size) + if (len(s)) == self.packet_size: + break + if (len(s)) > self.packet_size: + print("received wrong size of length " + str(len(s))) + time.sleep(0.01) + return -1 + + res_tuple = struct.unpack( + self.packet_type, + s) + self.queue.put(res_tuple) + except socket.timeout: + self.stop() + traceback.print_exc() + pass + + sock.close() + + def stop(self): + """stop thread""" + print("stop tcp_sync uhd message tcp thread") + self.q_quit.put("end") + + +class UhdSyncMsg(object): + """Creates a thread to connect to the synchronous uhd messages tcp port""" + + def __init__(self, ip_address = "127.0.0.1", port = 47009, packet_size = 3, packet_type = "fff"): + self.tcpa = _TcpSyncClient(ip_address, port, packet_size, packet_type) + self.tcpa.start() + + def __exit__(self): + self.tcpa.stop() + + def stop(self): + """stop tcp thread""" + self.tcpa.stop() + + def get_msgs(self, num): + """get received messages as string of integer""" + out = [] + while len(out) < num: + out.append(self.tcpa.queue.get()) + return out + + def get_res(self): + """get received messages as string of integer""" + out = [] + while not self.tcpa.queue.empty(): + out.append(self.tcpa.queue.get()) + return out + + def has_msg(self): + """Checks if one or more messages were received and empties the message queue""" + return self.get_res() != "" diff --git a/src/two_tone_lib.py b/src/two_tone_lib.py new file mode 100644 index 0000000..df7d53f --- /dev/null +++ b/src/two_tone_lib.py @@ -0,0 +1,75 @@ +import numpy as np +import matplotlib.pyplot as plt + +def gen_two_tone(path = "./input.dat", predist = None, par = None, debug = False): + period1 = 3.875 + period2 = 4 + t_both = 124 + assert(t_both / period1 % 1 == 0) + assert(t_both / period2 % 1 == 0) + + t = np.arange(0,t_both) + sin1 = np.sin(t * 2 * np.pi * 1./period1) + sin2 = np.sin(t * 2 * np.pi * 1./period2) + sig = sin1 + sin2 + + if predist is None: + res = sig + else: + res = predist(sig, par) + + res = res / np.max(res) + + res = res.astype(np.complex64) + res.tofile(path) + + a_load = np.fromfile(path, dtype=np.complex64) + assert(np.isclose(a_load, res).all()), "Inconsistent stored file" + + if debug == True: + plt.plot(np.abs(np.concatenate((a_load, a_load)))) + plt.savefig(path + ".png") + plt.clf() + + plt.plot(np.abs(np.fft.fftshift(np.fft.fft(np.concatenate((a_load, a_load))))), 'ro') + plt.savefig(path + "_fft.png") + plt.clf() + + return path + +def predist_poly(sig, coefs = []): + res = sig + for idx, coef in enumerate(coefs): + res += sig * np.abs(sig)**(idx+1) * coef #+1 because first correction term is squared + return res + +def analyse_power_spec(spec, debug = False, debug_path="", suffix=""): + peak_1 = None + peak_2 = None + spec_start = 4096 + spec_end = 8192 + first_peak = spec_start + 2048 + second_peak = spec_start + 2114 + delta_freq = 66 + peak_other = [] + if debug: plt.plot(spec[spec_start:spec_end]) + for x in [c * delta_freq + delta_freq//2 for c in range(spec_start//delta_freq)]: + start = spec_start + x + end = spec_start + x + delta_freq + peak = spec[start:end].max() + if debug: plt.plot((start-spec_start,end-spec_start), (peak, peak)) + if start < first_peak and end > first_peak: + peak_1 = peak + if debug: plt.plot((start-spec_start,end-spec_start), (peak+1, peak+1)) + elif start < second_peak and end > second_peak: + peak_2 = peak + if debug: plt.plot((start-spec_start,end-spec_start), (peak+1, peak+1)) + else: + peak_other.append(peak) + mean_signal = (peak_1 + peak_2) / 2 + mean_others = np.mean(peak_other) + score = mean_signal - mean_others + if debug: + plt.savefig(debug_path + "/" + str(score) + suffix + ".png") + plt.clf() + return score |