-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_gen.py
171 lines (127 loc) · 4.69 KB
/
conv_gen.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
import sionna as sn
from commpy.channels import awgn
from commpy.modulation import PSKModem
import pandas as pd
import random
binary_source = sn.utils.BinarySource()
psk = PSKModem(4)
snr = 10
# lengths = 1024, 1536, 2048
# rates = 1/2, 1/3
# ---- rate=1/2, length=1024 ----
#r = 1/2
#n = 1024
#k = int(r * n)
#enc = sn.fec.conv.ConvEncoder(constraint_length=7,
# rate=r)
#msg = binary_source([17000, k])
#c_12_1024 = enc(msg).numpy().astype(int).tolist()
#print("[info] code rate 1/2 and length 1024 done")
# ---- rate=1/3, length=1024 ----
#r = 1/3
#n = 1024
#k = int(r * n)
#enc = sn.fec.conv.ConvEncoder(constraint_length=7,
# rate=r)
#msg = binary_source([17000, k])
#c_13_1024 = enc(msg).numpy().astype(int).tolist()
#print("[info] code rate 1/3 and length 1024 done")
# ---- rate=1/2, length=1536 ----
#r = 1/2
#n = 1536
#k = int(r * n)
#enc = sn.fec.conv.ConvEncoder(constraint_length=7,
# rate=r)
#msg = binary_source([17000, k])
#c_12_1536 = enc(msg).numpy().astype(int).tolist()
#print("[info] code rate 1/2 and length 1536 done")
# ---- rate=1/3, length=1536 ----
#r = 1/3
#n = 1536
#k = int(r * n)
#enc = sn.fec.conv.ConvEncoder(constraint_length=7,
# rate=r)
#msg = binary_source([17000, k])
#c_13_1536 = enc(msg).numpy().astype(int).tolist()
#print("[info] code rate 1/3 and length 1536 done")
# ---- rate=1/2, length=2048 ----
r = 1/2
n = 2048
k = int(r * n)
enc = sn.fec.conv.ConvEncoder(constraint_length=7,
rate=r)
msg = binary_source([40000, k])
c_12_2048 = enc(msg).numpy().astype(int).tolist()
print("[info] code rate 1/2 and length 2048 done")
# ---- rate=1/2, length=1536 ----
r = 1/3
n = 2048
k = int(r * n)
enc = sn.fec.conv.ConvEncoder(constraint_length=7,
rate=r)
msg = binary_source([40000, k])
c_13_2048 = enc(msg).numpy().astype(int).tolist()
print("[info] code rate 1/3 and length 2048 done")
# ---- Modulation, AWGN and Demodulation utility funcs ----
def psk_modulation(bit_string):
"""
Takes the shift value and returns PSK modulation
"""
return psk.modulate(bit_string)
def psk_demodulation(noisy_signal):
"""
Performs PSK demodulation on the received signal
"""
return psk.demodulate(noisy_signal, demod_type='hard')
def apply_awgn(modulated_signal, snr, coderate):
"""
Adds Additive White Gaussian Noise on the signals
"""
return awgn(modulated_signal, snr, coderate)
def bin_string(bin_array):
"""
Converts a binary array into a binary string
"""
return ''.join(str(bit) for bit in bin_array)
# ---- modulation ----
#c_12_1024_mod = [psk_modulation(x) for x in c_12_1024]
#c_13_1024_mod = [psk_modulation(x) for x in c_13_1024]
#c_12_1536_mod = [psk_modulation(x) for x in c_12_1536]
#c_13_1536_mod = [psk_modulation(x) for x in c_13_1536]
c_12_2048_mod = [psk_modulation(x) for x in c_12_2048]
c_13_2048_mod = [psk_modulation(x) for x in c_13_2048]
print("[info] modulation completed")
# ---- awgn addition ----
#c_12_1024_noisy = [apply_awgn(x, snr, 1/2) for x in c_12_1024_mod]
#c_13_1024_noisy = [apply_awgn(x, snr, 1/3) for x in c_12_1024_mod]
#c_12_1536_noisy = [apply_awgn(x, snr, 1/2) for x in c_12_1536_mod]
#c_13_1536_noisy = [apply_awgn(x, snr, 1/3) for x in c_13_1536_mod]
c_12_2048_noisy = [apply_awgn(x, snr, 1/2) for x in c_12_2048_mod]
c_13_2048_noisy = [apply_awgn(x, snr, 1/3) for x in c_13_2048_mod]
print("[info] awgn completed")
# ---- demodulation ----
#c_12_1024_demod = [psk_demodulation(x) for x in c_12_1024_noisy]
#c_13_1024_demod = [psk_demodulation(x) for x in c_13_1024_noisy]
#c_12_1536_demod = [psk_demodulation(x) for x in c_12_1536_noisy]
#c_13_1536_demod = [psk_demodulation(x) for x in c_13_1536_noisy]
c_12_2048_demod = [psk_demodulation(x) for x in c_12_2048_noisy]
c_13_2048_demod = [psk_demodulation(x) for x in c_13_2048_noisy]
print("[info] demodulation completed")
#c_12_1024_str = [bin_string(x) for x in c_12_1024_demod]
#c_13_1024_str = [bin_string(x) for x in c_13_1024_demod]
#c_12_1536_str = [bin_string(x) for x in c_12_1536_demod]
#c_13_1536_str = [bin_string(x) for x in c_13_1536_demod]
c_12_2048_str = [bin_string(x) for x in c_12_2048_demod]
c_13_2048_str = [bin_string(x) for x in c_13_2048_demod]
print("[info] string conversion done")
encoded = c_12_2048_str + c_13_2048_str
labels = [2 for _ in range(80000)]
random.shuffle(encoded)
print("[info] data has been shuffled")
data = {
'encoded_data_string': encoded,
'encoding': labels
}
df = pd.DataFrame(data, index=None)
df.to_csv("convolutional_2048_variable_rates_4psk_80000.csv", header=True, index=False)
print("[info] file is written")