forked from E3V3A/gsm-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viterbi.c
149 lines (125 loc) · 3.47 KB
/
viterbi.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
/* (C) 2011 by Sylvain Munaut <tnt@246tNt.com>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "viterbi.h"
#define CONV_N 2
#define CONV_K 5
#define CONV_N_STATES (1<<(CONV_K-1))
#define MAX_AE 0x00ffffff
static const uint8_t conv_cch_next_output[CONV_N_STATES][2] = {
{0, 3}, {3, 0}, {3, 0}, {0, 3},
{0, 3}, {3, 0}, {3, 0}, {0, 3},
{1, 2}, {2, 1}, {2, 1}, {1, 2},
{1, 2}, {2, 1}, {2, 1}, {1, 2}
};
static const uint8_t conv_cch_next_state[CONV_N_STATES][2] = {
{0, 8}, {0, 8}, {1, 9}, {1, 9},
{2, 10}, {2, 10}, {3, 11}, {3, 11},
{4, 12}, {4, 12}, {5, 13}, {5, 13},
{6, 14}, {6, 14}, {7, 15}, {7, 15}
};
int conv_cch_encode(const uint8_t *in, uint8_t *out, unsigned size)
{
unsigned int i, state = 0, o;
for(i = 0; i < size; i++) {
o = conv_cch_next_output[state][in[i]];
state = conv_cch_next_state[state][in[i]];
*out++ = !!(o & 2);
*out++ = o & 1;
}
return 0;
}
int conv_cch_decode(int8_t *input, uint8_t *output, int n)
{
int i, s, b;
unsigned int ae[CONV_N_STATES];
unsigned int ae_next[CONV_N_STATES];
int8_t in_sym[CONV_N];
int8_t ev_sym[CONV_N];
int state_history[CONV_N_STATES][n];
int min_ae;
int min_state;
int cur_state;
/* Initial error (only state 0 is valid) */
ae[0] = 0;
for (i=1; i<CONV_N_STATES; i++) {
ae[i] = MAX_AE;
}
/* Scan the treillis */
for (i=0; i<n; i++) {
/* Reset next accumulated error */
for (s=0; s<CONV_N_STATES; s++) {
ae_next[s] = MAX_AE;
}
/* Get input */
in_sym[0] = input[2*i+0];
in_sym[1] = input[2*i+1];
/* Scan all states */
for (s=0; s<CONV_N_STATES; s++)
{
/* Scan possible input bits */
for (b=0; b<2; b++)
{
int nae;
/* Next output and state */
uint8_t out = conv_cch_next_output[s][b];
uint8_t state = conv_cch_next_state[s][b];
/* Expand */
ev_sym[0] = out & 2 ? -127 : 127;
ev_sym[1] = out & 1 ? -127 : 127;
/* New error for this path */
#define DIFF(x,y) (((x-y)*(x-y)) >> 9)
nae = ae[s] + \
DIFF(ev_sym[0], in_sym[0]) + \
DIFF(ev_sym[1], in_sym[1]);
/* Is it survivor */
if (ae_next[state] > nae) {
ae_next[state] = nae;
state_history[state][i+1] = s;
}
}
}
/* Copy accumulated error */
memcpy(ae, ae_next, sizeof(int) * CONV_N_STATES);
}
/* Find state with least error */
min_ae = MAX_AE;
min_state = -1;
for (s=0; s<CONV_N_STATES; s++)
{
if (ae[s] < min_ae) {
min_ae = ae[s];
min_state = s;
}
}
/* Traceback */
cur_state = min_state;
for (i=n-1; i >= 0; i--)
{
min_state = cur_state;
cur_state = state_history[cur_state][i+1];
if (conv_cch_next_state[cur_state][0] == min_state)
output[i] = 0;
else
output[i] = 1;
}
return 0;
}