Skip to content

Commit d52bd2f

Browse files
committed
Add a test for bit_lvl_repr and hundles #29.
1 parent de2f6ed commit d52bd2f

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

commpy/modulation.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import matplotlib.pyplot as plt
2424
from commpy.utilities import bitarray2dec, dec2bitarray
2525
from numpy import arange, array, zeros, pi, cos, sin, sqrt, log2, argmin, \
26-
hstack, repeat, tile, dot, sum, shape, concatenate, exp, \
26+
hstack, repeat, tile, dot, shape, concatenate, exp, \
2727
log, vectorize, empty, eye, kron
2828
from numpy.fft import fft, ifft
29-
from numpy.linalg import qr
29+
from numpy.linalg import qr, norm
3030

3131
__all__ = ['PSKModem', 'QAMModem', 'ofdm_tx', 'ofdm_rx', 'mimo_ml', 'kbest', 'bit_lvl_repr']
3232

@@ -118,7 +118,7 @@ def plotCons(self):
118118
for e in listBin:
119119
for i in range(beta):
120120
Bin.append(ord(e[i]) - 48)
121-
if (ord(e[i]) - 48 == 0):
121+
if ord(e[i]) - 48 == 0:
122122
mot.append(-1)
123123
else:
124124
mot.append(1)
@@ -236,10 +236,12 @@ def mimo_ml(y, h, constellation):
236236
Constellation used to modulate the symbols
237237
238238
"""
239+
_, n = h.shape
239240
m = len(constellation)
240-
x_ideal = array([tile(constellation, m), repeat(constellation, m)])
241-
y_vector = tile(y, m * m)
242-
min_idx = argmin(sum(abs(y_vector - dot(h, x_ideal)), axis=0))
241+
x_ideal = empty((n, pow(m, n)), complex)
242+
for i in range(0, n):
243+
x_ideal[i] = repeat(tile(constellation, pow(m, i)), pow(m, n - i - 1))
244+
min_idx = argmin(norm(y[:, None] - dot(h, x_ideal), axis=0))
243245
x_r = x_ideal[:, min_idx]
244246

245247
return x_r
@@ -340,10 +342,10 @@ def bit_lvl_repr(H, w):
340342
Channel matrix adapted to the bit-level representation.
341343
"""
342344
beta = len(w)
343-
if beta%2 == 0:
345+
if beta % 2 == 0:
344346
m, n = H.shape
345-
In = eye(n,n)
346-
kr = kron(In,w)
347-
return dot(H,kr)
347+
In = eye(n , n)
348+
kr = kron(In , w)
349+
return dot(H , kr)
348350
else:
349351
raise ValueError('Beta must be even.')

commpy/tests/test_modulation.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Authors: Youness Akourim <akourim97@gmail.com>
2+
# License: BSD 3-Clause
3+
4+
from commpy.channels import MIMOFlatChannel
5+
from commpy.links import *
6+
from commpy.modulation import QAMModem, mimo_ml, bit_lvl_repr
7+
from numpy import zeros, identity, arange, concatenate, log2, array
8+
from numpy.random import seed
9+
from numpy.testing import run_module_suite, assert_allclose, dec
10+
11+
12+
@dec.slow
13+
def test_bit_lvl_repr():
14+
qam = QAMModem(4)
15+
16+
nb_rx = 2
17+
nb_tx = 2
18+
RayleighChannel = MIMOFlatChannel(nb_tx, nb_rx)
19+
RayleighChannel.fading_param = (zeros((nb_rx, nb_tx), complex), identity(nb_tx), identity(nb_rx))
20+
21+
SNR = arange(10, 16, 5)
22+
23+
def receiverWithBLR(y, H, cons):
24+
beta = int(log2(len(cons)))
25+
# creation de w
26+
reel = [pow(2, i) for i in range(beta // 2 - 1, -1, -1)]
27+
im = [1j * pow(2, i) for i in range(beta // 2 - 1, -1, -1)]
28+
w = concatenate((reel, im), axis=None)
29+
A = bit_lvl_repr(H, w)
30+
mes = array(mimo_ml(y, A, [-1, 1]))
31+
mes[mes == -1] = 0
32+
return mes
33+
34+
def receiverWithoutBLR(y, H, cons):
35+
return qam.demodulate(mimo_ml(y, H, cons), 'hard')
36+
37+
MymodelWithoutBLR = \
38+
linkModel(qam.modulate, RayleighChannel, receiverWithoutBLR, qam.num_bits_symbol, qam.constellation, qam.Es)
39+
MymodelWithBLR = \
40+
linkModel(qam.modulate, RayleighChannel, receiverWithBLR, qam.num_bits_symbol, qam.constellation, qam.Es)
41+
42+
BERWithoutBLR = link_performance(MymodelWithoutBLR, SNR, 300e4, 300)
43+
BERWithBLR = link_performance(MymodelWithBLR, SNR, 300e4, 300)
44+
assert_allclose(BERWithoutBLR, BERWithBLR, rtol=0.25,
45+
err_msg='bit_lvl_repr changes the performance')
46+
47+
48+
if __name__ == "__main__":
49+
seed(17121996)
50+
run_module_suite()

0 commit comments

Comments
 (0)