From 1c87f451f3302cfeb6153af5b307eeef274ed50f Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Sun, 25 Aug 2019 15:33:33 -0600 Subject: [PATCH] Moved QAM into it's own file --- main.py | 36 +----------------------------------- qam.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 qam.py diff --git a/main.py b/main.py index 1dc5615..36eebae 100755 --- a/main.py +++ b/main.py @@ -7,42 +7,13 @@ from channel import channel_sim from serpar import parallelise, serialise - - -def qam(n, in_data): - """ - Modulates into 4-QAM encoding, might change that number later. - - Parameters: - n - number of channels to operate on - in_data - m X n array, m symbols to run on - - Output: - data - """ - - #initialise output array - out_data = np.ndarray((len(in_data), n), dtype=np.csingle) - - for i in range(len(in_data)): - for j in range(n): - - #4-QAM is nice - out_data[i][j] = 1 + 1j - - # Just rotate 90 degrees for every number and you've got your encoding - for k in range(in_data[i][j]): - out_data[i][j] = out_data[i][j] * (1j) - - return out_data +import qam def cyclic_prefix(n, in_data, prefix_len): out_data = np.ndarray((len(in_data), n + prefix_len), dtype=np.csingle) - - if __name__ == '__main__': with open('data.txt', 'r') as file: data = file.read() @@ -57,11 +28,6 @@ if __name__ == '__main__': rx = channel_sim(tx) - plt.plot(tx[0], 'r') - plt.plot(rx[0], 'b') - plt.show() - - print(channel_sim(pre_modulated)) diff --git a/qam.py b/qam.py new file mode 100644 index 0000000..879e905 --- /dev/null +++ b/qam.py @@ -0,0 +1,52 @@ +import numpy as np +from scipy.spatial.distances import euclidean + +qam_mapping_table = { + 0 : 1 + 1j, + 1 : -1 + 1j, + 2 : -1 - 1j, + 3 : 1 - 1j +} + +def qam_demapping_table = { x, y for y, x in qam_mapping_table.items() } + +def modulate(in_data): + """ + Modulates into 4-QAM encoding, might change that number later. + + Parameters: + in_data - m X n array, m symbols to run on + + Output: + data + """ + + #initialise output array + out_data = np.ndarray((len(in_data), len(in_data[0])), dtype=np.csingle) + + for i in range(len(in_data)): + for j in range(len(in_data[0])): + out_data[i][j] = qam_mapping_table[in_data[i][j]] + + return out_data + +def demodulate(n, in_data): + out_data = np.ndarray((len(in_data), len(in_data[0])), dtype=np.uint8) + + # Just pull the constellation array data out + constellation = { x for x in qam_demapping_table.keys() } + + for i in range(len(in_data)): + for j in range(len(in_data[0])): + distances = np.ndarray((len(constellation)), dtype=np.single) + + # Here we have to map to the closest constellation point, + # because floating point error + for k in range(len(constellation)): + distances[k] = euclidean(in_data[i][j], constellation[i][j]) + + # output is the index of the constellation, essentially + # this may have to change if I want to generalise + out_data[i][j] = np.argmin(distances) + + return out_data