-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHDD_BSC_H3.cpp
302 lines (232 loc) · 6.45 KB
/
HDD_BSC_H3.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include<bits/stdc++.h>
using namespace std;
// BSC channel
// Degree of CN is 4
// CN container
struct CN
{
int 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
//VN container
struct VN
{
int 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
int value; // Store the value of the transmitted signal after adding noise in it
int decision; // After tanner graph decoding it makes final decision that VN i is 1 or 0
}arr_VN[12];
int main()
{
int N=12,U=9,i,j,k,count,c1,c2,c3,v1,v2,v3,v4,flag=1,terminate=1,Nsim=10000,Ksim,l,stop; // N is number of columns and U is number of rows of H - matrix
int Ncorr[81]={0},Nerr[81]={0},arr[3];
float p[81],r; // Crossover Probability
p[0]=0;
for(i=1;i<81;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<81;l++) // Outer for loop for crossover probability p
{
for(Ksim=1;Ksim<=Nsim;Ksim++) // Loop for Monte - Carlo simulations
{
int tr[N]={0}; // Transmitted Signal
int noise[N]={0}; // Noise
int rx[N]={0}; // Received Signal
// Loading VNs with values 0 or 1
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]))
noise[i]=1;
else
noise[i]=0;
if(noise[i]==tr[i]) // Same as XOR operation (if r > 1-p then, bit will be flipped)
rx[i]=0;
else
rx[i]=1;
arr_VN[i].value=rx[i];
for(j=0;j<3;j++)
arr_VN[i].c[j][1]=rx[i];
}
// Tanner Graph decoding
flag=terminate=0;
// terminate variable 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
stop=0; // This is for checking even parity of CNs, if all CNs have even parity then that will also be the breaking condition
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];
if((arr_CN[i].v[0][1] + arr_CN[i].v[1][1] + arr_CN[i].v[2][1] + arr_CN[i].v[3][1])%2==0) // Every CN is checking for even parity
++stop;
// CN sends message to each VN connected to it - the modulo two sum of all other VNs
for(j=0;j<3;j++)
{
if(arr_VN[v1-1].c[j][0]==i+1)
{
arr_VN[v1-1].c[j][1]=(arr_CN[i].v[1][1] + arr_CN[i].v[2][1] + arr_CN[i].v[3][1])%2;
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v2-1].c[j][0]==i+1)
{
arr_VN[v2-1].c[j][1]=(arr_CN[i].v[0][1] + arr_CN[i].v[2][1] + arr_CN[i].v[3][1])%2;
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v3-1].c[j][0]==i+1)
{
arr_VN[v3-1].c[j][1]=(arr_CN[i].v[0][1] + arr_CN[i].v[1][1] + arr_CN[i].v[3][1])%2;
break;
}
}
for(j=0;j<3;j++)
{
if(arr_VN[v4-1].c[j][0]==i+1)
{
arr_VN[v4-1].c[j][1]=(arr_CN[i].v[0][1] + arr_CN[i].v[1][1] + arr_CN[i].v[2][1])%2;
break;
}
}
}
if(stop==9) // Because all CNs have even parity and therefore no meaning to decode again
break;
// Decision Time For VN i to CN j (in this it is checking values other than CN j)
if(terminate!=99)
{
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++)
{
count=k=0;
if(arr[(j+1)%3]==0)
++count;
else
++k;
if(arr[(j+2)%3]==0)
++count;
else
++k;
if(arr_VN[i].value==0)
++count;
else
++k;
if(count>k)
arr_VN[i].c[j][1]=0;
else
arr_VN[i].c[j][1]=1;
}
}
}
++terminate;
}
// Final decision for VN i that it is 1 or 0
for(i=0;i<N;i++)
{
count=k=0;
for(j=0;j<3;j++)
{
if(arr_VN[i].c[j][1]==0)
++count;
else
++k;
}
if(arr_VN[i].value==0)
++count;
else
++k;
if(count>=k)
arr_VN[i].decision=0;
else
arr_VN[i].decision=1;
}
// 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<81;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<<" ";
}