1- # Authors: Veeresh Taranalli <veeresht@gmail.com> & Bastien Trotobas <bastien.trotobas@gmail.com>
1+ # Authors: Veeresh Taranalli <veeresht@gmail.com> & Bastien Trotobas <bastien.trotobas@gmail.com> & Youness Akourim <akourim97@gmail.com>
22# License: BSD 3-Clause
33
44"""
1515 ofdm_rx -- OFDM Receive Signal Processing
1616 mimo_ml -- MIMO Maximum Likelihood (ML) Detection.
1717 kbest -- MIMO K-best Schnorr-Euchner Detection.
18+ bit_lvl_repr -- Bit level representation
1819
1920"""
2021from itertools import product
2122
23+ import matplotlib .pyplot as plt
24+ from commpy .utilities import bitarray2dec , dec2bitarray
2225from numpy import arange , array , zeros , pi , cos , sin , sqrt , log2 , argmin , \
2326 hstack , repeat , tile , dot , sum , shape , concatenate , exp , \
24- log , vectorize , empty
27+ log , vectorize , empty , eye , kron
2528from numpy .fft import fft , ifft
2629from numpy .linalg import qr
2730
28- from commpy .utilities import bitarray2dec , dec2bitarray
29-
30- __all__ = ['PSKModem' , 'QAMModem' , 'ofdm_tx' , 'ofdm_rx' , 'mimo_ml' , 'kbest' ]
31+ __all__ = ['PSKModem' , 'QAMModem' , 'ofdm_tx' , 'ofdm_rx' , 'mimo_ml' , 'kbest' , 'bit_lvl_repr' ]
3132
3233
3334class Modem :
@@ -99,6 +100,44 @@ def demodulate(self, input_symbols, demod_type, noise_var=0):
99100
100101 return demod_bits
101102
103+ def plotCons (self ):
104+ """ Plot the constellation """
105+ # init some arrays
106+ beta = self .num_bits_symbol
107+ numbit = '0' + str (beta ) + 'b'
108+ Bin = []
109+ mot = []
110+ const = []
111+
112+ # creation of w array
113+ reel = [pow (2 , i ) for i in range (beta // 2 - 1 , - 1 , - 1 )]
114+ im = [1j * pow (2 , i ) for i in range (beta // 2 - 1 , - 1 , - 1 )]
115+ w = concatenate ((reel , im ), axis = None )
116+
117+ listBin = [format (i , numbit ) for i in range (2 ** beta )]
118+ for e in listBin :
119+ for i in range (beta ):
120+ Bin .append (ord (e [i ]) - 48 )
121+ if (ord (e [i ]) - 48 == 0 ):
122+ mot .append (- 1 )
123+ else :
124+ mot .append (1 )
125+ const .append (dot (w , mot ))
126+ mot = []
127+ symb = self .modulate (Bin )
128+
129+ # plot the symbols
130+ x = symb .real
131+ y = symb .imag
132+
133+ plt .plot (x , y , '+' ,linewidth = 4 )
134+ for i in range (len (x )):
135+ plt .text (x [i ], y [i ] , listBin [i ])
136+
137+ plt .title ('Constellation' )
138+ plt .grid ()
139+ plt .show ()
140+
102141
103142class PSKModem (Modem ):
104143 """ Creates a Phase Shift Keying (PSK) Modem object. """
@@ -225,27 +264,35 @@ def kbest(y, h, constellation, K):
225264
226265 K : positive integer
227266 Number of candidates kept at each step
267+
268+ raises
269+ ------
270+ ValueError
271+ If h has more columns than rows.
228272 """
273+ nb_tx , nb_rx = h .shape
274+ if nb_rx > nb_tx :
275+ raise ValueError ('h has more columns than rows' )
276+
229277 # QR decomposition
230278 q , r = qr (h )
231279 yt = q .conj ().T .dot (y )
232280
233281 # Initialization
234282 m = len (constellation )
235- n = len (yt )
236283 nb_can = 1
237284
238285 if isinstance (constellation [0 ], complex ):
239286 const_type = complex
240287 else :
241288 const_type = float
242- X = empty ((n , K * m ), dtype = const_type ) # Set of current candidates
289+ X = empty ((nb_rx , K * m ), dtype = const_type ) # Set of current candidates
243290 d = tile (yt [:, None ], (1 , K * m )) # Corresponding distance vector
244291 d_tot = zeros (K * m , dtype = float ) # Corresponding total distance
245292 hyp = empty (K * m , dtype = const_type ) # Hypothesis vector
246293
247294 # Processing
248- for coor in range (n - 1 , - 1 , - 1 ):
295+ for coor in range (nb_rx - 1 , - 1 , - 1 ):
249296 nb_hyp = nb_can * m
250297
251298 # Copy best candidates m times
@@ -269,3 +316,29 @@ def kbest(y, h, constellation, K):
269316 d [:coor , :nb_can ] -= r [:coor , coor , None ] * hyp [argsort [:nb_can ]]
270317 d_tot [:nb_can ] = d_tot [argsort [:nb_can ]]
271318 return X [:, 0 ]
319+
320+
321+ def bit_lvl_repr (H , w ):
322+ """ Bit-level representation of matrix H with weights w.
323+
324+ parameters
325+ ----------
326+ H : 2D ndarray (shape : nb_rx, nb_tx)
327+ Channel Matrix.
328+
329+ w : 1D ndarray of complex (length : beta)
330+ Bit level representation weights. The length must be even.
331+
332+ return
333+ ------
334+ A : 2D nbarray (shape : nb_rx, nb_tx*beta)
335+ Channel matrix adapted to the bit-level representation.
336+ """
337+ beta = len (w )
338+ if beta % 2 == 0 :
339+ m , n = H .shape
340+ In = eye (n ,n )
341+ kr = kron (In ,w )
342+ return dot (H ,kr )
343+ else :
344+ raise ValueError ('Beta must be even.' )
0 commit comments