From f22dfb027442732da10b448f3b71de58e7c1669f Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Sun, 25 Aug 2019 15:22:04 -0600 Subject: [PATCH] Moved serialisation/parallelisation to new file --- main.py | 58 ++---------------------------------------- serpar.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 serpar.py diff --git a/main.py b/main.py index e3776c4..1dc5615 100755 --- a/main.py +++ b/main.py @@ -2,64 +2,10 @@ import numpy as np import matplotlib.pyplot as plt -from math import floor, ceil from channel import channel_sim -def parralelise(n, in_data): - """ - Parameters: - n - number of channels - in_data - input data, array of - - Output: - array of OFDM symbols, each symbol - """ - - data_len = len(in_data) - - # Find number of OFDM symbols - # Multiplied by four here because we have 2 bits per QAM symbol/channel/whatever - # so 8 / 2 = 4 - symbols = float(data_len * 4 / n) - - # Check if we have to pad last symbol - if symbols.is_integer(): - symbols = int(symbols) - else: - symbols = ceil(symbols) - - - # Initialise output array - out_data = np.ndarray(shape=(int(symbols), n), dtype=int) - - # Just a way to keep track of where I am in the byte array - # I bet there's a more "python-y" way to do this but whatever - byte_index = 0 - bit_index = 0 - - # Will have to be revamped if I want to do more than 4-QAM modulation - for i in range(symbols): - for j in range(n): - # if we have exhausted data, pad with zeroes - if i == symbols - 1 and byte_index == data_len: - for k in range(j, n): - out_data[i][k] = 0 - - break - - # Isolate the correct bits - out_data[i][j] = (in_data[byte_index] >> (bit_index * 2)) & 0b11 - - bit_index += 1 - - if bit_index == 4: - byte_index += 1 - bit_index = 0 - - - return out_data - +from serpar import parallelise, serialise @@ -103,7 +49,7 @@ if __name__ == '__main__': bytes = bytearray(data, 'utf8') - parallel = parralelise(16, bytes) + parallel = parallelise(16, bytes) modulated = qam(16, parallel) diff --git a/serpar.py b/serpar.py new file mode 100644 index 0000000..9a44537 --- /dev/null +++ b/serpar.py @@ -0,0 +1,75 @@ +# the naming is terrible but eh +import numpy as np +from math import floor, ceil + +def parallelise(n, in_data): + """ + Parameters: + n - number of channels + in_data - input data, array of + + Output: + array of OFDM symbols, each symbol + """ + + data_len = len(in_data) + + # Find number of OFDM symbols + # Multiplied by four here because we have 2 bits per QAM symbol/channel/whatever + # so 8 / 2 = 4 + symbols = float(data_len * 4 / n) + + # Check if we have to pad last symbol + if symbols.is_integer(): + symbols = int(symbols) + else: + symbols = ceil(symbols) + + + # Initialise output array + out_data = np.ndarray(shape=(int(symbols), n), dtype=int) + + # Just a way to keep track of where I am in the byte array + # I bet there's a more "python-y" way to do this but whatever + byte_index = 0 + bit_index = 0 + + # Will have to be revamped if I want to do more than 4-QAM modulation + for i in range(symbols): + for j in range(n): + # if we have exhausted data, pad with zeroes + if i == symbols - 1 and byte_index == data_len: + for k in range(j, n): + out_data[i][k] = 0 + + break + + # Isolate the correct bits + out_data[i][j] = (in_data[byte_index] >> (bit_index * 2)) & 0b11 + + bit_index += 1 + + if bit_index == 4: + byte_index += 1 + bit_index = 0 + + + return out_data + +def serialise(n, in_data): + out_data = bytearray() + + # Will need to be changed to deal with more QAM schemes + bytes_per_symbol = int(n / 4) + + for i in range(len(in_data)): + for j in range(bytes_per_symbol): + new_byte = np.uint8(0) + + for k in range(4): + new_byte |= in_data[i][(j*4) + k] << (k * 2) + + out_data.append(new_byte) + + return out_data +