1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import numpy as np import matplotlib.pyplot as plt
class OFDM(): def __init__(self, K, Kon, CP): self.k = K self.kon = Kon self.cp= CP
ofdm = OFDM(128,64,32)
def ofdm_modulation(ofdm, qam): xk = np.zeros(ofdm.k, dtype=complex) off = int(np.ceil((ofdm.k-ofdm.kon)/2)) xk[off:off+ofdm.kon] = qam xk = np.fft.fftshift(xk) xn = np.fft.ifft(xk) * np.sqrt(ofdm.k) xn = np.hstack([xn[-ofdm.cp:], xn]) return xn
def random_qam(ofdm): qam = np.array([1+1j, 1-1j, -1+1j, -1-1j]) / np.sqrt(2) return np.random.choice(qam, size=ofdm.kon, replace=True)
constellation = random_qam(ofdm)
tx_sig = np.array([]) for i in range(4): tx_sig = np.append(tx_sig, ofdm_modulation(ofdm, constellation)) f= np.linspace(-ofdm.k/2, ofdm.k/2, len(tx_sig)*8, endpoint=False) tx_spectrum = np.abs(np.fft.fftshift(np.fft.fft(tx_sig, 8*len(tx_sig))))
plt.figure(3) class Basic_OFDM_Receiver(): def __init__(self, ofdm_param): self._ofdm = ofdm_param self._symbol_len = ofdm_param.k + ofdm_param.cp self._current_symbol = np.array([]) self._symbol_without_cp = np.array([]) self._samps_left_in_symbol = self._symbol_len self._constellation = np.array([])
def receive(self, data): while(len(data) >= self._samps_left_in_symbol): self._current_symbol = np.append(self._current_symbol, data[:self._samps_left_in_symbol]) data = data[self._samps_left_in_symbol:] self._symbol_without_cp = np.append(self._symbol_without_cp, self._current_symbol[self._ofdm.cp:]) self.dft(self._symbol_without_cp) self._samps_left_in_symbol = self._symbol_len self._current_symbol = self._current_symbol[self._samps_left_in_symbol:] self._symbol_without_cp = np.array([])
self._current_symbol = np.append(self._current_symbol, data) self._samps_left_in_symbol -= len(data)
def dft(self, data): self._constellation = np.fft.fftshift(np.fft.fft(data)) plt.plot(self._constellation.real, self._constellation.imag, 'ro')
ofdm_rx = Basic_OFDM_Receiver(ofdm) ofdm_rx.receive(tx_sig) plt.show()
|