From ea76761275b68744f05550ddab612f551baad75a Mon Sep 17 00:00:00 2001 From: Achilleas Anastasopoulos Date: Fri, 18 Feb 2011 20:35:23 -0500 Subject: core algorithms such as viterbi/siso were refactored and implemented using templates. Minor renaming of some files --- gr-trellis/src/lib/core_algorithms.cc | 710 ++++++++++++++++++++++++++++++++++ 1 file changed, 710 insertions(+) create mode 100644 gr-trellis/src/lib/core_algorithms.cc (limited to 'gr-trellis/src/lib/core_algorithms.cc') diff --git a/gr-trellis/src/lib/core_algorithms.cc b/gr-trellis/src/lib/core_algorithms.cc new file mode 100644 index 000000000..41ecaf174 --- /dev/null +++ b/gr-trellis/src/lib/core_algorithms.cc @@ -0,0 +1,710 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include "core_algorithms.h" +#include "calc_metric.h" + +static const float INF = 1.0e9; + +template +void viterbi_algorithm(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + const float *in, T *out)//, + //std::vector &trace) +{ + std::vector trace(S*K); + std::vector alpha(S*2); + int alphai; + float norm,mm,minm; + int minmi; + int st; + + + if(S0<0) { // initial state not specified + for(int i=0;i=0;k--) { // traceback + int i0=trace[k*S+st]; + out[k]= (T) PI[st][i0]; + st=PS[st][i0]; + } + +} + + +template +void viterbi_algorithm(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + const float *in, unsigned char *out); + + +template +void viterbi_algorithm(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + const float *in, short *out); + +template +void viterbi_algorithm(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + const float *in, int *out); + + + +//============================================== + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const Ti *in, To *out +) +{ + std::vector trace(S*K); + std::vector alpha(S*2); + float *metric = new float[O]; + int alphai; + float norm,mm,minm; + int minmi; + int st; + + if(S0<0) { // initial state not specified + for(int i=0;i=0;k--) { // traceback + int i0=trace[k*S+st]; + out[k]= (To) PI[st][i0]; + st=PS[st][i0]; + } + + delete [] metric; + +} + +// Ti = s i f c +// To = b s i + +//--------------- + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const short *in, unsigned char *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const int *in, unsigned char *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const float *in, unsigned char *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const gr_complex *in, unsigned char *out +); + +//--------------- + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const short *in, short *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const int *in, short *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const float *in, short *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const gr_complex *in, short *out +); + +//-------------- + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const short *in, int *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const int *in, int *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const float *in, int *out +); + +template +void viterbi_algorithm_combined(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const gr_complex *in, int *out +); + + + + + + + + + + + + + + + + + + + + +//=============================================== + +inline float min(float a, float b) +{ + return a <= b ? a : b; +} + +inline float min_star(float a, float b) +{ + return (a <= b ? a : b)-log(1+exp(a <= b ? a-b : b-a)); + //return 0; +} + +void siso_algorithm(int I, int S, int O, + const std::vector &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + bool POSTI, bool POSTO, + float (*p2mymin)(float,float), + const float *priori, const float *prioro, float *post//, + //std::vector &alpha, + //std::vector &beta + ) +{ + float norm,mm,minm; + std::vector alpha(S*(K+1)); + std::vector beta(S*(K+1)); + + + if(S0<0) { // initial state not specified + for(int i=0;i=0;k--) { // backward recursion + norm=INF; + for(int j=0;j &NS, + const std::vector &OS, + const std::vector< std::vector > &PS, + const std::vector< std::vector > &PI, + int K, + int S0,int SK, + bool POSTI, bool POSTO, + float (*p2mymin)(float,float), + int D, + const std::vector &TABLE, + trellis_metric_type_t TYPE, + const float *priori, const float *observations, float *post//, + //std::vector &alpha, + //std::vector &beta + ) +{ + float norm,mm,minm; + std::vector alpha(S*(K+1)); + std::vector beta(S*(K+1)); + float *prioro = new float[O*K]; + + + if(S0<0) { // initial state not specified + for(int i=0;i=0;k--) { // backward recursion + norm=INF; + for(int j=0;j