-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviterbi_decoder.c
321 lines (259 loc) · 9.67 KB
/
viterbi_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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "mex.h"
#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 */
/* last column is transition probablity */
static int transurc8[16][6] = {
{0, 0, 0, 0, 0, 0},
{1, 4, 0, 1, 0, 0},
{2, 5, 0, 1, 0, 0},
{3, 1, 0, 0, 0, 0},
{4, 6, 0, 1, 0, 0},
{5, 2, 0, 0, 0, 0},
{6, 3, 0, 0, 0, 0},
{7, 7, 0, 1, 0, 0},
{0, 4, 1, 1, 0, 0},
{1, 0, 1, 0, 0, 0},
{2, 1, 1, 0, 0, 0},
{3, 5, 1, 1, 0, 0},
{4, 2, 1, 0, 0, 0},
{5, 6, 1, 1, 0, 0},
{6, 7, 1, 1, 0, 0},
{7, 3, 1, 0, 0, 0}
};
static int translte8[16][6] = {
{1-1, 1-1, 0, 0, 0, 0},
{2-1, 5-1, 0, 0, 0, 0},
{3-1, 6-1, 0, 1, 0, 0},
{4-1, 2-1, 0, 1, 0, 0},
{5-1, 3-1, 0, 1, 0, 0},
{6-1, 7-1, 0, 1, 0, 0},
{7-1, 8-1, 0, 0, 0, 0},
{8-1, 4-1, 0, 0, 0, 0},
{1-1, 5-1, 1, 1, 0, 0},
{2-1, 1-1, 1, 1, 0, 0},
{3-1, 2-1, 1, 0, 0, 0},
{4-1, 6-1, 1, 0, 0, 0},
{5-1, 7-1, 1, 0, 0, 0},
{6-1, 3-1, 1, 0, 0, 0},
{7-1, 4-1, 1, 1, 0, 0},
{8-1, 8-1, 1, 1, 0, 0}
};
static int transurc4[16][6] = {
{1, 1, 0, 0, 0, 0},
{1, 3, 1, 1, 0, 0},
{2, 3, 0, 1, 0, 0},
{2, 1, 1, 0, 0, 0},
{3, 4, 0, 1, 0, 0},
{3, 2, 1, 0, 0, 0},
{4, 2, 0, 0, 0, 0},
{4, 4, 1, 1, 0, 0}
};
#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)));
}
/* reg_trellis - does each state have 2 transitions ending there? */
void viterbi_decoder(double *uncoded_in,
double *uncoded_out,
int len, int trans[][6], int state_count, int trans_count,
double *trans_prob)
{
int i,j;
int transc = trans_count;
int set0,set1;
double p1,p0;
int a,b;
double **gammas;
double **alphas;
int states = state_count;
int state,best_state;
double best_alpha;
/* 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]){
for (j = 0; j < len; j++)
gammas[i][j] = uncoded_in[j];
}else{
for (j = 0; j < len; j++)
gammas[i][j] = 0;
}
}
if (trans_prob != (double*)0)
{
for (i = 0; i < transc; i++)
{
for (j=0; j < len; j++)
gammas[i][j] += trans_prob[i];
}
}
/* set and initialise memory */
alphas = (double**) malloc (states*sizeof(double *));
for (i = 0; i < states; i++)
alphas[i] = (double*) malloc(len*sizeof(double));
for (i = 0; i < states; i++) {
for (j=0; j< len; j++)
alphas[i][j] = -9000;
}
/* 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 < transc; j++)
alphas[trans[j][1]][i] = maxstar( alphas[trans[j][1]][i], alphas[trans[j][0]][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");
*/
/* viterbi time */
state = 0;
for (i = len-1; i>=0; i--){
best_alpha = -900000000;
for (j = 0; j < transc; j++){
if ((trans[j][1] == state) && (alphas[trans[j][0]][i] > best_alpha)){
best_alpha = alphas[trans[j][0]][i];
uncoded_out[i] = trans[j][2];
best_state = trans[j][0];
}
}
state = best_state;
}
for (i = 0; i < transc; i++){
free(gammas[i]);
}
free(gammas);
for (i = 0; i < states; i++){
free(alphas[i]);
}
free(alphas);
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *uncoded_in;
double *uncoded_out;
int len,i,j,states;
int blank_transitions[600][6];
double *trans_probs;
double *p;
int codeword_count = 1;
/* check for proper number of arguments */
if(nrhs<2) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs",">=Two inputs required. [uncoded_out] = viterbi_decoder(uncoded_in, transitions)");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required. [uncoded_out] = viterbi_decoder(uncoded_in, transitions)");
}
/* make sure the 1st input argument is type double */
if( !mxIsDouble(prhs[0]) ||
mxIsComplex(prhs[0])) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","Input matrix must be type double.");
}
/* make sure the 2nd input argument is type double */
if( !mxIsDouble(prhs[1]) ||
mxIsComplex(prhs[1])) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","Input matrix must be type double.");
}
/* check that number of rows in 1st input argument is 1 */
if(mxGetM(prhs[0])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","Input must be a row vector.");
}
len = mxGetN(prhs[0]);
if (len < 3)
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","Input must be a row vector.");
{
if(nrhs!=2)
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Two inputs required. [uncoded_out, coded_out] = viterbi_decoder(uncoded_in, transitions).");
if( !mxIsDouble(prhs[1]) ||
mxIsComplex(prhs[1])) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","Input matrix must be type double.");
}
if(mxGetM(prhs[1])==1 || mxGetM(prhs[1])>599) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notMatrix","transitions must be Tx5 (one codeword). Max transitions = 600");
}
if(mxGetN(prhs[1])!=5 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notMatrix","transitions must be Tx5 (one codeword). Max transitions = 600");
}
trans_probs = malloc(mxGetM(prhs[1])*sizeof(double *));
p = mxGetPr(prhs[1]);
for (i=0; i < 2; i++){
for (j=0; j < mxGetM(prhs[1]); j++)
blank_transitions[j][i] = ((int)*p++)-1;
}
for (i=2; i < 4; i++){
for (j=0; j < mxGetM(prhs[1]); j++)
blank_transitions[j][i] = (int)*p++;
}
for (i=0; i < mxGetM(prhs[1]); i++)
trans_probs[i] = *p++;
}
/* get pointers */
uncoded_in = mxGetPr(prhs[0]);
/* create the output matrixs */
plhs[0] = mxCreateDoubleMatrix(1,(mwSize)len,mxREAL);
/* get a pointer to the real data in the output matrix */
uncoded_out = mxGetPr(plhs[0]);
/* call the computational routine */
states = 0;
for (i=0;i<mxGetM(prhs[1]);i++)
{
states = max(states, blank_transitions[i][0]);
states = max(states, blank_transitions[i][1]);
}
viterbi_decoder(uncoded_in,
uncoded_out, len,blank_transitions,
states+1,mxGetM(prhs[1]),trans_probs);
free(trans_probs);
/* mexPrintf("%f %f %f %f %f",maxstar(2.4,2.4),maxstar(6.7,8.1), maxstar(3,200), maxstar(-5,-8999), maxstar(-6.7,8.1));*/
}