Skip to content

Commit

Permalink
Updating the wavfile decoding algorithm using the TimeSync class #24
Browse files Browse the repository at this point in the history
  • Loading branch information
mgm8 committed May 18, 2024
1 parent 45d2085 commit 5d8ca9b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 71 deletions.
117 changes: 47 additions & 70 deletions spacelab_decoder/spacelabdecoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@


import os
import threading
import sys
import signal
from datetime import datetime
import pathlib
import json
import csv

Expand All @@ -39,13 +35,12 @@

import matplotlib.pyplot as plt
from scipy.io import wavfile
import zmq

import pyngham

from spacelab_decoder.mm_decoder import mm_decoder
import spacelab_decoder.version

from spacelab_decoder.time_sync import TimeSync
from spacelab_decoder.bit_buffer import BitBuffer, _BIT_BUFFER_LSB
from spacelab_decoder.sync_word import SyncWord, _SYNC_WORD_LSB
from spacelab_decoder.byte_buffer import ByteBuffer, _BYTE_BUFFER_LSB
Expand Down Expand Up @@ -84,12 +79,7 @@
_DEFAULT_DOWNLINK_SYNC_WORD = '0x5DE62A7E'
_DEFAULT_MAX_PKT_LEN_BYTES = 300

_ZMQ_PUSH_PULL_ADDRESS = "tcp://127.0.0.1:2112"

_TOOLS_FILTERS = ["None", "Low pass", "High pass"]
_WAVFILE_BUFFER_FILE = "/tmp/spacelab_decoder_buffer.wav"

#Defining logfile default local
# Defining logfile default local
_DIR_CONFIG_LOGFILE_LINUX = 'spacelab_decoder'
_DEFAULT_LOGFILE_PATH = os.path.join(os.path.expanduser('~'), _DIR_CONFIG_LOGFILE_LINUX)
_DEFAULT_LOGFILE = 'logfile.csv'
Expand Down Expand Up @@ -294,10 +284,7 @@ def on_button_decode_clicked(self, button):
wav_filename = self.filechooser_audio_file.get_filename()
self.write_log("Audio file opened with a sample rate of " + str(sample_rate) + " Hz")

x = threading.Thread(target=self._decode_audio, args=(wav_filename, sample_rate, 1200, self.checkbutton_play_audio.get_active()))
z = threading.Thread(target=self._zmq_receiver)
x.start()
z.start()
self._decode_audio(self.filechooser_audio_file.get_filename(), 1200)

def on_button_plot_clicked(self, button):
if self.filechooser_audio_file.get_filename() is None:
Expand Down Expand Up @@ -443,20 +430,19 @@ def _load_default_preferences(self):
self.entry_preferences_downlink_s1.set_text('0x' + _DEFAULT_DOWNLINK_SYNC_WORD[6:8])
self.entry_preferences_downlink_s0.set_text('0x' + _DEFAULT_DOWNLINK_SYNC_WORD[8:10])

def _decode_audio(self, audio_file, sample_rate, baud, play):
tb = mm_decoder(input_file=audio_file, samp_rate=sample_rate, baudrate=baud, zmq_adr=_ZMQ_PUSH_PULL_ADDRESS, play_audio=play)
def _decode_audio(self, audio_file, baud):
sample_rate, data = wavfile.read(audio_file)

tb.start()
tb.wait()
samples = list()

def _zmq_receiver(self):
context = zmq.Context()
bits_receiver = context.socket(zmq.PULL)
bits_receiver.connect(_ZMQ_PUSH_PULL_ADDRESS)
# Convert stereo to mono by reading only the first channel
for i in range(len(data)):
samples.append(data[i][0])

poller = zmq.Poller()
poller.register(bits_receiver, zmq.POLLIN)
mm = TimeSync()
self._find_ngham_pkts(mm.get_bitstream(samples, sample_rate, baud))

