|
| 1 | +/* -*- c++ -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2019 Free Software Foundation, Inc. |
| 4 | + * |
| 5 | + * This is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 3, or (at your option) |
| 8 | + * any later version. |
| 9 | + * |
| 10 | + * This software is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this software; see the file COPYING. If not, write to |
| 17 | + * the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | + * Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "viterbi.h" |
| 22 | + |
| 23 | +viterbi::viterbi(int I, int S, int O, |
| 24 | + const std::vector<int> &NS, |
| 25 | + const std::vector<int> &OS) |
| 26 | + : d_I(I), d_S(S), d_O(O) |
| 27 | +{ |
| 28 | + if (NS.size() != S*I) { |
| 29 | + throw std::runtime_error("Invalid size for NS."); |
| 30 | + } |
| 31 | + d_NS = NS; |
| 32 | + |
| 33 | + if (OS.size() != S*I) { |
| 34 | + throw std::runtime_error("Invalid size for OS."); |
| 35 | + } |
| 36 | + d_OS = OS; |
| 37 | + |
| 38 | + generate_PS_PI(); |
| 39 | +} |
| 40 | + |
| 41 | +void |
| 42 | +viterbi::generate_PS_PI() |
| 43 | +{ |
| 44 | + d_PS.resize(d_S); |
| 45 | + d_PI.resize(d_S); |
| 46 | + |
| 47 | + for(int i=0;i<d_S;i++) { |
| 48 | + d_PS[i].resize(d_I*d_S); // max possible size |
| 49 | + d_PI[i].resize(d_I*d_S); |
| 50 | + int j=0; |
| 51 | + for(int ii=0;ii<d_S;ii++) for(int jj=0;jj<d_I;jj++) { |
| 52 | + if(d_NS[ii*d_I+jj]!=i) continue; |
| 53 | + d_PS[i][j]=ii; |
| 54 | + d_PI[i][j]=jj; |
| 55 | + j++; |
| 56 | + } |
| 57 | + d_PS[i].resize(j); |
| 58 | + d_PI[i].resize(j); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void |
| 63 | +viterbi::viterbi_algorithm(int K, int S0, int SK, const float *in, |
| 64 | + unsigned char *out) |
| 65 | +{ |
| 66 | + viterbi_algorithm(d_I, d_S, d_O, d_NS, d_OS, d_PS, d_PI, K, S0, SK, in, out); |
| 67 | +} |
| 68 | + |
| 69 | +void |
| 70 | +viterbi::viterbi_algorithm(int I, int S, int O, const std::vector<int> &NS, |
| 71 | + const std::vector<int> &OS, const std::vector< std::vector<int> > &PS, |
| 72 | + const std::vector< std::vector<int> > &PI, int K, int S0, int SK, |
| 73 | + const float *in, unsigned char *out) |
| 74 | +{ |
| 75 | + int tb_state, pidx; |
| 76 | + float can_metric = std::numeric_limits<float>::max(); |
| 77 | + float min_metric = std::numeric_limits<float>::max(); |
| 78 | + |
| 79 | + std::vector<int> trace(K*S); |
| 80 | + std::vector<float> alpha_prev(S, std::numeric_limits<float>::max()); |
| 81 | + std::vector<float> alpha_curr(S, std::numeric_limits<float>::max()); |
| 82 | + |
| 83 | + std::vector<float>::iterator alpha_curr_it; |
| 84 | + std::vector<int>::const_iterator PS_it, PI_it; |
| 85 | + std::vector<int>::iterator trace_it = trace.begin(); |
| 86 | + |
| 87 | + //If initial state was specified |
| 88 | + if(S0 != -1) { |
| 89 | + alpha_prev[S0] = 0.0; |
| 90 | + } |
| 91 | + |
| 92 | + for(float* in_k=(float*)in ; in_k < (float*)in + K*O ; in_k += O) { |
| 93 | + //Current path metric iterator |
| 94 | + alpha_curr_it = alpha_curr.begin(); |
| 95 | + for(int s=0 ; s < S ; ++s) { |
| 96 | + //Iterators for previous state and previous input lists |
| 97 | + PS_it=PS[s].begin(); |
| 98 | + PI_it=PI[s].begin(); |
| 99 | + |
| 100 | + //ACS for state s |
| 101 | + //Pre-loop |
| 102 | + *alpha_curr_it = alpha_prev[*PS_it] + in_k[OS[(*PS_it)*I + (*PI_it)]]; |
| 103 | + *trace_it = 0; |
| 104 | + |
| 105 | + //Loop |
| 106 | + for(size_t i=1 ; i<(PS[s]).size() ; ++i) { |
| 107 | + //Update PS/PI iterators |
| 108 | + ++PS_it; |
| 109 | + ++PI_it; |
| 110 | + |
| 111 | + //ADD |
| 112 | + can_metric = alpha_prev[*PS_it] + in_k[OS[(*PS_it)*I + (*PI_it)]]; |
| 113 | + |
| 114 | + //COMPARE |
| 115 | + if(can_metric < *alpha_curr_it) { |
| 116 | + //SELECT |
| 117 | + *alpha_curr_it = can_metric; |
| 118 | + *trace_it = i; //Store previous input index for traceback |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + //Update trace and path metric iterator |
| 123 | + ++trace_it; |
| 124 | + ++alpha_curr_it; |
| 125 | + } |
| 126 | + |
| 127 | + //Metrics normalization |
| 128 | + min_metric = *std::min_element(alpha_curr.begin(), alpha_curr.end()); |
| 129 | + std::transform(alpha_curr.begin(), alpha_curr.end(), alpha_curr.begin(), |
| 130 | + std::bind2nd(std::minus<double>(), min_metric)); |
| 131 | + |
| 132 | + //At this point, current path metrics becomes previous path metrics |
| 133 | + alpha_prev.swap(alpha_curr); |
| 134 | + } |
| 135 | + |
| 136 | + //If final state was specified |
| 137 | + if(SK != -1) { |
| 138 | + tb_state = SK; |
| 139 | + } |
| 140 | + else{ |
| 141 | + //at this point, alpha_prev contains the path metrics of states after time K |
| 142 | + tb_state = (int)(min_element(alpha_prev.begin(), alpha_prev.end()) - alpha_prev.begin()); |
| 143 | + } |
| 144 | + |
| 145 | + //Traceback |
| 146 | + trace_it = trace.end() - S; //place trace_it at the last time index |
| 147 | + |
| 148 | + for(unsigned char* out_k = out+K-1 ; out_k >= out ; --out_k) { |
| 149 | + //Retrieve previous input index from trace |
| 150 | + pidx=*(trace_it + tb_state); |
| 151 | + //Update trace_it for next output symbol |
| 152 | + trace_it -= S; |
| 153 | + |
| 154 | + //Output previous input |
| 155 | + *out_k = (unsigned char) PI[tb_state][pidx]; |
| 156 | + |
| 157 | + //Update tb_state with the previous state on the shortest path |
| 158 | + tb_state = PS[tb_state][pidx]; |
| 159 | + } |
| 160 | +} |
0 commit comments