-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.c
97 lines (85 loc) · 2.79 KB
/
metrics.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
/* $Header */
#undef debug
/* Generate metric tables for a soft-decision convolutional decoder
* assuming gaussian noise on a PSK channel.
*
* Works from "first principles" by evaluating the normal probability
* function and then computing the log-likelihood function
* for every possible received symbol value
*
* Copyright 1995 Phil Karn, KA9Q
*/
/* Symbols are offset-binary, with 128 corresponding to an erased (no
* information) symbol
*/
#define OFFSET 128
#ifdef debug
#include <stdio.h>
#endif
#include <stdlib.h>
#include <math.h>
/* Normal function integrated from -Inf to x. Range: 0-1 */
#define normal(x) (0.5 + 0.5*erf((x)/M_SQRT2))
/* Logarithm base 2 */
#define log2(x) (log(x)*M_LOG2E)
/* Generate log-likelihood metrics for 8-bit soft quantized channel
* assuming AWGN and BPSK
*/
void
gen_met(
int mettab[2][256], /* Metric table, [sent sym][rx symbol] */
int amp, /* Signal amplitude, units */
double noise, /* Relative noise voltage */
double bias, /* Metric bias; 0 for viterbi, rate for sequential */
int scale /* Scale factor */
){
int s,bit;
double metrics[2][256];
double p0,p1;
/* Zero is a special value, since this sample includes all
* lower samples that were clipped to this value, i.e., it
* takes the whole lower tail of the curve
*/
p1 = normal(((0-OFFSET+0.5)/amp - 1)/noise); /* P(s|1) */
/* Prob of this value occurring for a 0-bit */ /* P(s|0) */
p0 = normal(((0-OFFSET+0.5)/amp + 1)/noise);
metrics[0][0] = log2(2*p0/(p1+p0)) - bias;
metrics[1][0] = log2(2*p1/(p1+p0)) - bias;
for(s=1;s<255;s++){
/* P(s|1), prob of receiving s given 1 transmitted */
p1 = normal(((s-OFFSET+0.5)/amp - 1)/noise) -
normal(((s-OFFSET-0.5)/amp - 1)/noise);
/* P(s|0), prob of receiving s given 0 transmitted */
p0 = normal(((s-OFFSET+0.5)/amp + 1)/noise) -
normal(((s-OFFSET-0.5)/amp + 1)/noise);
#ifdef debug
fprintf(stderr,"P(%d|1) = %g, P(%d|0) = %g\n",s,p1,s,p0);
#endif
metrics[0][s] = log2(2*p0/(p1+p0)) - bias;
metrics[1][s] = log2(2*p1/(p1+p0)) - bias;
}
/* 255 is also a special value */
/* P(s|1) */
p1 = 1 - normal(((255-OFFSET-0.5)/amp - 1)/noise);
/* P(s|0) */
p0 = 1 - normal(((255-OFFSET-0.5)/amp + 1)/noise);
metrics[0][255] = log2(2*p0/(p1+p0)) - bias;
metrics[1][255] = log2(2*p1/(p1+p0)) - bias;
#ifdef debug
/* The probability of a raw symbol error is the probability
* that a 1-bit would be received as a sample with value
* 0-128. This is the offset normal curve integrated from -Inf to 0.
*/
fprintf(stderr,"symbol Pe = %g\n",normal(-1/noise));
#endif
for(bit=0;bit<2;bit++){
for(s=0;s<256;s++){
/* Scale and round to nearest integer */
mettab[bit][s] = floor(metrics[bit][s] * scale + 0.5);
#ifdef debug
fprintf(stderr,"metrics[%d][%d] = %g, mettab = %d\n",
bit,s,metrics[bit][s],mettab[bit][s]);
#endif
}
}
}