-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDD_BSC_H3_Bernard_s_Equation.cpp
282 lines (209 loc) · 6.84 KB
/
SDD_BSC_H3_Bernard_s_Equation.cpp
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
#include<bits/stdc++.h>
using namespace std;
// Soft Decision Decoding For BSC Channel (for 9 x 12 H - matrix) using Bernard's Equation
// Degree of CN is 4
struct CN
{
float v[4][2]={{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; // In first column I'll store which number of VN is connected (from 1 to 12) and in second column the values of that VN in message passing from VN to CN
}arr_CN[9];
// Degree of VN is 3
struct VN
{
float c[3][2]={{-1,-1},{-1,-1},{-1,-1}}; // In first column I'll store which number of CN is connected (from 1 to 9) and in second column the values of that CN in message passing from CN to VN
float value; // Store the conditional probability of transmitted signal after adding the noise that the value of corresponding VN is 1
float decision; // After each iteration it makes decision (by taking likelihood ratios) and that conditional probability it sends to CNs
}arr_VN[12];
int main()
{
int N=12,U=9,i,j,c1,c2,c3,v1,v2,v3,v4,flag,count,terminate,Nsim=10000,Ksim,l;
int Ncorr[101]={0},Nerr[101]={0};
float p[101],r,k,alpha,bita,VN_is_1,VN_is_0,VN_decision_1,VN_decision_0,arr[3];
p[0]=0; // Crossover Probability
for(i=1;i<=100;i++)
{
p[i]=p[i-1]+0.01;
}
int H[U][N]= {{1,0,0,0,0,1,0,1,0,1,0,0}, // 9 x 12 H - matrix
{1,0,0,1,1,0,0,0,0,0,1,0},
{0,1,0,0,1,0,1,0,1,0,0,0},
{0,0,1,0,0,1,0,0,0,0,1,1},
{0,0,1,0,0,0,1,1,0,0,0,1},
{0,1,0,0,1,0,0,0,1,0,1,0},
{1,0,0,1,0,0,1,0,0,1,0,0},
{0,1,0,0,0,1,0,1,0,1,0,0},
{0,0,1,1,0,0,0,0,1,0,0,1}};
// Connection of CNs with VNs
for(i=0;i<U;i++)
{
count=0;
for(j=0;j<N;j++)
{
if(H[i][j]==1)
{
arr_CN[i].v[count][0]=j+1;
++count;
}
}
}
// Connection of VNs with CNs
for(j=0;j<N;j++)
{
count=0;
for(i=0;i<U;i++)
{
if(H[i][j]==1)
{
arr_VN[j].c[count][0]=i+1;
++count;
}
}
}
// Tanner Graph Decoding With Monte Carlo Simulations
srand (time(NULL));
for(l=0;l<=100;l++) // Outer loop for crossover probability
{
for(Ksim=1;Ksim<=Nsim;Ksim++) // Loop for Monte - Carlo simulations
{
int tr[N]={0}; // Transmitted Signal
int noise[N]={0}; // Noise ( or received signal contains bits)
float rx[N]={0}; // Received Signal ( contains conditional probabilities of VNi is 1 given received bit
// Loading VNs with conditional probabilities that VN is 1 given received bit
for(i=0;i<N;i++)
{
r=((float) rand() / (RAND_MAX + 1)); // Generation of a random number between 0 to 1
if(r>(1-p[l])) // If r > 1-p then, the bit will be flipped
noise[i]=1;
else
noise[i]=0;
if(noise[i]==1)
rx[i]=1-p[l];
else
rx[i]=p[l];
arr_VN[i].value=rx[i];
for(j=0;j<3;j++)
{
alpha = 1/(rx[i]*rx[i]*rx[i] + (1-rx[i])*(1-rx[i])*(1-rx[i]));
arr_VN[i].c[j][1]=alpha*rx[i]*rx[i]*rx[i];
}
}
// Tanner Graph decoding
terminate=0;
// terminate is used for 100 iterations breaking condition
while(terminate<100)
{
// VN sends massege to CN (first iteration and next iterations)
for(i=0;i<N;i++)
{
c1=arr_VN[i].c[0][0];
c2=arr_VN[i].c[1][0];
c3=arr_VN[i].c[2][0];
for(j=0;j<4;j++)
{
if(arr_CN[c1-1].v[j][0]==i+1)
arr_CN[c1-1].v[j][1]=arr_VN[i].c[0][1];
}
for(j=0;j<4;j++)
{
if(arr_CN[c2-1].v[j][0]==i+1)
arr_CN[c2-1].v[j][1]=arr_VN[i].c[1][1];
}
for(j=0;j<4;j++)
{
if(arr_CN[c3-1].v[j][0]==i+1)
arr_CN[c3-1].v[j][1]=arr_VN[i].c[2][1];
}
}
// CN sends VN
for(i=0;i<U;i++)
{
v1=arr_CN[i].v[0][0];
v2=arr_CN[i].v[1][0];
v3=arr_CN[i].v[2][0];
v4=arr_CN[i].v[3][0];
// CN j sends VN i the conditional probability that VN i is 1 by Bernard's Equation and by using other VNs conditional probabilities
for(j=0;j<3;j++)
{
if(arr_VN[v1-1].c[j][0]==i+1)
{
arr_VN[v1-1].c[j][1] = (0.5-0.5*(1-2*arr_CN[i].v[1][1])*(1-2*arr_CN[i].v[2][1])*(1-2*arr_CN[i].v[3][1]));
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v2-1].c[j][0]==i+1)
{
arr_VN[v2-1].c[j][1] = (0.5-0.5*(1-2*arr_CN[i].v[0][1])*(1-2*arr_CN[i].v[2][1])*(1-2*arr_CN[i].v[3][1]));
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v3-1].c[j][0]==i+1)
{
arr_VN[v3-1].c[j][1] = (0.5-0.5*(1-2*arr_CN[i].v[0][1])*(1-2*arr_CN[i].v[1][1])*(1-2*arr_CN[i].v[3][1]));
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v4-1].c[j][0]==i+1)
{
arr_VN[v4-1].c[j][1] = (0.5-0.5*(1-2*arr_CN[i].v[0][1])*(1-2*arr_CN[i].v[1][1])*(1-2*arr_CN[i].v[2][1]));
break;
}
}
}
// Computing the conditional probability that VN i is 1 for CN j
flag=count=0;
// flag and count variables are used for second breaking condition
// If all VNs value after decision making became 1 or 0 then, after that iteration their values will remain same, so this is the breaking condition
for(i=0;i<N;i++)
{
for(j=0;j<3;j++)
arr[j]=arr_VN[i].c[j][1];
for(j=0;j<3;j++)
{
VN_decision_1=arr_VN[i].value*arr[(j+1)%3]*arr[(j+2)%3];
VN_decision_0=(1-arr_VN[i].value)*(1-arr[(j+1)%3])*(1-arr[(j+2)%3]);
alpha=1/(VN_decision_1 + VN_decision_0);
arr_VN[i].c[j][1]=alpha*VN_decision_1;
}
// Final decision after t th iteration that VN i is 1 or 0
arr_VN[i].decision=(arr_VN[i].value/(1-arr_VN[i].value))*(arr[0]/(1-arr[0]))*(arr[1]/(1-arr[1]))*(arr[2]/(1-arr[2]));
if(arr_VN[i].decision>=1)
{
arr_VN[i].decision=1;
++count;
}
else
{
arr_VN[i].decision=0;
++flag;
}
}
if(flag==12 || count==12)
break;
++terminate;
}
// Determine after decoding signal has stil any error or not
flag=1;
for(i=0;i<N;i++)
{
if(arr_VN[i].decision!=tr[i])
{
flag=0;
break;
}
}
// Increase Nerr if there is any error otherwise that is successfull decoding
if(flag==1)
++Ncorr[l];
else
++Nerr[l];
}
}
for(i=0;i<=100;i++)
//cout<<"p = "<<p[i]<<" "<<"Ncorr = "<<Ncorr[i]<<" Nerr = "<<Nerr[i]<<" Ncorr/Nsim = "<<(Ncorr[i]*1.0)/Nsim<<endl<<endl;
cout<<(Ncorr[i]*1.0)/Nsim<<" ";
}