Num_Bit = 3
BER = []
BitPerNoiseDB_iter = range(0,15)
for BitPerNoiseDB in BitPerNoiseDB_iter:
SymbolPerNoiseRATIO = Num_Bit*10**(BitPerNoiseDB/10)
AWGN_sigma = math.sqrt(1/(2*SymbolPerNoiseRATIO))
Error_Count = 0
Total_Error_Count = 0
Bit_Count = 0
while Total_Error_Count < 100:
Information_Generator = np.empty(Num_Bit, dtype='int')
Bit = np.empty(Num_Bit, dtype='int')
for generator in range(0, Num_Bit):
Information_Generator[generator] = randrange(2)
Detected_Signal = np.empty(Num_Bit, dtype='int')
Detected_Array = np.empty(Num_Bit, dtype='int')
Bit = Information_Generator
if (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 0):
symbol = complex(math.cos(PI/8), math.sin(PI/8))
elif (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 1):
symbol = complex(math.cos(3*PI/8), math.sin(3*PI/8))
elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 1):
symbol = complex(math.cos(5*PI/8), math.sin(5*PI/8))
elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 0):
symbol = complex(math.cos(7*PI/8), math.sin(7*PI/8))
elif (Bit[0] == 1 and Bit[1] == 1 and Bit[2] == 0):
symbol = complex(math.cos(-7*PI/8), math.sin(-7*PI/8))
elif (Bit[0] == 1 and Bit[1] == 1 and Bit[2] == 1):
symbol = complex(math.cos(-5*PI/8), math.sin(-5*PI/8))
elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 1):
symbol = complex(math.cos(-3*PI/8), math.sin(-3*PI/8))
elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 0):
symbol = complex(math.cos(-1*PI/8), math.sin(-1*PI/8))
AWGN = np.random.normal(size = (1,2))
AWGN = complex(AWGN[0][0], AWGN[0][1]) * AWGN_sigma
Received_Signal = symbol + AWGN
Received_Signal_Phase = math.atan2(Received_Signal.imag, Received_Signal.real)
if (Received_Signal_Phase >= 0) and (Received_Signal_Phase < PI/4):
Detected_Array = 0, 0, 0
elif (Received_Signal_Phase >= PI/4) and (Received_Signal_Phase < PI/2):
Detected_Array = 0, 0, 1
elif (Received_Signal_Phase >= PI/2) and (Received_Signal_Phase < 3*PI/4):
Detected_Array = 0, 1, 1
elif (Received_Signal_Phase >= 3*PI/4) and (Received_Signal_Phase < PI):
Detected_Array = 0, 1, 0
elif (Received_Signal_Phase >=- 1* PI) and (Received_Signal_Phase < -3*PI/4):
Detected_Array = 1, 1, 0
elif (Received_Signal_Phase >= -3*PI/4) and (Received_Signal_Phase < -PI/2):
Detected_Array = 1, 1, 1
elif (Received_Signal_Phase >= -PI/2) and (Received_Signal_Phase < -PI/4):
Detected_Array = 1, 0, 1
else:
Detected_Array = 1, 0, 0
Error_Count = 0
for index in range(0, Num_Bit):
if Bit[index] != Detected_Array[index]:
Error_Count += 1
Total_Error_Count += Error_Count
Bit_Count += Num_Bit
BER.append(Total_Error_Count/Bit_Count)
print("Bit_Count :", Bit_Count)
print("Eb/N0 :{} [dB]".format(BitPerNoiseDB))
print("The number of bits with error :", Total_Error_Count)
print("BER :", BER[BitPerNoiseDB] )
print("---------------------------------")