-
Notifications
You must be signed in to change notification settings - Fork 7
/
B-MMSE.py
92 lines (80 loc) · 3.31 KB
/
B-MMSE.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
"""
@author: Hasan Albinsaid
@site: https://github.com/hasanabs
"""
import os
import time
import numpy as np
import fungsi_dnn as fn
import matplotlib.pyplot as plt
#########################################################################################
####################-Change specification inside this area-##############################
#Spec GSM
Nt, Np, Nr, M = 4, 2, 2, 4 #Transmit_antena, Active_antena, Receive_antena, Constelation(2,4,16)
Ns=100000 #Number of transmit time slot
SNR_Min,step,SNR_Max=0,4,24
####################-Change specification inside this area-##############################
#########################################################################################
#Basic Properties of SM
L1= np.int(np.floor(np.log2(fn.nck(Nt,Np))))
L2=np.int(Np*np.log2(M))
L=L1+L2
N= np.int(np.power(2,L1))
Transmit_bit=L*Ns
Es=Np
modulation= fn.modulation(M)
#TAC Initialization
prob_TAC=fn.nchoosek(np.arange(1,Nt+1),Np)
tac=fn.optimum_TAC(prob_TAC,Nt,Np,N)
#Main Program
print("Nt."+str(Nt)+" Np."+str(Np)+" Nr."+str(Nr)+" M."+str(M))
start = time.time()
Range=np.arange(SNR_Min,SNR_Max+1,step);
Error_bit=np.zeros((len(Range), 1), dtype=np.float16)
noise=np.zeros((Nr, SNR_Max+1), dtype=np.float16)
index_error=0
predict_stop=0
for SNR_dB in Range:
Err_acumulation=0;
sigma_sq=Es/pow(10,(SNR_dB)/10);
#Sending data
for send in range(Ns):
data=np.random.randint(2, size=L)
x=fn.encode(data,Nt,Np,M,L1,tac,modulation)
H=fn.H(Nr, Nt)
noise=fn.noise(SNR_dB, Nr, Es)
y=np.matmul(H,x)+noise
#B-MMSE Detector
predict_start = time.time()
distance=[]
s=[]
for i in range(N):
sj=np.matmul(np.matmul(np.linalg.inv(np.matmul(fn.herm(H[:,tac[i]-1]),H[:,tac[i]-1])+sigma_sq*np.eye(Np)),fn.herm(H[:,tac[i]-1])),y)
s.append(modulation[np.argmin(np.abs(np.subtract(sj,modulation)),axis=1)])
distance.append(np.sum(np.abs(y[:,0]-np.matmul(H[:,tac[i]-1],s[i]))))
demod=fn.de2bi(np.argmin(distance),L1) #Which antenna in bits repr
for i in range(Np):
demod=np.append(demod,fn.de2bi(np.where(modulation==s[np.argmin(distance)][i])[0],int(np.log2(M))))
predict_stop = predict_stop + time.time()-predict_start
#B-MMSE Detector
Err_acumulation+=(data!=demod).sum()
Error_bit[index_error,0]=Err_acumulation/Transmit_bit
print("SNR="+str(SNR_dB)+" n_Error="+str(Err_acumulation)+" transmited="+str(Transmit_bit)+" BER="+str(Error_bit[index_error,0]))
index_error=index_error+1
#Plot
plt.plot(Range, Error_bit, 'mv-', linewidth=1, label="B-MMSE")
plt.legend(loc='upper right', fontsize='x-large')
plt.axis([SNR_Min, SNR_Max, np.min(Error_bit[np.nonzero(Error_bit)]), 1e-0])
plt.xscale('linear')
plt.yscale('log')
plt.xlabel('SNR (dB)')
plt.ylabel('BER')
plt.minorticks_on()
plt.grid(b=True, which='major')
plt.grid(b=True, which='minor',alpha=0.4)
plt.suptitle('GSM-MIMO', fontsize='x-large', fontweight='bold')
plt.title('Nt='+str(Nt)+' Np='+str(Np)+' Nr='+str(Nr)+' M='+str(M)+' Ns='+str(Ns), fontsize='large', fontweight='book')
plt.show()
if not os.path.exists('results'): os.makedirs('results')
plt.savefig('results/B-MMSE_Nt.'+str(Nt)+'_Np.'+str(Np)+'_Nr.'+str(Nr)+'_M.'+str(M)+'_Ns.'+str(Ns)+'.png')
print ("Time Complexity: ", predict_stop, "seconds.")