def _find_ngham_pkts(self, bitstream):
sync_word_buf = BitBuffer(32, _BIT_BUFFER_LSB)

sync_word_str = list()
Expand All @@ -481,50 +467,41 @@ def _zmq_receiver(self):
packet_detected = False
packet_buf = list()

while True:
socks = dict(poller.poll(1000))
if socks:
if socks.get(bits_receiver) == zmq.POLLIN:
bits = bits_receiver.recv(zmq.NOBLOCK)

for bit in bits:
if packet_detected:
byte_buf.push(bool(bit))
if byte_buf.is_full():
if len(packet_buf) < _DEFAULT_MAX_PKT_LEN_BYTES:
pkt_byte = byte_buf.to_byte()
packet_buf.append(pkt_byte)

pl, err, err_loc = self.ngham.decode_byte(pkt_byte)
if len(pl) == 0:
if err == -1:
packet_detected = False
if self.combobox_packet_type.get_active() == 0:
self.write_log("Error decoding a Beacon packet!")
elif self.combobox_packet_type.get_active() == 1:
self.write_log("Error decoding a Downlink packet!")
else:
self.write_log("Error decoding a Packet!")
else:
packet_detected = False
tm_now = datetime.now()
self.decoded_packets_index.append(self.textbuffer_pkt_data.create_mark(str(tm_now), self.textbuffer_pkt_data.get_end_iter(), True))
if self.combobox_packet_type.get_active() == 0:
self.write_log("Beacon packet decoded!")
elif self.combobox_packet_type.get_active() == 1:
self.write_log("Downlink packet decoded!")
else:
self.write_log("Packet decoded!")
self._decode_packet(pl)
byte_buf.clear()
sync_word_buf.push(bool(bit))
if (sync_word_buf == sync_word):
packet_buf = []
packet_detected = True
byte_buf.clear()

else:
break
for bit in bitstream:
if packet_detected:
byte_buf.push(bool(bit))
if byte_buf.is_full():
if len(packet_buf) < _DEFAULT_MAX_PKT_LEN_BYTES:
pkt_byte = byte_buf.to_byte()
packet_buf.append(pkt_byte)

pl, err, err_loc = self.ngham.decode_byte(pkt_byte)
if len(pl) == 0:
if err == -1:
packet_detected = False
if self.combobox_packet_type.get_active() == 0:
self.write_log("Error decoding a Beacon packet!")
elif self.combobox_packet_type.get_active() == 1:
self.write_log("Error decoding a Downlink packet!")
else:
self.write_log("Error decoding a Packet!")
else:
packet_detected = False
tm_now = datetime.now()
self.decoded_packets_index.append(self.textbuffer_pkt_data.create_mark(str(tm_now), self.textbuffer_pkt_data.get_end_iter(), True))
if self.combobox_packet_type.get_active() == 0:
self.write_log("Beacon packet decoded!")
elif self.combobox_packet_type.get_active() == 1:
self.write_log("Downlink packet decoded!")
else:
self.write_log("Packet decoded!")
self._decode_packet(pl)
byte_buf.clear()
sync_word_buf.push(bool(bit))
if (sync_word_buf == sync_word):
packet_buf = []
packet_detected = True
byte_buf.clear()

def _decode_packet(self, pkt):
pkt_txt = "Decoded packet from \"" + self.filechooser_audio_file.get_filename() + "\":\n"
Expand Down
2 changes: 1 addition & 1 deletion spacelab_decoder/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
__copyright__ = "Copyright The SpaceLab-Decoder Contributors"
__credits__ = ["Gabriel Mariano Marcelino - PU5GMA"]
__license__ = "GPLv3"
__version__ = "0.3.20"
__version__ = "0.3.21"
__maintainer__ = "Gabriel Mariano Marcelino - PU5GMA"
__email__ = "gabriel.mm8@gmail.com"
__status__ = "Development"

0 comments on commit 5d8ca9b

Please sign in to comment.