-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSimulation.py
777 lines (703 loc) · 34 KB
/
Simulation.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
import numpy as np
import datetime
import BP_SP_AWGN_Decoder
import BP_MS_AWGN_Decoder
import BP_SP_BSC_Decoder
import BP_MS_BSC_Decoder
import tensorflow as tf
import Modulation
import Transmission
import DataIO
import Quantized_ML_Decoder
import Adaptive_BP_QMS_Decoder
def LDPC_BP_SP_AWGN_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
## load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
SNR_set = top_config.SNR_set
BP_iter_num = top_config.BP_iter_nums
function = 'LDPC_BP_SP_AWGN_test'
# build BP decoding network
bp_decoder = BP_SP_AWGN_Decoder.BP_NetDecoder(H_matrix, batch_size)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
## initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s' % (ber_file, function))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
# simulation starts
start = datetime.datetime.now()
for SNR in SNR_set:
y_recieve_file = format('%s_%.1f.dat' % (top_config.decoding_y_file, SNR))
x_transmit_file = format('%s_%.1f.dat' % (top_config.decoding_x_file, SNR))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
#encode and transmisssion
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = y_receive - s_mod
ch_noise_sigma = np.sqrt(1 / np.power(10, SNR / 10.0) / 2.0)
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise_sigma, ch_noise = channel.channel_transmit(batch_size, s_mod, SNR)
# ch_noise_sigma = np.sqrt(1 / np.power(10, SNR / 10.0) / 2.0)
LLR = y_receive * 2.0 / (ch_noise_sigma * ch_noise_sigma)
##practical noise
noise_power = np.mean(np.square(ch_noise))
practical_snr = 10*np.log10(1 / (noise_power * 2.0))
print('Practical EbN0: %.2f' % practical_snr)
#BP decoder
u_BP_decoded = bp_decoder.decode(LLR.astype(np.float32), BP_iter_num[0])
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
# frame_errs_iter[0] += np.sum(np.sign(np.sum(output_x != x_bits, axis=1)))
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
#ber
fout_ber.write(str(SNR) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def LDPC_BP_SP_BSC_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
## load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
crossover_prob_set = top_config.crossover_prob_set
BP_iter_num = top_config.BP_iter_nums
function = 'LDPC_BP_SP_BSC_test'
# build BP decoding network
bp_decoder = BP_SP_BSC_Decoder.BP_NetDecoder(H_matrix, batch_size)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
## initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s' % (ber_file, function))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for crossover_prob in crossover_prob_set:
y_recieve_file = format('%s_%.3f.dat' % (top_config.decoding_y_file, crossover_prob))
x_transmit_file = format('%s_%.3f.dat' % (top_config.decoding_x_file, crossover_prob))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
# encode and transmisssion
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise = channel.channel_transmit(batch_size, s_mod, crossover_prob)
# LLR = y_receive * (np.log(1-crossover_prob)-np.log(crossover_prob))
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = np.multiply(y_receive,s_mod)
# ch_noise = y_receive
LLR = y_receive * (np.log(1-crossover_prob)-np.log(crossover_prob))
##practical noise
practical_crossover_prob = np.mean((1-ch_noise)/2)
print('Practical Crossover Probability: %.3f' % practical_crossover_prob)
#BP decoder
u_BP_decoded = bp_decoder.decode(LLR.astype(np.float32), BP_iter_num[0])
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
#ber
fout_ber.write(str(crossover_prob) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def LDPC_BP_MS_AWGN_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
## load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
SNR_set = top_config.SNR_set
BP_iter_num = top_config.BP_iter_nums
alpha = top_config.alpha
beta = top_config.beta
function = 'LDPC_BP_MS_AWGN_test'
# build BP decoding network
bp_decoder = BP_MS_AWGN_Decoder.BP_NetDecoder(H_matrix, batch_size, alpha, beta)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
## initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s' % (ber_file, function))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for SNR in SNR_set:
y_recieve_file = format('%s_%.1f.dat' % (top_config.decoding_y_file, SNR))
x_transmit_file = format('%s_%.1f.dat' % (top_config.decoding_x_file, SNR))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
#encode and transmisssion
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = y_receive - s_mod
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise_sigma, ch_noise = channel.channel_transmit(batch_size, s_mod, SNR)
LLR = y_receive
##practical noise
noise_power = np.mean(np.square(ch_noise))
practical_snr = 10*np.log10(1 / (noise_power * 2.0))
print('Practical EbN0: %.2f' % practical_snr)
#BP decoder
u_BP_decoded = bp_decoder.decode(LLR.astype(np.float32), BP_iter_num[0])
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
fout_ber.write(str(SNR) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def LDPC_BP_MS_BSC_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
## load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
crossover_prob_set = top_config.crossover_prob_set
BP_iter_num = top_config.BP_iter_nums
alpha = top_config.alpha
beta = top_config.beta
function = 'LDPC_BP_MS_BSC_test'
# build BP decoding network
bp_decoder = BP_MS_BSC_Decoder.BP_NetDecoder(H_matrix, batch_size, alpha, beta)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
## initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s' % (ber_file, function))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for crossover_prob in crossover_prob_set:
y_recieve_file = format('%s_%.3f.dat' % (top_config.decoding_y_file, crossover_prob))
x_transmit_file = format('%s_%.3f.dat' % (top_config.decoding_x_file, crossover_prob))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
# encode and transmisssion
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise = channel.channel_transmit(batch_size, s_mod, crossover_prob)
# LLR = y_receive
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = np.multiply(y_receive,s_mod)
LLR = y_receive
##practical noise
practical_crossover_prob = np.mean((1-ch_noise)/2)
print('Practical Crossover Probability: %.3f' % practical_crossover_prob)
#BP decoder
u_BP_decoded = bp_decoder.decode(LLR.astype(np.float32), BP_iter_num[0])
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
fout_ber.write(str(crossover_prob) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def Neural_BP_QMS_AWGN_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size, bp_decoder):
# load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
SNR_set = train_config.SNR_set_for_test
BP_iter_num = train_config.BP_iter_nums
alpha = train_config.alpha
beta = train_config.beta
function = 'Neural_BP_QMS_AWGN_test'
# build BP decoding network
# bp_decoder = Quantized_ML_Decoder.BP_NetDecoder(H_matrix, batch_size)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
# initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s_%.1f_%.1f' % (ber_file, function, alpha[0], beta[0]))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for SNR in SNR_set:
y_recieve_file = format('%s_%.1f.dat' % (top_config.decoding_y_file, SNR))
x_transmit_file = format('%s_%.1f.dat' % (top_config.decoding_x_file, SNR))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
#encode and transmisssion
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = y_receive - s_mod
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise_sigma, ch_noise = channel.channel_transmit(batch_size, s_mod, SNR)
LLR = y_receive
##practical noise
noise_power = np.mean(np.square(ch_noise))
practical_snr = 10*np.log10(1 / (noise_power * 2.0))
print('Practical EbN0: %.2f' % practical_snr)
#BP decoder
u_BP_decoded = bp_decoder.quantized_decode(LLR.astype(np.float32), BP_iter_num[0], alpha, beta)
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
#ber
fout_ber.write(str(SNR) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def Neural_BP_QMS_BSC_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
## load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
crossover_prob_set = train_config.crossover_prob_set_for_test
BP_iter_num = train_config.BP_iter_nums
alpha = train_config.alpha
beta = train_config.beta
function = 'Neural_BP_QMS_BSC_test'
# build BP decoding network
bp_decoder = Quantized_ML_Decoder.BP_NetDecoder(H_matrix, batch_size)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
## initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s_%.1f_%.1f' % (ber_file, function, alpha[0], beta[0]))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for crossover_prob in crossover_prob_set:
y_recieve_file = format('%s_%.3f.dat' % (top_config.decoding_y_file, crossover_prob))
x_transmit_file = format('%s_%.3f.dat' % (top_config.decoding_x_file, crossover_prob))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
# encode and transmisssion
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise = channel.channel_transmit(batch_size, s_mod, crossover_prob)
# LLR = y_receive
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = np.multiply(y_receive,s_mod)
LLR = y_receive
##practical noise
practical_crossover_prob = np.mean((1-ch_noise)/2)
print('Practical Crossover Probability: %.3f' % practical_crossover_prob)
#BP decoder
u_BP_decoded = bp_decoder.quantized_decode(LLR.astype(np.float32), BP_iter_num[0], alpha, beta)
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
#ber
fout_ber.write(str(crossover_prob) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def Adaptive_BP_QMS_AWGN_test(code, channel, top_config, train_config, simutimes_range, target_err_bits_num, batch_size):
# load configurations from top_config
N = top_config.N_code
K = top_config.K_code
H_matrix = code.H_matrix
beta_set = top_config.beta_set
syndrome_weight_determining = top_config.syndrome_weight_determining
SNR_set = top_config.SNR_set
BP_iter_num = top_config.BP_iter_nums
function = 'Adaptive_BP_QMS_AWGN_test'
# build BP decoding network
bp_decoder = Adaptive_BP_QMS_Decoder.BP_NetDecoder(H_matrix, batch_size, beta_set, syndrome_weight_determining)
# init gragh
init = tf.global_variables_initializer()
sess = tf.Session()
print('Open a tf session!')
sess.run(init)
# initialize simulation times
max_simutimes = simutimes_range[1]
min_simutimes = simutimes_range[0]
max_batches, residual_times = np.array(divmod(max_simutimes, batch_size), np.int32)
if residual_times!=0:
max_batches += 1
## generate out ber file
bp_str = np.array2string(BP_iter_num, separator='_', formatter={'int': lambda d: "%d" % d})
bp_str = bp_str[1:(len(bp_str) - 1)]
ber_file = format('%sBER(%d_%d)_BP(%s)' % (top_config.results_folder, N, K, bp_str))
ber_file = format('%s_%s_%.1f_%.1f_%.1f' % (ber_file, function, beta_set[0,0], beta_set[0,1], beta_set[0,2]))
ber_file = format('%s.txt' % ber_file)
fout_ber = open(ber_file, 'wt')
## simulation starts
start = datetime.datetime.now()
for SNR in SNR_set:
y_recieve_file = format('%s_%.1f.dat' % (top_config.decoding_y_file, SNR))
x_transmit_file = format('%s_%.1f.dat' % (top_config.decoding_x_file, SNR))
dataio_decode = DataIO.BPdecDataIO(y_recieve_file, x_transmit_file, top_config)
real_batch_size = batch_size
# simulation part
actual_simutimes = 0
bit_errs_iter = np.zeros(1, dtype=np.int32)
for ik in range(0, max_batches):
print('Batch %d in total %d batches.' % (ik, int(max_batches)), end=' ')
if ik == max_batches - 1 and residual_times != 0:
real_batch_size = residual_times
#encode and transmisssion
y_receive, x_bits = dataio_decode.load_next_batch(batch_size, ik)
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
ch_noise = y_receive - s_mod
# x_bits = np.random.randint(0, 2, size=(batch_size, K))
# u_coded_bits = code.encode_LDPC(x_bits)
# s_mod = Modulation.BPSK(u_coded_bits)
# y_receive, ch_noise_sigma, ch_noise = channel.channel_transmit(batch_size, s_mod, SNR)
LLR = y_receive
##practical noise
noise_power = np.mean(np.square(ch_noise))
practical_snr = 10*np.log10(1 / (noise_power * 2.0))
print('Practical EbN0: %.2f' % practical_snr)
#BP decoder
u_BP_decoded = bp_decoder.quantized_decode(LLR.astype(np.float32), BP_iter_num[0])
#BER
output_x = code.dec_src_bits(u_BP_decoded)
bit_errs_iter[0] += np.sum(output_x != x_bits)
actual_simutimes += real_batch_size
if bit_errs_iter[0] >= target_err_bits_num and actual_simutimes >= min_simutimes:
break
print('%d bits are simulated!' % (actual_simutimes * K))
# load to files
ber_iter = np.zeros(1, dtype=np.float64)
#ber
fout_ber.write(str(SNR) + '\t')
ber_iter[0] = bit_errs_iter[0] / float(K * actual_simutimes)
fout_ber.write(str(ber_iter[0]))
fout_ber.write('\n')
#simulation finished
fout_ber.close()
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
sess.close()
print('Close the tf session!')
def softsign(x_in):
x_temp = x_in/(np.abs(x_in) + 0.01)
y_out = np.divide(1-x_temp, 2)
return y_out
def sigmoid(x_in):
y_out = 1/(1+np.exp(-x_in))
return y_out
def Generate_AWGN_Training_Data(code, channel, top_config, train_config, generate_data_for):
#initialized
SNR_set = train_config.SNR_set
if generate_data_for == 'Training':
batch_size_each_SNR = int(train_config.training_minibatch_size // np.size(train_config.SNR_set))
total_batches = int(train_config.training_sample_num // train_config.training_minibatch_size)
elif generate_data_for == 'Test':
batch_size_each_SNR = int(train_config.test_minibatch_size // np.size(train_config.SNR_set))
total_batches = int(train_config.test_sample_num // train_config.test_minibatch_size)
else:
print('Invalid objective of data generation!')
exit(0)
## Data generating starts
start = datetime.datetime.now()
if generate_data_for == 'Training':
fout_feature = open(train_config.training_feature_file, 'wb')
fout_label = open(train_config.training_label_file, 'wb')
elif generate_data_for == 'Test':
fout_feature = open(train_config.test_feature_file, 'wb')
fout_label = open(train_config.test_label_file, 'wb')
for ik in range(0, total_batches):
for SNR in SNR_set:
x_bits, u_coded_bits, s_mod, ch_noise, y_receive = Transmission.AWGN_transmission(SNR, batch_size_each_SNR, top_config, code, channel)
y_receive = y_receive.astype(np.float32)
y_receive.tofile(fout_feature) # write features to file
x_bits = x_bits.astype(np.float32)
x_bits.tofile(fout_label)
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
def Generate_BSC_Training_Data(code, channel, top_config, train_config, generate_data_for):
#initialized
crossover_prob_set = train_config.crossover_prob_set
if generate_data_for == 'Training':
batch_size_each_crossover_prob = int(train_config.training_minibatch_size // np.size(train_config.crossover_prob_set))
total_batches = int(train_config.training_sample_num // train_config.training_minibatch_size)
elif generate_data_for == 'Test':
batch_size_each_crossover_prob = int(train_config.test_minibatch_size // np.size(train_config.crossover_prob_set))
total_batches = int(train_config.test_sample_num // train_config.test_minibatch_size)
else:
print('Invalid objective of data generation!')
exit(0)
## Data generating starts
start = datetime.datetime.now()
if generate_data_for == 'Training':
fout_feature = open(train_config.training_feature_file, 'wb')
fout_label = open(train_config.training_label_file, 'wb')
elif generate_data_for == 'Test':
fout_feature = open(train_config.test_feature_file, 'wb')
fout_label = open(train_config.test_label_file, 'wb')
for ik in range(0, total_batches):
for crossover_prob in crossover_prob_set:
x_bits, u_coded_bits, s_mod, ch_noise, y_receive = Transmission.BSC_transmission(crossover_prob, batch_size_each_crossover_prob, top_config, code, channel)
y_receive = y_receive.astype(np.float32)
y_receive.tofile(fout_feature) # write features to file
x_bits = x_bits.astype(np.float32)
x_bits.tofile(fout_label)
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
def Generate_AWGN_Decoding_Data(top_config, code):
#initialized
SNR_set = top_config.SNR_set
total_samples = top_config.total_samples
batch_size = 5000
K = top_config.K_code
N = top_config.N_code
rng = np.random.RandomState(None)
total_batches = int(total_samples // (batch_size*K))
## Data generating starts
start = datetime.datetime.now()
for SNR in SNR_set:
y_recieve_file = format('%s_%.1f.dat' % (top_config.decoding_y_file, SNR))
x_transmit_file = format('%s_%.1f.dat' % (top_config.decoding_x_file, SNR))
fout_yrecieve = open(y_recieve_file, 'wb')
fout_xtransmit = open(x_transmit_file, 'wb')
for ik in range(0, total_batches):
x_bits = np.random.randint(0, 2, size=(batch_size, K))
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
noise_awgn = rng.randn(batch_size, N)
ch_noise_normalize = noise_awgn.astype(np.float32)
ch_noise_sigma = np.sqrt(1 / np.power(10, SNR / 10.0) / 2.0)
ch_noise = ch_noise_normalize * ch_noise_sigma
y_receive = s_mod + ch_noise
y_receive = y_receive.astype(np.float32)
y_receive.tofile(fout_yrecieve)
x_bits = x_bits.astype(np.float32)
x_bits.tofile(fout_xtransmit)
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")
def Generate_BSC_Decoding_Data(top_config, code):
#initialized
crossover_prob_set = top_config.crossover_prob_set
total_samples = top_config.total_samples
batch_size = 5000
K = top_config.K_code
N = top_config.N_code
total_batches = int(total_samples // (batch_size*K))
## Data generating starts
start = datetime.datetime.now()
for crossover_prob in crossover_prob_set:
y_recieve_file = format('%s_%.3f.dat' % (top_config.decoding_y_file, crossover_prob))
x_transmit_file = format('%s_%.3f.dat' % (top_config.decoding_x_file, crossover_prob))
fout_yrecieve = open(y_recieve_file, 'wb')
fout_xtransmit = open(x_transmit_file, 'wb')
for ik in range(0, total_batches):
x_bits = np.random.randint(0, 2, size=(batch_size, K))
u_coded_bits = code.encode_LDPC(x_bits)
s_mod = Modulation.BPSK(u_coded_bits)
noise_bsc = np.sign(np.random.random(size=(batch_size, N))-crossover_prob)
y_receive = np.multiply(s_mod,noise_bsc)
y_receive = y_receive.astype(np.float32)
y_receive.tofile(fout_yrecieve) # write features to file
x_bits = x_bits.astype(np.float32)
x_bits.tofile(fout_xtransmit)
end = datetime.datetime.now()
print('Time: %ds' % (end-start).seconds)
print("end\n")