-
Notifications
You must be signed in to change notification settings - Fork 7
/
jmimo.py
1028 lines (812 loc) · 26 KB
/
jmimo.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Author
--------
Best regards,
Sungjin (James) Kim, PhD
Postdoc, CCB in Harvard
sungjinkim@fas.harvard.edu
[Web] http://aspuru.chem.harvard.edu/james-sungjin-kim/
[Linkedin] https://www.linkedin.com/in/jamessungjinkim
[Facebook] https://www.facebook.com/jamessungjin.kim
[alternative email] jamessungjin.kim@gmail.com
Licence
---------
MIT License
"""
from __future__ import print_function
# I started to use __future__ so as to be compatible with Python3
import numpy as np
from sklearn import linear_model
from sklearn import cross_validation
from sklearn import metrics
import pandas as pd
from collections import OrderedDict
# To improve the speed, I using pyx.
import jpyx
import jutil
from jsklearn import codes
def mld( r_l, mod_l = [-0.70710678, 0.70710678]):
"""
maximum likelihood detection
r_l: received signals after reception processing
mod_l: list of all modulation signals
BPSK: [-0.70710678, 0.70710678]
return the demodulated signals (0, 1, ...)
"""
sd_l = list() # store demodulated signal
for r in r_l:
dist = list() #Store distance
for m in mod_l:
d = np.power( np.abs( r - m), 2)
dist.append( d)
sd = np.argmin( dist)
sd_l.append( sd)
return np.array( sd_l)
def calc_BER( r_l, x_l):
"""
calculate bit error rate (BER)
r_l: demodulated signals (ndarray, 1D)
x_l: transmitted signals (ndarray, 1D)
"""
err_l = r_l - x_l
errs = np.where( err_l != 0)[0]
# print 'err_l =', err_l
# print 'errs =', errs
Nerr = len(np.where( err_l != 0)[0])
return float( Nerr) / len( err_l), Nerr
def db2var( SNRdB):
return np.power( 10.0, SNRdB / 10.0)
def gen_BPSK(Nx, Nt):
"""
Generate BPSK modulated signals
"""
BPSK = np.array( [1, -1]) / np.sqrt( 2.0)
s_a = np.random.randint( 0, 2, Nx * Nt)
x_flat_a = BPSK[ s_a]
x_a = np.reshape( x_flat_a, (Nx, Nt))
return BPSK, s_a, x_flat_a, x_a
def gen_H( Nr, Nt):
return np.random.randn( Nr, Nt)
def gen_Rx( Nr, Nx, SNR, H_a, x_a):
"""
The received signals are modeled.
"""
n_a = np.random.randn( Nr, Nx) / np.sqrt( SNR)
y_a = np.dot( H_a, x_a.T) + n_a
return y_a
def normalize( W_a):
"Weight is normalized."
nW_a = np.linalg.norm( W_a, axis = 1)
for a0 in range( W_a.shape[0]):
W_a[a0,:] = np.divide( W_a[a0,:], nW_a[a0])
return W_a
class MIMO(object):
"""
Modeling for a MIMO wireless communication system.
"""
def __init__(self, Nt = 2, Nr = 4, Nx = 10, SNRdB = 10, model = "Ridge", Npilot = 10, Nloop = 10):
"""
The parameter of 'model' determines the regression method.
"""
self.set_param( (Nt, Nr, Nx, SNRdB))
self.model = model
self.Npilot = Npilot
self.Nloop = Nloop
# The function of test_ridge_all() uses 3 cases for testing.
# self.N_test_ridge_all = 3
def set_param( self, param_NtNrNxSNRdB):
Nt, Nr, Nx, SNRdB = param_NtNrNxSNRdB
# The antenna configuration is conducted.
self.Nt = Nt
self.Nr = Nr
# No of streams is fixed.
self.Nx = Nx
# Initial SNR is defined
self.SNRdB = SNRdB
self.SNR = db2var(SNRdB)
def _gen_BPSK_r0(self):
"""
Generate BPSK modulated signals
"""
self.BPSK = np.array( [1, -1]) / np.sqrt( 2.0)
self.s_a = np.random.randint( 0, 2, self.Nx * self.Nt)
self.x_flat_a = self.BPSK[ self.s_a]
self.x_a = np.reshape( self.x_flat_a, (self.Nx, self.Nt))
def gen_BPSK( self):
"""
Generate BPSK signals using global function gen_BPSK().
This function will be used to generate pilot signal as well.
"""
self.BPSK, self.s_a, self.x_flat_a, self.x_a = gen_BPSK( self.Nx, self.Nt)
def gen_H(self):
"""
The MIMO channel is generated.
"""
self.H_a = gen_H( self.Nr, self.Nt)
def _gen_Rx_r0(self):
"""
The received signals are modeled.
"""
self.n_a = np.random.randn( self.Nr, self.Nx) / np.sqrt( self.SNR)
self.y_a = np.dot( self.H_a, self.x_a.T) + self.n_a
def gen_Rx(self):
"""
The received signals are modeled.
"""
self.y_a = gen_Rx( self.Nr, self.Nx, self.SNR, self.H_a, self.x_a)
def gen_WR_ideal(self):
"""
The reception process with ideal channel estimation
is conducted.
each reception vector of W_a should be noramlized to one.
"""
self.W_a = np.linalg.pinv( self.H_a)
# The reception signal vector is transposed.
self.gen_Decoding()
def gen_WR_pilot(self, pilot_SNRdB):
"""
The reception process with pilot channel estimation
is conducted.
Pilot will be transmitted through random information channel.
"""
pilot_SNR = db2var(pilot_SNRdB)
N_a = np.random.randn( *self.H_a.shape) / np.sqrt( pilot_SNR)
Hp_a = self.H_a + N_a
self.W_a = np.linalg.pinv( Hp_a)
self.gen_Decoding()
def gen_WR_pilot_channel(self, pilot_SNRdB):
"""
The reception process with pilot channel estimation
is conducted.
"""
Npilot = self.Npilot
SNRpilot = db2var( pilot_SNRdB)
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
# print( x_a.shape, yT_a.shape)
lm = linear_model.LinearRegression()
lm.fit( yT_a, x_a)
"""
Power normalization should be considered
unless it is multiplied with both sinal and noise.
In this case, MMSE weight is calculated while
pinv() obtain ZF filter.
"""
self.W_a = lm.coef_
# print( "np.dot( W_a, H_a) =", np.dot( self.W_a, self.H_a))
self.gen_Decoding()
def gs_pilot_reg_only(self, alpha_l):
"""
Grid search is applied for alpha_l.
Later, the best alpha will be selected and decode data using it.
"""
pdo = pd.DataFrame()
for alpha in alpha_l:
pdi = self.cv_pilot_reg_only( alpha)
pdo = pdo.append( pdi, ignore_index = True)
return pdo
def gs_pilot_reg_full(self, alpha_l):
"""
Full means data and pilot are both generated and processed including data decoding
"""
self.gen_BPSK()
self.gen_H()
self.gen_Rx()
self.rx_pilot()
return self.gs_pilot_reg_only( alpha_l)
def gs_pilot_reg_best(self, alpha_l):
"""
Find the best alpha using Ridge regression.
Return
--------
The best alpha is returned.
"""
pdi = self.gs_pilot_reg_only( alpha_l)
# print( 'pdi["E[scores]"]', pdi["E[scores]"])
i_max = np.argmin( pdi["E[scores]"])
alpha_best = pdi["alpha"][i_max]
return alpha_best
def gs_pilot_reg_best_full(self, alpha_l):
"""
Full means data and pilot are both generated and processed including data decoding
"""
self.gen_BPSK()
self.gen_H()
self.gen_Rx()
self.rx_pilot()
return self.gs_pilot_reg_best( alpha_l)
def rx_pilot(self):
Npilot = self.Npilot
SNRpilot = self.SNR
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
self.rx_p = dict()
self.rx_p["yT_a"] = yT_a
self.rx_p["x_a"] = x_a
def cv_pilot_only(self):
"""
Cross-validatin scores are evaluated using LOO.
SNRpilot is equal to SNR, which is SNRdata.
"""
yT_a = self.rx_p["yT_a"]
x_a = self.rx_p["x_a"]
lm = linear_model.LinearRegression()
scores = codes.cross_val_score_loo( lm, yT_a, x_a)
# Output is stored with enviromental variables.
pdi = pd.DataFrame()
pdi["model"] = ["LinearRegression"]
pdi["alpha"] = [0]
pdi["metric"] = ["mean_squared_error"]
pdi["E[scores]"] = [np.mean(scores)]
pdi["std[scores]"] = [np.std(scores)]
pdi["scores"] = [scores]
return pdi
def cv_pilot( self):
self.rx_pilot()
return self.cv_pilot_only()
def _cv_pilot_reg_only_r0(self, alpha = 0):
model = self.model
yT_a = self.rx_p["yT_a"]
x_a = self.rx_p["x_a"]
# kf = KFold()
# loo = cross_validation.LeaveOneOut( x_a.shape[0])
if alpha == 0:
lm = linear_model.LinearRegression()
else:
lm = getattr( linear_model, model)(alpha)
scores = codes.cross_val_score_loo( lm, yT_a, x_a)
return scores
def cv_pilot_reg_only(self, alpha = 0):
model = self.model
yT_a = self.rx_p["yT_a"]
x_a = self.rx_p["x_a"]
# kf = KFold()
# loo = cross_validation.LeaveOneOut( x_a.shape[0])
if alpha == 0:
lm = linear_model.LinearRegression()
else:
lm = getattr( linear_model, model)(alpha)
scores = codes.cross_val_score_loo( lm, yT_a, x_a)
# Output is stored with enviromental variables.
pdi = pd.DataFrame()
pdi["model"] = [model]
pdi["alpha"] = [alpha]
pdi["metric"] = ["mean_squared_error"]
pdi["E[scores]"] = [np.mean(np.power(scores,2))] # MSE
pdi["std[scores]"] = ["t.b.d."]
pdi["scores"] = [scores]
return pdi
def cv_pilot_reg( self, alpha = 0):
self.rx_pilot()
return self.cv_pilot_reg_only( alpha)
def _cv_pilot_reg_r0(self, alpha = 0):
"""
Cross-validatin scores are evaluated using LOO.
SNRpilot is equal to SNR, which is SNRdata.
"""
Npilot = self.Npilot
SNRpilot = self.SNR
model = self.model
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
# kf = KFold()
# loo = cross_validation.LeaveOneOut( x_a.shape[0])
if alpha == 0:
lm = linear_model.LinearRegression()
else:
lm = getattr( linear_model, model)(alpha)
scores = codes.cross_val_score_loo( lm, yT_a, x_a)
return scores
def _gen_WR_pilot_ch_r0(self, pilot_SNRdB, alpha = 0):
"""
The reception process with pilot channel estimation
is conducted.
"""
Npilot = 10
SNRpilot = db2var( pilot_SNRdB)
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
# print( x_a.shape, yT_a.shape)
lm = linear_model.Ridge( alpha)
lm.fit( yT_a, x_a)
self.W_a = lm.coef_
# print( "np.dot( W_a, H_a) =", np.dot( self.W_a, self.H_a))
self.gen_Decoding()
def _gen_WR_pilot_ch_r1(self, pilot_SNRdB, alpha = 0, model = "Ridge"):
"""
The reception process with pilot channel estimation
is conducted.
"""
Npilot = 10
SNRpilot = db2var( pilot_SNRdB)
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
# print( x_a.shape, yT_a.shape)
# Now you can use either Ridge or Lasso methods.
#lm = linear_model.Ridge( alpha)
lm = getattr( linear_model, model)(alpha)
lm.fit( yT_a, x_a)
self.W_a = lm.coef_
# print( "np.dot( W_a, H_a) =", np.dot( self.W_a, self.H_a))
self.gen_Decoding()
def gen_WR_pilot_ch(self, pilot_SNRdB, alpha_l1r = 0, model = "Ridge"):
"""
The reception process with pilot channel estimation
is conducted.
"""
Npilot = self.Npilot
SNRpilot = db2var( pilot_SNRdB)
BPSK, s_a, x_flat_a, x_a = gen_BPSK( Npilot, self.Nt)
# H_a = gen_H( self.Nr, self.Nt)
# H_a = self.H_a
y_a = gen_Rx( self.Nr, Npilot, SNRpilot, self.H_a, x_a)
yT_a = y_a.T
# print( x_a.shape, yT_a.shape)
# Now you can use either Ridge or Lasso methods.
#lm = linear_model.Ridge( alpha)
if model == "ElasticNet":
lm = linear_model.ElasticNet( alpha_l1r[0], alpha_l1r[1])
else:
lm = getattr( linear_model, model)(alpha_l1r)
lm.fit( yT_a, x_a)
self.W_a = lm.coef_
# print( "np.dot( W_a, H_a) =", np.dot( self.W_a, self.H_a))
self.gen_Decoding()
def gen_WR_pilot_only(self, alpha_l1r = 0):
"""
yT_a and x_a was prepared already.
Now, W_a is calculated using alpha and then,
decode data.
For linear regression, alpha_l1r should not be specified except 0.
"""
yT_a = self.rx_p["yT_a"]
x_a = self.rx_p["x_a"]
# for alpha == 0, model is changed to linear regression.
if alpha_l1r == 0:
model = "LinearRegression"
else:
model = self.model
if model == "LinearRegression":
lm = linear_model.LinearRegression()
elif model == "ElasticNet":
lm = linear_model.ElasticNet( alpha_l1r[0], alpha_l1r[1])
else: # This is either Ridge or Lasso
lm = getattr( linear_model, model)(alpha_l1r)
lm.fit( yT_a, x_a)
self.W_a = lm.coef_
# print( "np.dot( W_a, H_a) =", np.dot( self.W_a, self.H_a))
self.gen_Decoding()
def gen_WR( self, pilot_SNRdB = None):
if pilot_SNRdB:
gen_WR_pilot( pilot_SNRdB)
else:
gen_WR_ideal()
def gen_Decoding(self):
"""
The reception process is conducted.
"""
self.W_a = normalize( self.W_a) # not important (useless at this moment)
self.rT_a = np.dot( self.W_a, self.y_a)
self.r_flat_a = self.rT_a.T.flatten()
#print( "type( self.r_flat_a), type( self.BPSK)")
#print( type( self.r_flat_a), type( self.BPSK))
# self.sd_a = jpyx.mld( self.r_flat_a, self.BPSK)
self.sd_a = jpyx.mld_fast( self.r_flat_a, self.BPSK)
self.BER, self.Nerr = calc_BER( self.s_a, self.sd_a)
def run_ideal( self, param_NtNrNxSNRdB = None, Nloop = 10, disp = False):
"""
A system is run from the transmitter to the receiver.
"""
return self.run_pilot( param_NtNrNxSNRdB = param_NtNrNxSNRdB, Nloop = Nloop, disp = disp)
def run_pilot( self, pilot_SNRdB = None, param_NtNrNxSNRdB = None, Nloop = 10, disp = False):
"""
A system is run from the transmitter to the receiver.
"""
if param_NtNrNxSNRdB:
self.set_param( param_NtNrNxSNRdB)
self.gen_BPSK()
BER_l = list()
Nerr_total = 0
for nloop in range( Nloop):
self.gen_H()
self.gen_Rx()
if pilot_SNRdB is not None:
self.gen_WR_pilot( pilot_SNRdB)
else:
self.gen_WR_ideal()
BER_l.append( self.BER)
Nerr_total += self.Nerr
self.BER = np.mean( BER_l)
if disp:
Ntot = self.Nt * self.Nx * Nloop
print( "BER is {} with {}/{} errors at {} SNRdB ".format( self.BER, Nerr_total, Ntot, self.SNRdB))
return self.BER
def run_pilot_channel( self, pilot_SNRdB = None, param_NtNrNxSNRdB = None, Nloop = 10, disp = False):
"""
A system is run from the transmitter to the receiver.
"""
if param_NtNrNxSNRdB:
self.set_param( param_NtNrNxSNRdB)
self.gen_BPSK()
BER_l = list()
Nerr_total = 0
for nloop in range( Nloop):
self.gen_H()
self.gen_Rx()
if pilot_SNRdB is not None:
# self.gen_WR_pilot( pilot_SNRdB)
self.gen_WR_pilot_channel( pilot_SNRdB)
# self.gen_WR_pilot_ch( pilot_SNRdB, alpha)
else:
self.gen_WR_ideal()
BER_l.append( self.BER)
Nerr_total += self.Nerr
self.BER = np.mean( BER_l)
if disp:
Ntot = self.Nt * self.Nx * Nloop
print( "BER is {} with {}/{} errors at {} SNRdB ".format( self.BER, Nerr_total, Ntot, self.SNRdB))
return self.BER
def run_pilot_ch( self, pilot_SNRdB = None, param_NtNrNxSNRdB = None, Nloop = 10, alpha = 0, disp = False):
"""
A system is run from the transmitter to the receiver.
"""
if param_NtNrNxSNRdB:
self.set_param( param_NtNrNxSNRdB)
self.gen_BPSK()
BER_l = list()
Nerr_total = 0
for nloop in range( Nloop):
self.gen_H()
self.gen_Rx()
if pilot_SNRdB:
# self.gen_WR_pilot( pilot_SNRdB)
# self.gen_WR_pilot_channel( pilot_SNRdB)
self.gen_WR_pilot_ch( pilot_SNRdB, alpha)
else:
self.gen_WR_ideal()
BER_l.append( self.BER)
Nerr_total += self.Nerr
self.BER = np.mean( BER_l)
if disp:
Ntot = self.Nt * self.Nx * Nloop
print( "BER is {} with {}/{} errors at {} SNRdB ".format( self.BER, Nerr_total, Ntot, self.SNRdB))
return self.BER
def test_ridge_iter( self, alpha_l):
# Ideal ZF(H)
ID = 0
self.method = "Ideal ZF(H)"
self.model = "ZF"
self.alpha = 0
self.gen_WR_ideal()
yield ID
# Multiple Ridge regressions with alpha_l
for alpha in alpha_l:
ID += 1
self.method = "Ridge each"
self.model = "Ridge"
self.alpha = alpha
self.gen_WR_pilot_only( self.alpha)
yield ID
# Ridge regression with the best alpha among alpha_l
ID += 1
self.method = "Ridge best"
self.model = "Ridge"
self.alpha = self.gs_pilot_reg_best( alpha_l)
self.gen_WR_pilot_only( self.alpha)
yield ID
def test_ridge_all( self, pdi_d_prev, alpha_l):
"""
1. LinearRegression
2. multiple Ridge regression with each alpha in alpha_l
3. Ridge regression with the best alpha among alpha_l
"""
# pdi_d is generated only once.
if pdi_d_prev is None:
pdi_d = dict()
else:
pdi_d = pdi_d_prev
for ID in self.test_ridge_iter(alpha_l):
"""
If pdi_l is not defined yet,
it will be generated first and initial values are stored.
Otherwise, new data are added for the corresponding space.
"""
if pdi_d_prev is None:
pdi = pd.DataFrame()
pdi["Nerr_total"] = [0]
pdi["BER_l"] = [[self.BER]]
else:
pdi = pdi_d[ ID]
pdi["Nerr_total"] = [ pdi["Nerr_total"][0] + self.Nerr]
pdi["BER_l"] = [pdi["BER_l"][0] + [self.BER]]
pdi["method"] = [self.method]
pdi["model"] = [self.model]
pdi["alpha"] = [self.alpha]
# print( 'pdi["BER_l"]', pdi["BER_l"])
pdi["BER"] = [np.mean( pdi["BER_l"][0])]
pdi_d[ ID] = pdi
return pdi_d
def run_gs_pilot_Ridge( self, alpha_l):
"""
Search the best alpha using Ridge.
I focus on Ridge for simplicity at this moment.
Other regularization modes will be used later on.
"""
Nloop = self.Nloop
pdi_d = None
for nloop in range( Nloop):
self.gen_BPSK()
self.gen_H()
self.gen_Rx()
# For fair comparision, pilot is also generated commonly for all methods.
self.rx_pilot()
pdi_d = self.test_ridge_all( pdi_d, alpha_l)
pdo = pd.DataFrame()
for pdi in pdi_d.values():
pdo = pdo.append( pdi, ignore_index = True)
return pdo
def run_pilot_ch_model( self, pilot_SNRdB = None, param_NtNrNxSNRdB = None, Nloop = 10, alpha = 0, disp = False):
"""
A system is run from the transmitter to the receiver.
self.model is used to determine the regression model such as Ridge and Lasso
"""
if param_NtNrNxSNRdB:
self.set_param( param_NtNrNxSNRdB)
self.gen_BPSK()
BER_l = list()
Nerr_total = 0
for nloop in range( Nloop):
self.gen_H()
self.gen_Rx()
if pilot_SNRdB is not None: # 'is' needed for checking None
# self.gen_WR_pilot( pilot_SNRdB)
# self.gen_WR_pilot_channel( pilot_SNRdB)
self.gen_WR_pilot_ch( pilot_SNRdB, alpha, self.model)
else:
self.gen_WR_ideal()
BER_l.append( self.BER)
Nerr_total += self.Nerr
self.BER = np.mean( BER_l)
if disp:
Ntot = self.Nt * self.Nx * Nloop
print( "BER is {} with {}/{} errors at {} SNRdB ".format( self.BER, Nerr_total, Ntot, self.SNRdB))
return self.BER
def get_BER_pilot_ch_model_eqsnr(
self,
SNRdB_l = [5,6,7],
param_NtNrNx = (2,4,100),
Nloop = 1000,
pilot_ch = False,
alpha = 0,
model = "Ridge"):
"""
Ridge regression will be using to estimate channel.
If alpha is zero, linear regression will be applied.
If alpha is more than zero, Ridge regression will be applied.
The default value of alpha is zero.
"""
Nt, Nr, Nx = param_NtNrNx
BER_pilot = list()
for SNRdB in SNRdB_l:
# if pilot channel is used, SNRdB is given
# Otherwise, ideal channel estimation is assumed.
if pilot_ch:
pilot_SNRdB = SNRdB
else:
pilot_SNRdB = None
if alpha > 0:
"""
Ridge or Lasso is used.
"""
self.model = model
ber = self.run_pilot_ch_model( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, alpha = alpha, disp = True)
BER_pilot.append( ber)
else:
"""
LinearRegression is used.
"""
ber = self.run_pilot_channel( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, disp = True)
BER_pilot.append( ber)
# print( "List of average BERs =", BER_pilot)
return BER_pilot
def get_BER_pilot_ch_model( self,
SNRdB_l = [5,6,7],
param_NtNrNx = (2,4,100),
Nloop = 1000,
pilot_SNRdB = None,
alpha = 0,
model = "Ridge"):
"""
Ridge regression will be using to estimate channel.
If alpha is zero, linear regression will be applied.
If alpha is more than zero, Ridge regression will be applied.
The default value of alpha is zero.
This function becomes a member function of class MIMO.
"""
BER_pilot = list()
Nt, Nr, Nx = param_NtNrNx
if alpha > 0:
"""
Ridge or Lasso is used.
"""
for SNRdB in SNRdB_l:
self.model = model
ber = self.run_pilot_ch_model( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, alpha = alpha, disp = True)
BER_pilot.append( ber)
else:
"""
LinearRegression is used.
"""
for SNRdB in SNRdB_l:
ber = self.run_pilot_channel( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, disp = True)
BER_pilot.append( ber)
# print( "List of average BERs =", BER_pilot)
return BER_pilot
def get_BER( SNRdB_l = [5,6,7], param_NtNrNx = (2,4,100), Nloop = 1000, pilot_SNRdB = None):
BER_pilot = list()
Nt, Nr, Nx = param_NtNrNx
for SNRdB in SNRdB_l:
ber = MIMO().run_pilot( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, disp = True)
BER_pilot.append( ber)
# print( "List of average BERs =", BER_pilot)
return BER_pilot
def get_BER_pilot_ch( SNRdB_l = [5,6,7], param_NtNrNx = (2,4,100), Nloop = 1000, pilot_SNRdB = None, alpha = 0):
"""
Ridge regression will be using to estimate channel.
If alpha is zero, linear regression will be applied.
If alpha is more than zero, Ridge regression will be applied.
The default value of alpha is zero.
"""
BER_pilot = list()
Nt, Nr, Nx = param_NtNrNx
if alpha > 0:
"""
LinearRegression is using.
"""
for SNRdB in SNRdB_l:
ber = MIMO().run_pilot_ch( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, alpha = alpha, disp = True)
BER_pilot.append( ber)
else:
"""
Ridge is using.
"""
for SNRdB in SNRdB_l:
ber = MIMO().run_pilot_channel( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, disp = True)
BER_pilot.append( ber)
# print( "List of average BERs =", BER_pilot)
return BER_pilot
def get_BER_pilot_ch_model(
SNRdB_l = [5,6,7],
param_NtNrNx = (2,4,100),
Nloop = 1000,
pilot_SNRdB = None,
alpha = 0,
model = "Ridge"):
"""
Ridge regression will be using to estimate channel.
If alpha is zero, linear regression will be applied.
If alpha is more than zero, Ridge regression will be applied.
The default value of alpha is zero.
"""
BER_pilot = list()
Nt, Nr, Nx = param_NtNrNx
if alpha > 0:
"""
Ridge or Lasso is used.
"""
for SNRdB in SNRdB_l:
ber = MIMO( model = model).run_pilot_ch_model( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, alpha = alpha, disp = True)
BER_pilot.append( ber)
else:
"""
LinearRegression is used.
"""
for SNRdB in SNRdB_l:
ber = MIMO().run_pilot_channel( pilot_SNRdB = pilot_SNRdB,
param_NtNrNxSNRdB =(Nt, Nr, Nx, SNRdB), Nloop = Nloop, disp = True)
BER_pilot.append( ber)
# print( "List of average BERs =", BER_pilot)
return BER_pilot
def pd_gen_4_snr_pilot(Method, BER_l, alpha = None, Npilot = 10,
sim_task = "Fixed SNRpilot", pilot_SNRdB = 7,
param_NtNrNx = (2,10,100), SNRdB_l = range(-5, 5, 5)):
"""
This is a generalized pd_gen() which can be used for both
fixed_snr_pilot() and snr_snr_pilot().
"""
pdi = pd.DataFrame()
pdi["Simulation task"] = [ sim_task] * len( BER_l)
pdi["Method"] = [ Method] * len( BER_l)
if type(pilot_SNRdB) is list:
pdi["SNRpilot"] = pilot_SNRdB
else:
pdi["SNRpilot"] = [pilot_SNRdB] * len( BER_l)
pdi["#pilots"] = [Npilot] * len( BER_l)
pdi["Nt,Nr,Nx"] = [param_NtNrNx] * len( BER_l)
if alpha is None:
pdi["alpha"] = ["Not defined"] * len( BER_l)
else:
pdi["alpha"] = [alpha] * len( BER_l)
pdi["SNR"] = SNRdB_l
pdi["BER"] = BER_l
return pdi
def fixed_snr_pilot( SNRdB_l = range(-5, 5, 1), param_NtNrNx = (2,10,100), pilot_SNRdB = 7,
alpha_l = [0.01, 0.1, 1, 10, 100], Nloop = 5000):
"""
Simulate BER for fixed SNRpilot cases
the results will be saved to pandas dataframe.
The basic parameters are given from the input argements.
"""
def pd_gen(Method, BER_l, alpha = None, Npilot = 10):
"""
This is a meta-function of pd_gen_4_snr_pilot()
"""
return pd_gen_4_snr_pilot( Method = Method, BER_l = BER_l, Npilot = Npilot, alpha = alpha,
sim_task = "Fixed SNRpilot", pilot_SNRdB = pilot_SNRdB,
param_NtNrNx = param_NtNrNx, SNRdB_l = SNRdB_l)
pdi_l = list()
BER_l = get_BER( SNRdB_l, param_NtNrNx = param_NtNrNx, Nloop = Nloop, pilot_SNRdB = None)
pdi_l.append( pd_gen( "Ideal, ZF Rx", BER_l))
BER_l = get_BER_pilot_ch( SNRdB_l, param_NtNrNx = param_NtNrNx, Nloop = Nloop, pilot_SNRdB = pilot_SNRdB)
pdi_l.append( pd_gen( r"Pilot, $\alpha$=0 (MMSE)", BER_l, alpha = 0))
for alpha in alpha_l:
BER_l = get_BER_pilot_ch( SNRdB_l, param_NtNrNx = param_NtNrNx, Nloop = Nloop,
pilot_SNRdB = pilot_SNRdB, alpha = alpha)
pdi_l.append( pd_gen( r"Pilot, $\alpha$={}".format(alpha),BER_l, alpha))
pdo = pd.concat( pdi_l, ignore_index = True)
return pdo
def snr_snr_pilot( SNRdB_l = range(-5, 5, 1), param_NtNrNx = (2,10,100),
alpha_l = [0.01, 0.1, 1, 10, 100], Npilot = 15, Nloop = 5000):
"""
Simulate BER for fixed SNRpilot cases
the results will be saved to pandas dataframe.
The basic parameters are given from the input argements.
"""
def pd_gen(Method, BER_l, alpha = None):
"""
This is a meta-function of pd_gen_4_snr_pilot()
"""