python-ofdm/main.py
2019-08-25 15:55:13 -06:00

70 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3.7
"""
Shitty OFDM simulator designed to make it so I understand OFDM.
Hopefully eventually this modem design makes it onto an fpga.
TODO:
Add comments for functions
Explain what the main function is doing
Finish doing the channel simulation stuff (add noise, and verify channel response)
Add support for 16-QAM, 64-QAM, etc...
Add channel estimation via pilot carriers
Add some sort of payload support, i.e. be able to drop the padding at the end
"""
import numpy as np
import matplotlib.pyplot as plt
import channel
import qam
from serpar import parallelise, serialise
def cp_add(in_data, prefix_len):
out_data = np.ndarray((len(in_data), len(in_data[0]) + prefix_len), dtype=np.csingle)
for i in range(len(in_data)):
cp = in_data[i][-prefix_len:]
out_data[i] = np.hstack([cp, in_data[i]])
return out_data
def cp_remove(in_data, prefix_len):
out_data = np.ndarray((len(in_data), len(in_data[0]) - prefix_len), dtype=np.csingle)
for i in range(len(in_data)):
out_data[i] = in_data[i][prefix_len:]
return out_data
if __name__ == '__main__':
with open('data.txt', 'r') as file:
data = file.read()
bytes = bytearray(data, 'utf8')
parallel = parallelise(16, bytes)
modulated = qam.modulate(parallel)
ofdm_time = np.fft.ifft(modulated)
ofdm_prefixed = cp_add(ofdm_time, 4)
# Put the channel simulator stuff here
ofdm_cp_removed = cp_remove(ofdm_prefixed, 4)
to_decode = np.fft.fft(ofdm_cp_removed)
to_serialise = qam.demodulate(to_decode)
data = serialise(16, to_serialise)
print(data)