-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_bcjr_base.cc
205 lines (168 loc) · 5.1 KB
/
log_bcjr_base.cc
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
/* -*- c++ -*- */
/*
* Copyright 2020 Alexandre Marquet.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "log_bcjr_base.h"
log_bcjr_base::log_bcjr_base(int I, int S, int O,
const std::vector<int> &NS,
const std::vector<int> &OS)
: d_I(I), d_S(S), d_O(O), d_ordered_OS(S*I)
{
if (NS.size() != S*I) {
throw std::runtime_error("Invalid size for NS.");
}
d_NS = NS;
if (OS.size() != S*I) {
throw std::runtime_error("Invalid size for OS.");
}
d_OS = OS;
generate_PS_PI();
//Compute ordered_OS
std::vector<int>::iterator ordered_OS_it = d_ordered_OS.begin();
for(int s=0 ; s < S ; ++s) {
for(size_t i=0 ; i<(d_PS[s]).size() ; ++i) {
*(ordered_OS_it++) = OS[d_PS[s][i]*I + d_PI[s][i]];
}
}
}
void
log_bcjr_base::generate_PS_PI()
{
d_PS.resize(d_S);
d_PI.resize(d_S);
for(int i=0 ; i<d_S ; ++i) {
d_PS[i].reserve(d_I*d_S); // max possible size
d_PI[i].reserve(d_I*d_S);
for(int ii=0 ; ii<d_S ; ++ii) {
for(int jj=0 ; jj<d_I ; ++jj) {
if(d_NS[ii*d_I+jj] != i) {
continue;
}
d_PS[i].push_back(ii);
d_PI[i].push_back(jj);
}
}
}
}
void
log_bcjr_base::compute_fw_metrics(const std::vector<float> &G,
const std::vector<float> &A0, std::vector<float> &A, size_t K)
{
A.resize(d_S*(K+1), -std::numeric_limits<float>::max());
float norm_A = -std::numeric_limits<float>::max();
std::vector<float>::iterator A_prev, A_curr;
std::vector<int>::iterator PS_it, ordered_OS_it;
//Integrate initial forward metrics
std::copy(A0.begin(), A0.end(), A.begin());
//Initialize iterators
A_prev = A.begin();
A_curr = A.begin() + d_S;
for(std::vector<float>::const_iterator G_k = G.begin() ;
G_k != G.end() ; G_k += d_O) {
ordered_OS_it = d_ordered_OS.begin();
for(int s=0 ; s < d_S ; ++s) {
//Iterators for previous state and previous input lists
PS_it=d_PS[s].begin();
//Loop
for(size_t i=0 ; i<(d_PS[s]).size() ; ++i) {
// Equivalent to:
// *A_curr = _max_star(*A_curr,
// A_prev[PS[s][i]] + G_k[d_OS[PS[s][i]*I + PI[s][i]]);
*A_curr = _max_star(*A_curr,
A_prev[*(PS_it++)] + G_k[*(ordered_OS_it++)]);
}
//Update iterators
++A_curr;
}
//Advance A_prev
A_prev += d_S;
//Metrics normalization
norm_A = _max_star(&(*(A_prev)), d_S);
std::transform(A_prev, A_curr, A_prev,
std::bind2nd(std::minus<float>(), norm_A));
}
}
void
log_bcjr_base::compute_bw_metrics(const std::vector<float> &G,
const std::vector<float> &BK, std::vector<float> &B, size_t K)
{
B.resize(d_S*(K+1), -std::numeric_limits<float>::max());
float norm_B = -std::numeric_limits<float>::max();
std::vector<float>::reverse_iterator B_next, B_curr;
std::vector<int>::reverse_iterator NS_it, OS_it;
//Integrate initial forward metrics
std::copy(BK.rbegin(), BK.rend(), B.rbegin());
//Initialize iterators
B_curr = B.rbegin() + d_S;
B_next = B.rbegin();
for(std::vector<float>::const_reverse_iterator G_k = G.rbegin() ;
G_k != G.rend() ; G_k += d_O) {
//Iterators for next state and next output lists
NS_it=d_NS.rbegin();
OS_it=d_OS.rbegin();
for(int s=0 ; s < d_S ; ++s) {
//Loop
for(size_t i=0 ; i < d_I ; ++i) {
*B_curr = _max_star(*B_curr,
B_next[(d_S-1)-*(NS_it++)] + G_k[(d_O-1)-*(OS_it++)]);
}
//Update iterators
++B_curr;
}
//Advance B_next (go back, as it is a reverse iterator...)
B_next += d_S;
//Metrics normalization
norm_B = _max_star(&(*B_curr)+1, d_S);
std::transform(B_next, B_curr, B_next,
std::bind2nd(std::minus<float>(), norm_B));
}
}
void
log_bcjr_base::compute_app(const std::vector<float> &A, const std::vector<float> &B,
const std::vector<float> &G, size_t K, std::vector<float> &out)
{
std::vector<float>::const_iterator A_it = A.begin();
std::vector<float>::const_iterator B_it = B.begin() + d_S;
out.reserve(d_S*d_I*K);
for(std::vector<float>::const_iterator G_k = G.begin() ;
G_k != G.end() ; G_k += d_O) {
for(int s=0 ; s < d_S ; ++s) {
for (int i=0 ; i < d_I ; ++i) {
out.push_back(B_it[d_NS[s*d_I+i]] + G_k[d_OS[s*d_I+i]] + *A_it);
}
//Update forward iterator
++A_it;
}
//Update backward iterator
B_it += d_S;
}
}
void
log_bcjr_base::log_bcjr_algorithm(const std::vector<float> &A0,
const std::vector<float> &BK, const std::vector<float> &in,
std::vector<float> &out)
{
std::vector<float> A, B;
size_t K = in.size()/d_O;
//Forward recursion
compute_fw_metrics(in, A0, A, K);
//Backward recursion
compute_bw_metrics(in, BK, B, K);
//Compute branch APP
compute_app(A, B, in, K, out);
}