-
Notifications
You must be signed in to change notification settings - Fork 0
/
LBC.py
199 lines (157 loc) · 6.51 KB
/
LBC.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 13:09:12 2020
@author: user
"""
import numpy as np
from itertools import combinations
from operator import add
import math
import matplotlib.pyplot as plt
#-----------------------------------------------------------------------------
# Varivables
#-----------------------------------------------------------------------------
size= 100
G_1= [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1]]
#G_1 = np.array(G_1).reshape((-1, 14)).tolist()
#s_1= BPSK(bitgenerator(size))
#s_1 = [1, 0, 0, 1, 0, 1, 1, 1]
#-----------------------------------------------------------------------------
# Bit generator
#-----------------------------------------------------------------------------
def bitgenerator(size):
return [np.random.randint(2) for i in range(size)]
#-----------------------------------------------------------------------------
# BPSK generator
#-----------------------------------------------------------------------------
def BPSK(bits):
bpsk = []
for k in bits:
if k == 1:
bpsk.append(1)
else:
bpsk.append(-1)
return bpsk
#-----------------------------------------------------------------------------
# Linear block code
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Parity check matrix (H)
#-----------------------------------------------------------------------------
def parityCheck( G):
G=np.array(G)
p= G[0:G.shape[0],G.shape[0]:G.shape[1]]
H= np.concatenate((p.transpose(),np.identity(G.shape[1]-G.shape[0])),1)
return H
#-----------------------------------------------------------------------------
# Encoding
#-----------------------------------------------------------------------------
def codeword(sent,G):#sent is a list , G is a list, n is noise
G=np.array(G)
s=np.array(sent).reshape((-1,G.shape[0]))
codewords= s.dot(G) %2
return codewords.flatten()
#-----------------------------------------------------------------------------
# Decoding
#-----------------------------------------------------------------------------
def decoder(Recievedbits,G): #G is a list and Rec is a list too
G=np.array(G)
c=np.array(Recievedbits).reshape((-1,G.shape[1]))
H=parityCheck(G)
z=c.dot(H.T)%2
for k in range(0,len(c)):
if(sum(z[k]>=1)):
index=[]
for i in H.T: # Search for the syndrom Vec in H.T matrix
if( (i == z[k]).all()):
index.append(1)
else:
index.append(0)
#check if z was found in the H.T matrix
if(sum(index)>0): #found something change cest
c[k] ^= np.array(index) #xor
else: #nothing was found so find combinations
for com in combinations(range(len(H.T)),2):
OR = H.T[com[0]].astype(int)^H.T[com[1]].astype(int)
if(OR==z[k]).all():
c[k][com[0]] ^= 1
c[k][com[1]] ^= 1
break
return c[:,:G.shape[0]].flatten()
#------------------------------------------------------------------------------
def Add_noise(transmitted,RC, SNR):
M=2
Gnoise= np.random.normal(0,size=len(transmitted))
gama = 1 / np.sqrt(math.pow(10, (SNR / 10)) * 2 * RC)
# print(gama)
new = [i * gama for i in Gnoise]
R = list(map(add, transmitted, new))
return R
#k=[1,-1,1,1]
# ____________________________________________________________________________
# Detection
# ____________________________________________________________________________
def BPSKDetection(comp):
points = [-1, 1] #sybols
Bpoints = [[0], [1]]
recieved = -1
minDistance = 99
decoded = []
Bdecoded = []
for y in comp:
if(y>0):
decoded.append(1)
Bdecoded.append(1)
else:
decoded.append(-1)
Bdecoded.append(0)
return decoded, Bdecoded # recieved
#__________________________________________________________________________
# Bit Error calculation
#______________________________________________________________________________
def bit_errors(sent, recieved):
error = 0
for k in range(0,len(recieved)):
if sent[k] != recieved[k]:
error += 1
BER = error / len(recieved)*100
return BER
s_1= bitgenerator(100)
def tester():
xValues = np.linspace(-4, 15, 100)
yvalues=[]
yvalues1=[]
for x in xValues:
ber=0
ber1=0
for i in range(0,10):
s_1= bitgenerator(100)
symbol,bits=BPSKDetection(Add_noise(BPSK(codeword(s_1,G_1).tolist()),0.5,x))
symbol1,bits1=BPSKDetection(Add_noise(BPSK(s_1),1,x))
#bits = [item for sublist in bits for item in sublist]
#print(Add_noise(codeword(s_1,G_1).tolist(),0.5,1000))
#print(bits)
#c_est = [1,0,0,0,0,1,1,1,1,0,1,0,1,0]
ber+= bit_errors(s_1,decoder(bits,G_1).tolist())
ber1+= bit_errors(s_1,bits1)
ber=ber/10
ber1=ber1/10
yvalues.append(ber)
yvalues1.append(ber1)
plt.semilogy(xValues,yvalues, label="Linear Block Code")
plt.ylabel('BER')
plt.xlabel('SNR (dB)')
plt.semilogy(xValues,yvalues1, label="Optimal detection uncoded")
#At the end
plt.title(" BER vs SNR")
plt.legend()
tester()