mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.6 KiB
C
191 lines
4.6 KiB
C
11 years ago
|
/* K=9 r=1/3 Viterbi decoder in portable C
|
||
|
* Copyright Aug 2006, Phil Karn, KA9Q
|
||
|
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||
|
*/
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
11 years ago
|
#include <stdint.h>
|
||
|
|
||
11 years ago
|
#include <memory.h>
|
||
|
#include "viterbi37.h"
|
||
11 years ago
|
#include "parity.h"
|
||
|
#include <limits.h>
|
||
11 years ago
|
|
||
|
typedef union {
|
||
10 years ago
|
uint32_t w[64];
|
||
11 years ago
|
} metric_t;
|
||
|
typedef union {
|
||
11 years ago
|
unsigned long w[2];
|
||
11 years ago
|
} decision_t;
|
||
|
|
||
|
static union {
|
||
10 years ago
|
uint8_t c[128];
|
||
11 years ago
|
} Branchtab37[3];
|
||
|
|
||
|
/* State info for instance of Viterbi decoder */
|
||
|
struct v37 {
|
||
11 years ago
|
metric_t metrics1; /* path metric buffer 1 */
|
||
|
metric_t metrics2; /* path metric buffer 2 */
|
||
|
decision_t *dp; /* Pointer to current decision */
|
||
|
metric_t *old_metrics, *new_metrics; /* Pointers to path metrics, swapped on every bit */
|
||
|
decision_t *decisions; /* Beginning of decisions for block */
|
||
11 years ago
|
};
|
||
|
|
||
|
/* Initialize Viterbi decoder for start of new frame */
|
||
11 years ago
|
int init_viterbi37_port(void *p, uint32_t starting_state) {
|
||
11 years ago
|
struct v37 *vp = p;
|
||
11 years ago
|
uint32_t i;
|
||
11 years ago
|
|
||
|
if (p == NULL)
|
||
|
return -1;
|
||
|
for (i = 0; i < 64; i++)
|
||
|
vp->metrics1.w[i] = 63;
|
||
|
|
||
|
vp->old_metrics = &vp->metrics1;
|
||
|
vp->new_metrics = &vp->metrics2;
|
||
|
vp->dp = vp->decisions;
|
||
|
if (starting_state != -1) {
|
||
|
vp->old_metrics->w[starting_state & 255] = 0; /* Bias known start state */
|
||
|
}
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void set_viterbi37_polynomial_port(uint32_t polys[3]) {
|
||
|
uint32_t state;
|
||
11 years ago
|
|
||
|
for (state = 0; state < 32; state++) {
|
||
|
Branchtab37[0].c[state] =
|
||
|
(polys[0] < 0) ^ parity((2 * state) & abs(polys[0])) ? 255 : 0;
|
||
|
Branchtab37[1].c[state] =
|
||
|
(polys[1] < 0) ^ parity((2 * state) & abs(polys[1])) ? 255 : 0;
|
||
|
Branchtab37[2].c[state] =
|
||
|
(polys[2] < 0) ^ parity((2 * state) & abs(polys[2])) ? 255 : 0;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/* Create a new instance of a Viterbi decoder */
|
||
11 years ago
|
void *create_viterbi37_port(uint32_t polys[3], uint32_t len) {
|
||
11 years ago
|
struct v37 *vp;
|
||
11 years ago
|
|
||
11 years ago
|
set_viterbi37_polynomial_port(polys);
|
||
11 years ago
|
|
||
11 years ago
|
if ((vp = (struct v37 *) malloc(sizeof(struct v37))) == NULL)
|
||
|
return NULL ;
|
||
11 years ago
|
|
||
11 years ago
|
if ((vp->decisions = (decision_t *) malloc((len + 6) * sizeof(decision_t)))
|
||
|
== NULL) {
|
||
|
free(vp);
|
||
|
return NULL ;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return vp;
|
||
11 years ago
|
}
|
||
|
|
||
|
/* Viterbi chainback */
|
||
10 years ago
|
int chainback_viterbi37_port(void *p, uint8_t *data, /* Decoded output data */
|
||
11 years ago
|
uint32_t nbits, /* Number of data bits */
|
||
|
uint32_t endstate) { /* Terminal encoder state */
|
||
11 years ago
|
struct v37 *vp = p;
|
||
|
decision_t *d;
|
||
|
|
||
|
if (p == NULL)
|
||
|
return -1;
|
||
|
|
||
|
d = vp->decisions;
|
||
|
|
||
|
/* Make room beyond the end of the encoder register so we can
|
||
|
* accumulate a full byte of decoded data
|
||
|
*/
|
||
|
endstate %= 64;
|
||
|
endstate <<= 2;
|
||
|
|
||
|
/* The store into data[] only needs to be done every 8 bits.
|
||
|
* But this avoids a conditional branch, and the writes will
|
||
|
* combine in the cache anyway
|
||
|
*/
|
||
|
d += 6; /* Look past tail */
|
||
|
while (nbits-- != 0) {
|
||
|
int k;
|
||
|
|
||
|
k = (d[nbits].w[(endstate >> 2) / 32] >> ((endstate >> 2) % 32)) & 1;
|
||
|
endstate = (endstate >> 1) | (k << 7);
|
||
|
data[nbits] = k;
|
||
|
}
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
|
/* Delete instance of a Viterbi decoder */
|
||
|
void delete_viterbi37_port(void *p) {
|
||
11 years ago
|
struct v37 *vp = p;
|
||
11 years ago
|
|
||
11 years ago
|
if (vp != NULL) {
|
||
|
free(vp->decisions);
|
||
|
free(vp);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/* C-language butterfly */
|
||
|
#define BFLY(i) {\
|
||
10 years ago
|
uint32_t metric,m0,m1,decision;\
|
||
11 years ago
|
metric = (Branchtab37[0].c[i] ^ sym0) + (Branchtab37[1].c[i] ^ sym1) + \
|
||
|
(Branchtab37[2].c[i] ^ sym2);\
|
||
|
m0 = vp->old_metrics->w[i] + metric;\
|
||
|
m1 = vp->old_metrics->w[i+32] + (765 - metric);\
|
||
|
decision = (signed int)(m0-m1) > 0;\
|
||
|
vp->new_metrics->w[2*i] = decision ? m1 : m0;\
|
||
|
d->w[i/16] |= decision << ((2*i)&31);\
|
||
|
m0 -= (metric+metric-765);\
|
||
|
m1 += (metric+metric-765);\
|
||
|
decision = (signed int)(m0-m1) > 0;\
|
||
|
vp->new_metrics->w[2*i+1] = decision ? m1 : m0;\
|
||
|
d->w[i/16] |= decision << ((2*i+1)&31);\
|
||
|
}
|
||
|
|
||
|
/* Update decoder with a block of demodulated symbols
|
||
|
* Note that nbits is the number of decoded data bits, not the number
|
||
|
* of symbols!
|
||
|
*/
|
||
|
|
||
11 years ago
|
int update_viterbi37_blk_port(void *p, uint8_t *syms, uint32_t nbits, uint32_t *best_state) {
|
||
11 years ago
|
struct v37 *vp = p;
|
||
|
decision_t *d;
|
||
|
|
||
|
if (p == NULL)
|
||
|
return -1;
|
||
11 years ago
|
uint32_t k=0;
|
||
11 years ago
|
d = (decision_t *) vp->dp;
|
||
|
while (nbits--) {
|
||
|
void *tmp;
|
||
11 years ago
|
uint8_t sym0, sym1, sym2;
|
||
|
uint32_t i;
|
||
11 years ago
|
|
||
|
d->w[0] = d->w[1] = 0;
|
||
|
|
||
|
sym0 = *syms++;
|
||
|
sym1 = *syms++;
|
||
|
sym2 = *syms++;
|
||
|
|
||
|
k++;
|
||
|
for (i = 0; i < 32; i++)
|
||
|
BFLY(i);
|
||
|
|
||
|
d++;
|
||
|
tmp = vp->old_metrics;
|
||
|
vp->old_metrics = vp->new_metrics;
|
||
|
vp->new_metrics = tmp;
|
||
|
}
|
||
|
if (best_state) {
|
||
11 years ago
|
uint32_t i, bst=0;
|
||
|
uint32_t minmetric=UINT_MAX;
|
||
11 years ago
|
for (i=0;i<64;i++) {
|
||
10 years ago
|
if (vp->old_metrics->w[i] <= minmetric) {
|
||
11 years ago
|
bst = i;
|
||
|
minmetric = vp->old_metrics->w[i];
|
||
|
}
|
||
|
}
|
||
|
*best_state = bst;
|
||
|
}
|
||
|
vp->dp = d;
|
||
|
return 0;
|
||
11 years ago
|
}
|