-
Notifications
You must be signed in to change notification settings - Fork 3
/
bcjr_decoder.c
222 lines (196 loc) · 5.58 KB
/
bcjr_decoder.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
/* note, the second half of the table must only be the second transition to each state */
static int translte8[16][4] = {
{1-1, 1-1, 0, 0},
{2-1, 5-1, 0, 0},
{3-1, 6-1, 0, 1},
{4-1, 2-1, 0, 1},
{5-1, 3-1, 0, 1},
{6-1, 7-1, 0, 1},
{7-1, 8-1, 0, 0},
{8-1, 4-1, 0, 0},
{1-1, 5-1, 1, 1},
{2-1, 1-1, 1, 1},
{3-1, 2-1, 1, 0},
{4-1, 6-1, 1, 0},
{5-1, 7-1, 1, 0},
{6-1, 3-1, 1, 0},
{7-1, 4-1, 1, 1},
{8-1, 8-1, 1, 1}
};
#ifndef max
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif
__inline double maxstar(double a, double b)
{
return max(a,b) + log(1.0+exp(-fabs(a-b)));
}
void bcjr_decoder(double *uncoded_in, double *coded_in1, double *uncoded_out,
int len, int trans[][4], int state_count, int trans_count, int last_state)
{
int i,j;
int transc = trans_count;
int set0,set1;
double p1,p0;
int a,b;
double **gammas;
double **alphas;
double **betas;
double temp[16];
int states = state_count;
/* calculate gammas */
gammas = (double**) malloc (transc*sizeof(double *));
for (i = 0; i < transc; i++)
gammas[i] = (double*) malloc(len*sizeof(double));
for (i = 0; i < transc; i++)
{
if (trans[i][2] && trans[i][3]){
for (j = 0; j < len; j++){
gammas[i][j] = uncoded_in[j] + coded_in1[j];
}
}
else if (trans[i][2]){
for (j = 0; j < len; j++)
gammas[i][j] = uncoded_in[j];
}
else if (trans[i][3]){
for (j = 0; j < len; j++)
gammas[i][j] = coded_in1[j];
}
else{
for (j = 0; j < len; j++)
gammas[i][j] = 0;
}
}
/* set and initialise memory */
alphas = (double**) malloc (states*sizeof(double *));
for (i = 0; i < states; i++)
alphas[i] = (double*) malloc(len*sizeof(double));
betas = (double**) malloc (states*sizeof(double *));
for (i = 0; i < states; i++)
betas[i] = (double*) malloc(len*sizeof(double));
/* forward recursion (alphas) */
alphas[0][0] = 0; /* first state */
for (i = 1; i < states; i++)
alphas[i][0] = -9000;
for (i = 1; i < len; i++)
{
for (j = 0; j < states; j++)
temp[trans[j][1]] = alphas[trans[j][0]][i-1] + gammas[j][i-1];
for (j = states; j < transc; j++)
alphas[trans[j][1]][i] = maxstar( temp[trans[j][1]], alphas[trans[j][0]][i-1] + gammas[j][i-1] );
}
/* backwards recursion (betas) */
/* double betas[8][len]; */
if (last_state < 1 || last_state > states){
for (i = 0; i < states; i++)
betas[i][len-1] = 0; /* end state unknown */
}
else
{
for (i = 0; i < states; i++)
betas[i][len-1] = -9000;
betas[last_state-1][len-1] = 0;
}
for (i = len-2; i >= 0; i--)
{
for (j = 0; j < states; j++)
temp[trans[j][0]] = betas[trans[j][1]][i+1] + gammas[j][i+1];
for (j = states; j < transc; j++)
betas[trans[j][0]][i] = maxstar( temp[trans[j][0]], betas[trans[j][1]][i+1] + gammas[j][i+1] );
}
/*
* if (trans_prob != (double*)0)
* {
* mexPrintf("trans probs\n");
* for (i=0;i<transc;i++)
* {
* mexPrintf("%f ",trans_prob[i]);
*
*
* }
* }
* mexPrintf("GAMMAS\n");
* for (i=0;i<transc;i++)
* {
* for (j=0;j<len;j++)
* mexPrintf("%f ",gammas[i][j]);
* mexPrintf("\n");
*
* }
* mexPrintf("ALPHAS\n");
* for (i=0;i<states;i++)
* {
* for (j=0;j<len;j++)
* mexPrintf("%f ",alphas[i][j]);
* mexPrintf("\n");
*
* }
* mexPrintf("\n");
* mexPrintf("\n");
* mexPrintf("BETAS\n");
* for (i=0;i<states;i++)
* {
* for (j=0;j<len;j++)
* mexPrintf("%f ",betas[i][j]);
* mexPrintf("\n");
*
* }
* mexPrintf("\n");
*/
/* deltas */
/* reuse gammas memory */
for (i = 0; i < transc; i++)
{
a = trans[i][0];
b = trans[i][1];
for (j = 0; j < len; j++)
gammas[i][j] += alphas[a][j] + betas[b][j];
}
/* extrinisic uncoded llr */
for (j = 0; j < len; j++)
{
set0 = 0;
set1 = 0;
for (i = 0; i < transc; i++)
{
if (trans[i][2]){
if (set1)
p1 = maxstar(p1,gammas[i][j]);
else
{
set1 = 1;
p1 = gammas[i][j];
}
}
else{
if (set0)
p0 = maxstar(p0,gammas[i][j]);
else
{
set0 = 1;
p0 = gammas[i][j];
}
}
}
uncoded_out[j] = p1 - p0 - uncoded_in[j];
}
for (i = 0; i < transc; i++){
free(gammas[i]);
}
free(gammas);
for (i = 0; i < states; i++){
free(alphas[i]);
}
free(alphas);
for (i = 0; i < states; i++){
free(betas[i]);
}
free(betas);
}