2 * Copyright 1995 Phil Karn, KA9Q
3 * Copyright 2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
24 * Viterbi decoder for K=7 rate=1/2 convolutional code
25 * Some modifications from original Karn code by Matt Ettus
30 /* The two generator polynomials for the NASA Standard K=7 code.
31 * Since these polynomials are known to be optimal for this constraint
32 * length there is not much point in changing them. But if you do, you
33 * will have to regenerate the BUTTERFLY macro calls in viterbi()
38 /* The basic Viterbi decoder operation, called a "butterfly"
39 * operation because of the way it looks on a trellis diagram. Each
40 * butterfly involves an Add-Compare-Select (ACS) operation on the two nodes
41 * where the 0 and 1 paths from the current node merge at the next step of
44 * The code polynomials are assumed to have 1's on both ends. Given a
45 * function encode_state() that returns the two symbols for a given
46 * encoder state in the low two bits, such a code will have the following
47 * identities for even 'n' < 64:
49 * encode_state(n) = encode_state(n+65)
50 * encode_state(n+1) = encode_state(n+64) = (3 ^ encode_state(n))
52 * Any convolutional code you would actually want to use will have
53 * these properties, so these assumptions aren't too limiting.
55 * Doing this as a macro lets the compiler evaluate at compile time the
56 * many expressions that depend on the loop index and encoder state and
57 * emit them as immediate arguments.
58 * This makes an enormous difference on register-starved machines such
59 * as the Intel x86 family where evaluating these expressions at runtime
60 * would spill over into memory.
62 #define BUTTERFLY(i,sym) { \
65 /* ACS for 0 branch */\
66 m0 = state[i].metric + mets[sym]; /* 2*i */\
67 m1 = state[i+32].metric + mets[3^sym]; /* 2*i + 64 */\
69 next[2*i].metric = m0;\
70 next[2*i].path = state[i].path << 1;\
72 next[2*i].metric = m1;\
73 next[2*i].path = (state[i+32].path << 1)|1;\
75 /* ACS for 1 branch */\
76 m0 = state[i].metric + mets[3^sym]; /* 2*i + 1 */\
77 m1 = state[i+32].metric + mets[sym]; /* 2*i + 65 */\
79 next[2*i+1].metric = m0;\
80 next[2*i+1].path = state[i].path << 1;\
82 next[2*i+1].metric = m1;\
83 next[2*i+1].path = (state[i+32].path << 1)|1;\
87 extern unsigned char Partab[]; /* Parity lookup table */
89 /* Convolutionally encode data into binary symbols */
91 encode(unsigned char *symbols,
94 unsigned char encstate)
100 encstate = (encstate << 1) | ((*data >> i) & 1);
101 *symbols++ = Partab[encstate & POLYA];
102 *symbols++ = Partab[encstate & POLYB];
110 /* Viterbi decoder */
112 viterbi(unsigned long *metric, /* Final path metric (returned value) */
113 unsigned char *data, /* Decoded output data */
114 unsigned char *symbols, /* Raw deinterleaved input symbols */
115 unsigned int nbits, /* Number of output bits */
116 int mettab[2][256] /* Metric table, [sent sym][rx symbol] */
118 unsigned int bitcnt = 0;
122 struct viterbi_state state0[64],state1[64],*state,*next;
127 /* Initialize starting metrics to prefer 0 state */
130 state[i].metric = -999999;
133 for(bitcnt = 0;bitcnt < nbits;bitcnt++){
134 /* Read input symbol pair and compute all possible branch
137 mets[0] = mettab[0][symbols[0]] + mettab[0][symbols[1]];
138 mets[1] = mettab[0][symbols[0]] + mettab[1][symbols[1]];
139 mets[2] = mettab[1][symbols[0]] + mettab[0][symbols[1]];
140 mets[3] = mettab[1][symbols[0]] + mettab[1][symbols[1]];
143 /* These macro calls were generated by genbut.c */
177 /* Swap current and next states */
186 //if(bitcnt > nbits-7){
187 /* In tail, poison non-zero nodes */
188 //for(i=1;i<64;i += 2)
189 // state[i].metric = -9999999;
191 /* Produce output every 8 bits once path memory is full */
192 if((bitcnt % 8) == 5 && bitcnt > 32){
193 /* Find current best path */
194 bestmetric = state[0].metric;
197 if(state[i].metric > bestmetric){
198 bestmetric = state[i].metric;
203 printf("metrics[%d] = %d state = %lx\n",beststate,
204 state[beststate].metric,state[beststate].path);
206 *data++ = state[beststate].path >> 24;
210 /* Output remaining bits from 0 state */
211 // ETTUS Find best state instead
212 bestmetric = state[0].metric;
215 if(state[i].metric > bestmetric){
216 bestmetric = state[i].metric;
220 if((i = bitcnt % 8) != 6)
221 state[beststate].path <<= 6-i;
223 *data++ = state[beststate].path >> 24;
224 *data++ = state[beststate].path >> 16;
225 *data++ = state[beststate].path >> 8;
226 *data = state[beststate].path;
227 //printf ("BS = %d\tBSM = %d\tM0 = %d\n",beststate,state[beststate].metric,state[0].metric);
228 *metric = state[beststate].metric;
234 viterbi_chunks_init(struct viterbi_state* state) {
235 // Initialize starting metrics to prefer 0 state
240 state[i].metric = -999999;
244 viterbi_butterfly8(unsigned char *symbols, int mettab[2][256], struct viterbi_state *state0, struct viterbi_state *state1)
249 struct viterbi_state *state, *next;
252 // Operate on 16 symbols (8 bits) at a time
253 for(bitcnt = 0;bitcnt < 8;bitcnt++){
254 // Read input symbol pair and compute all possible branch metrics
255 mets[0] = mettab[0][symbols[0]] + mettab[0][symbols[1]];
256 mets[1] = mettab[0][symbols[0]] + mettab[1][symbols[1]];
257 mets[2] = mettab[1][symbols[0]] + mettab[0][symbols[1]];
258 mets[3] = mettab[1][symbols[0]] + mettab[1][symbols[1]];
261 // These macro calls were generated by genbut.c
262 BUTTERFLY(0,0);BUTTERFLY(1,1);BUTTERFLY(2,3);BUTTERFLY(3,2);
263 BUTTERFLY(4,3);BUTTERFLY(5,2);BUTTERFLY(6,0);BUTTERFLY(7,1);
264 BUTTERFLY(8,0);BUTTERFLY(9,1);BUTTERFLY(10,3);BUTTERFLY(11,2);
265 BUTTERFLY(12,3);BUTTERFLY(13,2);BUTTERFLY(14,0);BUTTERFLY(15,1);
266 BUTTERFLY(16,2);BUTTERFLY(17,3);BUTTERFLY(18,1);BUTTERFLY(19,0);
267 BUTTERFLY(20,1);BUTTERFLY(21,0);BUTTERFLY(22,2);BUTTERFLY(23,3);
268 BUTTERFLY(24,2);BUTTERFLY(25,3);BUTTERFLY(26,1);BUTTERFLY(27,0);
269 BUTTERFLY(28,1);BUTTERFLY(29,0);BUTTERFLY(30,2);BUTTERFLY(31,3);
271 // Swap current and next states
283 viterbi_butterfly2(unsigned char *symbols, int mettab[2][256], struct viterbi_state *state0, struct viterbi_state *state1)
285 //unsigned int bitcnt;
288 struct viterbi_state *state, *next;
291 // Operate on 4 symbols (2 bits) at a time
293 // Read input symbol pair and compute all possible branch metrics
294 mets[0] = mettab[0][symbols[0]] + mettab[0][symbols[1]];
295 mets[1] = mettab[0][symbols[0]] + mettab[1][symbols[1]];
296 mets[2] = mettab[1][symbols[0]] + mettab[0][symbols[1]];
297 mets[3] = mettab[1][symbols[0]] + mettab[1][symbols[1]];
299 // These macro calls were generated by genbut.c
300 BUTTERFLY(0,0);BUTTERFLY(1,1);BUTTERFLY(2,3);BUTTERFLY(3,2);
301 BUTTERFLY(4,3);BUTTERFLY(5,2);BUTTERFLY(6,0);BUTTERFLY(7,1);
302 BUTTERFLY(8,0);BUTTERFLY(9,1);BUTTERFLY(10,3);BUTTERFLY(11,2);
303 BUTTERFLY(12,3);BUTTERFLY(13,2);BUTTERFLY(14,0);BUTTERFLY(15,1);
304 BUTTERFLY(16,2);BUTTERFLY(17,3);BUTTERFLY(18,1);BUTTERFLY(19,0);
305 BUTTERFLY(20,1);BUTTERFLY(21,0);BUTTERFLY(22,2);BUTTERFLY(23,3);
306 BUTTERFLY(24,2);BUTTERFLY(25,3);BUTTERFLY(26,1);BUTTERFLY(27,0);
307 BUTTERFLY(28,1);BUTTERFLY(29,0);BUTTERFLY(30,2);BUTTERFLY(31,3);
312 // Read input symbol pair and compute all possible branch metrics
313 mets[0] = mettab[0][symbols[2]] + mettab[0][symbols[3]];
314 mets[1] = mettab[0][symbols[2]] + mettab[1][symbols[3]];
315 mets[2] = mettab[1][symbols[2]] + mettab[0][symbols[3]];
316 mets[3] = mettab[1][symbols[2]] + mettab[1][symbols[3]];
318 // These macro calls were generated by genbut.c
319 BUTTERFLY(0,0);BUTTERFLY(1,1);BUTTERFLY(2,3);BUTTERFLY(3,2);
320 BUTTERFLY(4,3);BUTTERFLY(5,2);BUTTERFLY(6,0);BUTTERFLY(7,1);
321 BUTTERFLY(8,0);BUTTERFLY(9,1);BUTTERFLY(10,3);BUTTERFLY(11,2);
322 BUTTERFLY(12,3);BUTTERFLY(13,2);BUTTERFLY(14,0);BUTTERFLY(15,1);
323 BUTTERFLY(16,2);BUTTERFLY(17,3);BUTTERFLY(18,1);BUTTERFLY(19,0);
324 BUTTERFLY(20,1);BUTTERFLY(21,0);BUTTERFLY(22,2);BUTTERFLY(23,3);
325 BUTTERFLY(24,2);BUTTERFLY(25,3);BUTTERFLY(26,1);BUTTERFLY(27,0);
326 BUTTERFLY(28,1);BUTTERFLY(29,0);BUTTERFLY(30,2);BUTTERFLY(31,3);
330 viterbi_get_output(struct viterbi_state *state, unsigned char *outbuf) {
331 // Produce output every 8 bits once path memory is full
332 // if((bitcnt % 8) == 5 && bitcnt > 32) {
334 // Find current best path
335 unsigned int i,beststate;
338 bestmetric = state[0].metric;
341 if(state[i].metric > bestmetric) {
342 bestmetric = state[i].metric;
345 *outbuf = state[beststate].path >> 24;
350 //printf ("BS = %d\tBSM = %d\tM0 = %d\n",beststate,state[beststate].metric,state[0].metric);
351 // In tail, poison non-zero nodes
352 //if(bits_out > packet_size-7)
353 // for(i=1;i<64;i += 2)
354 // state[i].metric = -9999999;