-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathofdm.py
235 lines (179 loc) · 10 KB
/
ofdm.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import numpy as np
from utils import dec2bin, bin2dec, polar2rect
from modulation import qammod, qamdemod, pskmod, pskdemod
from convcode import Trellis, conv_encode, viterbi_decode
class OFDMTransmitter(object):
def __init__(self, **params):
# necessary params
self.N = params['N']
self.modType = params['modType']
self.M = params['M']
# optional params
self.cyclicPrefix = params['cyclicPrefix'] if 'cyclicPrefix' in params else int(0.25 * self.N)
self.upsampleFactor = params['upsampleFactor'] if 'upsampleFactor' in params else 1
self.useConvCode = params['useConvCode'] if 'useConvCode' in params else False
self.convCodeGMatrix = params['convCodeGMatrix'] if 'convCodeGMatrix' in params else [0o5, 0o7]
self.useClipping = params['useClipping'] if 'useClipping' in params else False
self.clippingPercent = params['clippingPercent'] if 'clippingPercent' in params else 0.75
self.useSLM = params['useSLM'] if 'useSLM' in params else False
self.SLMCandidates = params['SLMCandidates'] if 'SLMCandidates' in params else 16
self.usePAPRnet = params['usePAPRnet'] if 'usePAPRnet' in params else False
self.PAPRnetEncoder = params['PAPRnetEncoder'] if 'PAPRnetEncoder' in params else None
self.dumpModChunks = params['dumpModChunks'] if 'dumpModChunks' in params else False
if self.clippingPercent == 1.0:
self.useClipping = False
if self.SLMCandidates == 0:
self.useSLM = False
self.SLMPhaseRotationOptions = [1, -1, 1j, -1j]
self.SLMPhaseVectorIdx = None
self.seed = 2727
self.padBits = 0
self.dividingFactor = int(np.log2(self.M) * self.N / self.upsampleFactor)
if len(self.convCodeGMatrix) == 0:
self.useConvCode = False
if self.useConvCode:
memory = np.array([np.max([len(bin(p))-2 for p in self.convCodeGMatrix])-1])
self.trellis = Trellis(memory, np.array([self.convCodeGMatrix]))
self.codeRate = '{}/{}'.format(self.trellis.k, self.trellis.n)
self.dividingFactor = int(self.dividingFactor * self.trellis.n / self.trellis.k)
else:
self.trellis = None
self.codeRate = None
def transmit(self, bitStream):
if (len(bitStream) % self.dividingFactor):
self.padBits = self.dividingFactor - (len(bitStream) % self.dividingFactor)
txBitStream = np.concatenate([bitStream, np.zeros(self.padBits, dtype=bitStream.dtype)])
else:
self.padBits = 0
txBitStream = bitStream
# convolutional encoding
if self.useConvCode:
nBitsToKeep = int(len(txBitStream) * self.trellis.n / self.trellis.k)
txBitStream = conv_encode(txBitStream, self.trellis)
txBitStream = txBitStream[0:nBitsToKeep]
# modulation
# modBitChunks = np.reshape(txBitStream, (-1, int(np.log2(self.M))))
modSymbols = bin2dec(txBitStream, int(np.log2(self.M)))
if self.modType == 'qam':
modSymbolsMapped = qammod(modSymbols, self.M)
elif self.modType == 'psk':
modSymbolsMapped = pskmod(modSymbols, self.M)
else:
raise("unrecognized modulation type : {}".format(self.modType))
# chunking; each row will be converted to a ofdm symbol
ofdmChunks = np.reshape(modSymbolsMapped, (-1, int(self.N/self.upsampleFactor)))
# upsamping
if self.upsampleFactor > 1:
allIndexes = np.arange(1, ofdmChunks.shape[-1]+1)
allIndexes = np.repeat(allIndexes, self.upsampleFactor-1)
ofdmChunks = np.insert(self.ofdmChunks, allIndexes, 0, axis=-1)
# selectively mapping to reduce papr
if self.useSLM:
ofdmChunks = self.applySLM(ofdmChunks)
if self.dumpModChunks:
return ofdmChunks # stored to dump as training data for NN
if self.usePAPRnet:
ofdmChunksRect = np.concatenate( [np.expand_dims(ofdmChunks.real, 1),
np.expand_dims(ofdmChunks.imag, 1)], axis=1) # splitting real and imaginary comp
ofdmChunksNN = self.PAPRnetEncoder.predict(ofdmChunksRect)
ofdmChunks = ofdmChunksNN[:, 0, :] + 1j * ofdmChunksNN[:, 1, :] # converting to complex vals (a+bj)
# OFDM
ofdmSignal = np.fft.ifft(ofdmChunks, n=self.N, axis=-1) # apply ifft on last axis (across columns)
# clipping peaks to reduce papr
if self.useClipping:
ofdmSignal = self.clipOFDM(ofdmSignal)
# adding cyclic prefix
if self.cyclicPrefix > 0:
ofdmSignal = np.concatenate([ofdmSignal[:, (ofdmSignal.shape[-1]-self.cyclicPrefix) : ], ofdmSignal], axis=-1)
return ofdmSignal
def applySLM(self, ofdmChunks):
np.random.seed(self.seed)
self.SLMPhaseVectorIdx = np.zeros(ofdmChunks.shape[0])
phaseVecCandidates = np.random.choice(self.SLMPhaseRotationOptions, (self.SLMCandidates, self.N))
slmChunks = np.zeros_like(ofdmChunks)
for cdx, chunk in enumerate(ofdmChunks):
# multiplying by each phaseVec candidate and finding min obtained papr
minPAPR = np.inf
minPdx = None
for pdx, phaseVec in enumerate(phaseVecCandidates):
mapped = np.multiply(chunk, phaseVec)
mappedTimeDomainSq = np.power(np.abs(np.fft.ifft(mapped, self.N, axis=-1)),2)
PAPR = np.divide(np.max(mappedTimeDomainSq), np.mean(mappedTimeDomainSq))
if PAPR < minPAPR:
minPAPR = PAPR
minPdx = pdx
# storing mapped vec with min papr
slmChunks[cdx] = np.multiply(chunk, phaseVecCandidates[minPdx])
self.SLMPhaseVectorIdx [cdx] = minPdx
return slmChunks
def clipOFDM(self, ofdmChunks):
for cdx, chunk in enumerate(ofdmChunks):
clippingVal = self.clippingPercent * np.max(np.abs(chunk))
for vdx, val in enumerate(chunk):
if np.abs(val) > clippingVal:
# clipping magnitude, phase same as before
ofdmChunks[cdx, vdx] = polar2rect(clippingVal, np.angle(val, deg=True))
return ofdmChunks
class OFDMReceiver(object):
def __init__(self, **params):
# necessary params
self.N = params['N']
self.modType = params['modType']
self.M = params['M']
# optional params
self.cyclicPrefix = params['cyclicPrefix'] if 'cyclicPrefix' in params else int(0.25 * self.N)
self.upsampleFactor = params['upsampleFactor'] if 'upsampleFactor' in params else 1
self.useConvCode = params['useConvCode'] if 'useConvCode' in params else False
self.convCodeGMatrix = params['convCodeGMatrix'] if 'convCodeGMatrix' in params else [0o5, 0o7]
self.useSLM = params['useSLM'] if 'useSLM' in params else False
self.SLMCandidates = params['SLMCandidates'] if 'SLMCandidates' in params else 16
self.usePAPRnet = params['usePAPRnet'] if 'usePAPRnet' in params else False
self.PAPRnetDecoder = params['PAPRnetDecoder'] if 'PAPRnetDecoder' in params else None
if len(self.convCodeGMatrix) == 0:
self.useConvCode = False
if self.useConvCode:
memory = np.array([np.max([len(bin(p))-2 for p in self.convCodeGMatrix])-1])
self.trellis = Trellis(memory, np.array([self.convCodeGMatrix]))
if self.SLMCandidates == 0:
self.useSLM = False
self.SLMPhaseRotationOptions = [1, -1, 1j, -1j]
self.SLMPhaseVectorIdx = None
self.seed = 2727
self.padBits = 0
def receive(self, signal):
# creating chunks corresponding to ofdm symbols
ofdmChunks = np.reshape(signal, (-1, self.N + self.cyclicPrefix))
# removing cyclic prefix
ofdmChunks = ofdmChunks[:, self.cyclicPrefix: ]
# converting OFDM signal to mapped mod symbols
modSymbolsMapped = np.fft.fft(ofdmChunks, n=self.N, axis=-1)
if self.usePAPRnet:
modSymbolsMappedRect = np.concatenate( [np.expand_dims(modSymbolsMapped.real, 1),
np.expand_dims(modSymbolsMapped.imag, 1)], axis=1) # splitting real and imaginary comp
modSymbolsMappedNN= self.PAPRnetDecoder.predict(modSymbolsMappedRect)
modSymbolsMapped = modSymbolsMappedNN[:, 0, :] + 1j * modSymbolsMappedNN[:, 1, :] # converting to complex vals (a+bj)
# reversing selective mapping
if self.useSLM:
modSymbolsMapped = self.unmapSLM(modSymbolsMapped)
# downsampling
if self.upsampleFactor > 1:
modSymbolsMapped = modSymbolsMapped[:, ::(self.upsampleFactor)]
# un-mapping mod symbols
modSymbols = qamdemod(modSymbolsMapped, self.M)
# converting back to bits
bitStream = dec2bin(modSymbols.flatten(), int(np.log2(self.M)))
# convolutional decoding using viterbi algorithm
if self.useConvCode:
bitStream = viterbi_decode(bitStream, self.trellis)
return bitStream[0:len(bitStream)-self.padBits]
def unmapSLM(self, modSymbols):
if self.SLMPhaseVectorIdx is None:
raise Exception("phase vector indexes use for slm not provided")
elif self.SLMPhaseVectorIdx.shape[0] != modSymbols.shape[0]:
raise Exception("number of phase vector indexes not the same as number of symbols received")
np.random.seed(self.seed)
phaseVecCandidates = np.random.choice(self.SLMPhaseRotationOptions, (self.SLMCandidates, self.N))
unmappedSymbols = np.zeros_like(modSymbols)
for sdx, (pdx, sym) in enumerate(zip(self.SLMPhaseVectorIdx, modSymbols)):
unmappedSymbols[sdx] = np.multiply(np.conj(phaseVecCandidates[int(pdx)]), sym) # multiplying by complex conjugate of phase vector
return unmappedSymbols