From 75edb689e503769dcb1ab228e4a59ea2e0cf8d9e Mon Sep 17 00:00:00 2001 From: sara Date: Tue, 14 Dec 2021 18:57:29 +0100 Subject: BER block angepasst --- src/gr-fadingui/grc/fadingui_ber.block.yml | 4 ++-- src/gr-fadingui/python/ber.py | 34 ++++++++++++++++++++++++------ src/gr-fadingui/python/logger.py | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/gr-fadingui/grc/fadingui_ber.block.yml b/src/gr-fadingui/grc/fadingui_ber.block.yml index 477f9ff..b0ec75c 100644 --- a/src/gr-fadingui/grc/fadingui_ber.block.yml +++ b/src/gr-fadingui/grc/fadingui_ber.block.yml @@ -5,7 +5,7 @@ category: '[fadingui]' templates: imports: import fadingui make: fadingui.ber(vgl=${vgl}, vlen=${vlen},address=${address}) - + # Make one 'parameters' list entry for every parameter you want settable from the GUI. # Keys include: # * id (makes the value accessible as \$keyname, e.g. in the make entry) @@ -21,7 +21,7 @@ parameters: - id: address label: Address dtype: string - default: "udp://localhost:31415" + default: "udp://localhost:31420" # Make one 'inputs' list entry per input and one 'outputs' list entry per output. diff --git a/src/gr-fadingui/python/ber.py b/src/gr-fadingui/python/ber.py index 205dcd4..1c5debf 100644 --- a/src/gr-fadingui/python/ber.py +++ b/src/gr-fadingui/python/ber.py @@ -23,6 +23,7 @@ import socket from urllib.parse import urlparse import numpy as np +from numpy_ringbuffer import RingBuffer from gnuradio import gr from fadingui.logger import get_logger @@ -42,6 +43,9 @@ class ber(gr.sync_block): self.vgl=vgl self.vlen=vlen + self.ber_samples = RingBuffer(capacity=2000, dtype=int) + self.ber_samples.extend(np.zeros(self.ber_samples.maxlen)) + # Create a socket and parse remote machine url self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.url = urlparse(address) @@ -65,14 +69,23 @@ class ber(gr.sync_block): # FIXME: this could be (very) slow, is there a faster way with numpy? # Maybe numpy.array2string return bytes(str(data) + "\n", "ascii") - + + def ber_stats(self): + ber_max = np.max(self.ber_samples) + ber_min = np.min(self.ber_samples) + ber_avg = np.sum(self.ber_samples) / self.ber_samples.maxlen + + return ber_max, ber_min, ber_avg + def work(self, input_items, output_items): inp = input_items[0] log.debug(f"Length: {len(inp)}") - log.debug(f"Inp_vector:{inp}") + # log.debug(f"Inp_vector:{inp}") + + for i in inp: i = np.array(i, dtype=np.uint8) v = np.array(self.vgl, dtype=np.uint8) ^ i @@ -81,11 +94,20 @@ class ber(gr.sync_block): trueber = ber - 32 if trueber < 0: trueber = 0 - log.debug(f"BER {trueber} in Paket {i}") - - self.send(self.encode(trueber)) + # log.debug(f"BER {trueber} in Paket {i}") + + self.ber_samples.appendleft(trueber) - return len(inp) + ber_max, ber_min, ber_avg = self.ber_stats() + log.debug(f"Statistics: {ber_max}, {ber_min}, {ber_avg}") + + #self.send(self.encode(ber_max, ber_min, ber_avg)) + self.send(self.encode(trueber)) + self.send(self.encode(ber_max)) + self.send(self.encode(ber_min)) + self.send(self.encode(ber_avg)) + + return len(inp) #return len(input_items[0]) diff --git a/src/gr-fadingui/python/logger.py b/src/gr-fadingui/python/logger.py index a101b9e..a189aeb 100644 --- a/src/gr-fadingui/python/logger.py +++ b/src/gr-fadingui/python/logger.py @@ -10,5 +10,5 @@ def get_logger(module): log.addHandler(stdout_handler) stdout_handler.setFormatter(formatter) - log.setLevel(logging.WARNING) + log.setLevel(logging.DEBUG) return log -- cgit v1.2.1 From 06f4ef810c7089c8b994f438b90b293762133bdd Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 14 Dec 2021 19:40:03 +0100 Subject: Implement network_value to show BER --- src/gr-fadingui/python/ber.py | 10 ++-------- src/gui/gui.py | 11 +++++++++-- src/gui/net.py | 24 +++++++++++++++++++++--- 3 files changed, 32 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/gr-fadingui/python/ber.py b/src/gr-fadingui/python/ber.py index 1c5debf..664780f 100644 --- a/src/gr-fadingui/python/ber.py +++ b/src/gr-fadingui/python/ber.py @@ -43,7 +43,7 @@ class ber(gr.sync_block): self.vgl=vgl self.vlen=vlen - self.ber_samples = RingBuffer(capacity=2000, dtype=int) + self.ber_samples = RingBuffer(capacity=100, dtype=int) self.ber_samples.extend(np.zeros(self.ber_samples.maxlen)) # Create a socket and parse remote machine url @@ -84,8 +84,6 @@ class ber(gr.sync_block): log.debug(f"Length: {len(inp)}") # log.debug(f"Inp_vector:{inp}") - - for i in inp: i = np.array(i, dtype=np.uint8) v = np.array(self.vgl, dtype=np.uint8) ^ i @@ -102,11 +100,7 @@ class ber(gr.sync_block): ber_max, ber_min, ber_avg = self.ber_stats() log.debug(f"Statistics: {ber_max}, {ber_min}, {ber_avg}") - #self.send(self.encode(ber_max, ber_min, ber_avg)) - self.send(self.encode(trueber)) - self.send(self.encode(ber_max)) - self.send(self.encode(ber_min)) - self.send(self.encode(ber_avg)) + self.send(self.encode([trueber, ber_max, ber_avg])) return len(inp) #return len(input_items[0]) diff --git a/src/gui/gui.py b/src/gui/gui.py index 6091618..9ac69f7 100755 --- a/src/gui/gui.py +++ b/src/gui/gui.py @@ -253,12 +253,18 @@ with window(label="Bit Error Rate ", width=300, height=150, pos=(1200,875), no_t add_theme_color(mvThemeCol_Text,(0,0,0))#Schwarz add_theme_style(mvStyleVar_FrameRounding, 5) - add_button(label="BER", height=60, width=150,pos=(75,60)) - bind_item_theme(last_item(),"button_ber") + add_button(label="BER", height=60, width=-1, tag="ber_value") + bind_item_theme(last_item(), "button_ber") # bind_item_theme(ber_window, "ber_window") # bind_item_font(ber_window, test) +def set_ber(values): + ber_curr, ber_max, ber_avg = values + configure_item("ber_value", label=f"Current: {ber_curr}, Max: {ber_max}, Avg: {ber_avg}") + +ber_value = net.network_value(url="udp://localhost:31420", dtype=float, refresh_func=set_ber) + #================================================ # Picture Window with window(label="Picture", width=400, height=300, pos=(0,825)) as picture_window : @@ -275,6 +281,7 @@ set_primary_window("primary_window", True) while is_dearpygui_running(): for plt, tag in network_plots.items(): plt.refresh_series(tag) + ber_value.refresh() render_dearpygui_frame() diff --git a/src/gui/net.py b/src/gui/net.py index 715e7a0..f836f42 100644 --- a/src/gui/net.py +++ b/src/gui/net.py @@ -12,11 +12,12 @@ class udpsource: """ Creates an UDP listening socket """ - def __init__(self, url, dtype, timeout=0.05): + def __init__(self, url, dtype, timeout=0.05, blocksize=1024): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.url = urlparse(url) self.dtype = dtype self.timeout = timeout + self.blocksize = blocksize def __del__(self): self.sock.close() @@ -33,8 +34,7 @@ class udpsource: return None # read from socket - blocksize = 1024 * 4 - string = ready[0].recv(nblocks * blocksize).decode("ascii") + string = ready[0].recv(nblocks * self.blocksize).decode("ascii") # decode string, remove empty values chunks = filter(None, re.split(r"\[(.+?)\]", string)) @@ -55,6 +55,24 @@ class udpsource: return values +class network_value(udpsource): + def __init__(self, url, dtype, refresh_func): + udpsource.__init__(self, url, dtype, blocksize=16) + + self._refresh = refresh_func + self.value = None + + self.bind() + + def read(self): + return udpsource.read(self, 1) + + def refresh(self): + self.value = self.read() + if self.value: + self._refresh(self.value) + + class network_plot(udpsource): """ Wraps a udpsource while at the same time intefacing with DearPyGUI as a plot element. -- cgit v1.2.1