libsent/src/phmm/addlog.c

Go to the documentation of this file.
00001 
00017 /*
00018  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00019  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00020  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology
00021  * All rights reserved
00022  */
00023 
00024 #include <sent/stddefs.h>
00025 #include <sent/hmm.h>
00026 
00027 #define TBLSIZE 500000          
00028 #define VRANGE 15               
00029 #define TMAG 33333.3333         
00030 
00031 static LOGPROB tbl[TBLSIZE];    
00032 static boolean built_tbl = FALSE;
00033 
00040 void
00041 make_log_tbl()
00042 {
00043   LOGPROB f;
00044   int i;
00045 
00046   if (built_tbl == FALSE) {
00047     j_printerr("Generating addlog table...");
00048     for (i=0;i<TBLSIZE;i++){
00049       f = - ((float)VRANGE * (float)i / (float)TBLSIZE);
00050       tbl[i] = log(1 + exp(f));
00051       /*if (i < 10 || i > TBLSIZE - 10) j_printf("%f: %d(%f)\n", f, i, tbl[i]);*/
00052     }
00053     j_printerr("%d kb...done\n",(TBLSIZE * sizeof(LOGPROB)) / 1024);
00054     built_tbl = TRUE;
00055   }
00056 }
00057 
00069 LOGPROB
00070 addlog(LOGPROB x, LOGPROB y)
00071 {
00072   /* return(log(exp(x)+exp(y))) */
00073   LOGPROB tmp;
00074   unsigned int idx;
00075   
00076   if (x < y) {
00077     if ((tmp = x - y) < LOG_ADDMIN) return y;
00078     else {
00079       idx = (unsigned int)((- tmp) * TMAG + 0.5);
00080       /* j_printf("%f == %f\n",tbl[idx],log(1 + exp(tmp))); */
00081       return (y + tbl[idx]);
00082     }
00083   } else {
00084     if ((tmp = y - x) < LOG_ADDMIN) return x;
00085     else {
00086       idx =(unsigned int)((- tmp) * TMAG + 0.5);
00087       /* j_printf("%f == %f\n",tbl[idx],log(1 + exp(tmp))); */
00088       return (x + tbl[idx]);
00089     }
00090   }
00091 }
00092 
00101 LOGPROB
00102 addlog_array(LOGPROB *a, int n)
00103 {
00104   LOGPROB tmp;
00105   LOGPROB x,y;
00106   unsigned int idx;
00107 
00108   y = LOG_ZERO;
00109   for(n--; n >= 0; n--) {
00110     x = a[n];
00111     if (x > y) {
00112       tmp = x; x = y; y = tmp;
00113     }
00114     /* always y >= x */
00115     if ((tmp = x - y) < LOG_ADDMIN) continue;
00116     else {
00117       idx = (unsigned int)((- tmp) * TMAG + 0.5);
00118       y += tbl[idx];
00119     }
00120   }
00121   return(y);
00122 }

Generated on Tue Dec 26 16:16:33 2006 for Julius by  doxygen 1.5.0