Moved QAM into it's own file

This commit is contained in:
David Lenfesty 2019-08-25 15:33:33 -06:00
parent f22dfb0274
commit 1c87f451f3
2 changed files with 53 additions and 35 deletions

36
main.py
View File

@ -7,42 +7,13 @@ from channel import channel_sim
from serpar import parallelise, serialise from serpar import parallelise, serialise
import qam
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
def cyclic_prefix(n, in_data, prefix_len): def cyclic_prefix(n, in_data, prefix_len):
out_data = np.ndarray((len(in_data), n + prefix_len), dtype=np.csingle) out_data = np.ndarray((len(in_data), n + prefix_len), dtype=np.csingle)
if __name__ == '__main__': if __name__ == '__main__':
with open('data.txt', 'r') as file: with open('data.txt', 'r') as file:
data = file.read() data = file.read()
@ -57,11 +28,6 @@ if __name__ == '__main__':
rx = channel_sim(tx) rx = channel_sim(tx)
plt.plot(tx[0], 'r')
plt.plot(rx[0], 'b')
plt.show()
print(channel_sim(pre_modulated))

52
qam.py Normal file
View File

@ -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