Sophie

Sophie

distrib > Fedora > 14 > i386 > by-pkgid > e9280da098bff237733732ce38a34d57 > files > 143

pocketsphinx-devel-0.7-1.fc14.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>PocketSphinx: src/libpocketsphinx/ms_gauden.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">PocketSphinx&#160;<span id="projectnumber">0.6</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('ms__gauden_8c.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<h1>src/libpocketsphinx/ms_gauden.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */</span>
<a name="l00002"></a>00002 <span class="comment">/* ====================================================================</span>
<a name="l00003"></a>00003 <span class="comment"> * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights</span>
<a name="l00004"></a>00004 <span class="comment"> * reserved.</span>
<a name="l00005"></a>00005 <span class="comment"> *</span>
<a name="l00006"></a>00006 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<a name="l00007"></a>00007 <span class="comment"> * modification, are permitted provided that the following conditions</span>
<a name="l00008"></a>00008 <span class="comment"> * are met:</span>
<a name="l00009"></a>00009 <span class="comment"> *</span>
<a name="l00010"></a>00010 <span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
<a name="l00011"></a>00011 <span class="comment"> *    notice, this list of conditions and the following disclaimer. </span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<a name="l00014"></a>00014 <span class="comment"> *    notice, this list of conditions and the following disclaimer in</span>
<a name="l00015"></a>00015 <span class="comment"> *    the documentation and/or other materials provided with the</span>
<a name="l00016"></a>00016 <span class="comment"> *    distribution.</span>
<a name="l00017"></a>00017 <span class="comment"> *</span>
<a name="l00018"></a>00018 <span class="comment"> * This work was supported in part by funding from the Defense Advanced </span>
<a name="l00019"></a>00019 <span class="comment"> * Research Projects Agency and the National Science Foundation of the </span>
<a name="l00020"></a>00020 <span class="comment"> * United States of America, and the CMU Sphinx Speech Consortium.</span>
<a name="l00021"></a>00021 <span class="comment"> *</span>
<a name="l00022"></a>00022 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS&#39;&#39; AND </span>
<a name="l00023"></a>00023 <span class="comment"> * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, </span>
<a name="l00024"></a>00024 <span class="comment"> * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR</span>
<a name="l00025"></a>00025 <span class="comment"> * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY</span>
<a name="l00026"></a>00026 <span class="comment"> * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00027"></a>00027 <span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT </span>
<a name="l00028"></a>00028 <span class="comment"> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, </span>
<a name="l00029"></a>00029 <span class="comment"> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY </span>
<a name="l00030"></a>00030 <span class="comment"> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT </span>
<a name="l00031"></a>00031 <span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE </span>
<a name="l00032"></a>00032 <span class="comment"> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00033"></a>00033 <span class="comment"> *</span>
<a name="l00034"></a>00034 <span class="comment"> * ====================================================================</span>
<a name="l00035"></a>00035 <span class="comment"> *</span>
<a name="l00036"></a>00036 <span class="comment"> */</span>
<a name="l00037"></a>00037 <span class="comment">/*</span>
<a name="l00038"></a>00038 <span class="comment"> * gauden.c -- gaussian density module.</span>
<a name="l00039"></a>00039 <span class="comment"> *</span>
<a name="l00040"></a>00040 <span class="comment"> ***********************************************</span>
<a name="l00041"></a>00041 <span class="comment"> * CMU ARPA Speech Project</span>
<a name="l00042"></a>00042 <span class="comment"> *</span>
<a name="l00043"></a>00043 <span class="comment"> * Copyright (c) 1996 Carnegie Mellon University.</span>
<a name="l00044"></a>00044 <span class="comment"> * ALL RIGHTS RESERVED.</span>
<a name="l00045"></a>00045 <span class="comment"> ***********************************************</span>
<a name="l00046"></a>00046 <span class="comment"> *</span>
<a name="l00047"></a>00047 <span class="comment"> * HISTORY</span>
<a name="l00048"></a>00048 <span class="comment"> * $Log$</span>
<a name="l00049"></a>00049 <span class="comment"> * Revision 1.7  2006/02/22  17:09:55  arthchan2003</span>
<a name="l00050"></a>00050 <span class="comment"> * Merged from SPHINX3_5_2_RCI_IRII_BRANCH: 1, Followed Dave&#39;s change, keep active to be uint8 instead int8 in gauden_dist_norm.\n 2, Introdued gauden_dump and gauden_dump_ind.  This allows debugging of ms_gauden routine. \n 3, Introduced gauden_free, this fixed some minor memory leaks. \n 4, gauden_init accept an argument precompute to specify whether the distance is pre-computed or not.\n 5, Added license. \n 6, Fixed dox-doc.</span>
<a name="l00051"></a>00051 <span class="comment"> * </span>
<a name="l00052"></a>00052 <span class="comment"> *</span>
<a name="l00053"></a>00053 <span class="comment"> * Revision 1.5.4.7  2006/01/16 19:45:59  arthchan2003</span>
<a name="l00054"></a>00054 <span class="comment"> * Change the gaussian density dumping routine to a function.</span>
<a name="l00055"></a>00055 <span class="comment"> *</span>
<a name="l00056"></a>00056 <span class="comment"> * Revision 1.5.4.6  2005/10/09 19:51:05  arthchan2003</span>
<a name="l00057"></a>00057 <span class="comment"> * Followed Dave&#39;s changed in the trunk.</span>
<a name="l00058"></a>00058 <span class="comment"> *</span>
<a name="l00059"></a>00059 <span class="comment"> * Revision 1.5.4.5  2005/09/25 18:54:20  arthchan2003</span>
<a name="l00060"></a>00060 <span class="comment"> * Added a flag to turn on and off precomputation.</span>
<a name="l00061"></a>00061 <span class="comment"> *</span>
<a name="l00062"></a>00062 <span class="comment"> * Revision 1.6  2005/10/05 00:31:14  dhdfu</span>
<a name="l00063"></a>00063 <span class="comment"> * Make int8 be explicitly signed (signedness of &#39;char&#39; is</span>
<a name="l00064"></a>00064 <span class="comment"> * architecture-dependent).  Then make a bunch of things use uint8 where</span>
<a name="l00065"></a>00065 <span class="comment"> * signedness is unimportant, because on the architecture where &#39;char&#39; is</span>
<a name="l00066"></a>00066 <span class="comment"> * unsigned, it is that way for a reason (signed chars are slower).</span>
<a name="l00067"></a>00067 <span class="comment"> *</span>
<a name="l00068"></a>00068 <span class="comment"> * Revision 1.5.4.4  2005/09/07 23:29:07  arthchan2003</span>
<a name="l00069"></a>00069 <span class="comment"> * Added FIXME warning.</span>
<a name="l00070"></a>00070 <span class="comment"> *</span>
<a name="l00071"></a>00071 <span class="comment"> * Revision 1.5.4.3  2005/09/07 23:25:10  arthchan2003</span>
<a name="l00072"></a>00072 <span class="comment"> * 1, Behavior changes of cont_mgau, instead of remove Gaussian with zero variance vector before flooring, now remove Gaussian with zero mean and variance before flooring. Notice that this is not yet synchronize with ms_mgau. 2, Added warning message in multi-stream gaussian distribution.</span>
<a name="l00073"></a>00073 <span class="comment"> *</span>
<a name="l00074"></a>00074 <span class="comment"> * Revision 1.5.4.2  2005/08/03 18:53:44  dhdfu</span>
<a name="l00075"></a>00075 <span class="comment"> * Add memory deallocation functions.  Also move all the initialization</span>
<a name="l00076"></a>00076 <span class="comment"> * of ms_mgau_model_t into ms_mgau_init (duh!), which entails removing it</span>
<a name="l00077"></a>00077 <span class="comment"> * from decode_anytopo and friends.</span>
<a name="l00078"></a>00078 <span class="comment"> *</span>
<a name="l00079"></a>00079 <span class="comment"> * Revision 1.5.4.1  2005/07/20 19:39:01  arthchan2003</span>
<a name="l00080"></a>00080 <span class="comment"> * Added licences in ms_* series of code.</span>
<a name="l00081"></a>00081 <span class="comment"> *</span>
<a name="l00082"></a>00082 <span class="comment"> * Revision 1.5  2005/06/21 18:55:09  arthchan2003</span>
<a name="l00083"></a>00083 <span class="comment"> * 1, Add comments to describe this modules, 2, Fixed doxygen documentation. 3, Added $ keyword.</span>
<a name="l00084"></a>00084 <span class="comment"> *</span>
<a name="l00085"></a>00085 <span class="comment"> * Revision 1.3  2005/03/30 01:22:47  archan</span>
<a name="l00086"></a>00086 <span class="comment"> * Fixed mistakes in last updates. Add</span>
<a name="l00087"></a>00087 <span class="comment"> *</span>
<a name="l00088"></a>00088 <span class="comment"> * </span>
<a name="l00089"></a>00089 <span class="comment"> * 20-Dec-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00090"></a>00090 <span class="comment"> *              Changed gauden_param_read to use the new libio/bio_fread functions.</span>
<a name="l00091"></a>00091 <span class="comment"> * </span>
<a name="l00092"></a>00092 <span class="comment"> * 26-Sep-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00093"></a>00093 <span class="comment"> *              Added gauden_mean_reload() for application of MLLR; and correspondingly</span>
<a name="l00094"></a>00094 <span class="comment"> *              made gauden_param_read allocate memory for parameter only if not</span>
<a name="l00095"></a>00095 <span class="comment"> *              already allocated.</span>
<a name="l00096"></a>00096 <span class="comment"> * </span>
<a name="l00097"></a>00097 <span class="comment"> * 09-Sep-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00098"></a>00098 <span class="comment"> *              Interleaved two density computations for speed improvement.</span>
<a name="l00099"></a>00099 <span class="comment"> * </span>
<a name="l00100"></a>00100 <span class="comment"> * 19-Aug-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00101"></a>00101 <span class="comment"> *              Added compute_dist_all special case for improving speed.</span>
<a name="l00102"></a>00102 <span class="comment"> * </span>
<a name="l00103"></a>00103 <span class="comment"> * 26-Jan-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00104"></a>00104 <span class="comment"> *              Added check for underflow and floor insertion in gauden_dist.</span>
<a name="l00105"></a>00105 <span class="comment"> * </span>
<a name="l00106"></a>00106 <span class="comment"> * 20-Jan-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00107"></a>00107 <span class="comment"> *              Added active argument to gauden_dist_norm and gauden_dist_norm_global,</span>
<a name="l00108"></a>00108 <span class="comment"> *              and made the latter a static function.</span>
<a name="l00109"></a>00109 <span class="comment"> * </span>
<a name="l00110"></a>00110 <span class="comment"> * 07-Nov-95    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.</span>
<a name="l00111"></a>00111 <span class="comment"> *              Initial version created.</span>
<a name="l00112"></a>00112 <span class="comment"> *              Very liberally borrowed/adapted from Eric&#39;s S3 trainer implementation.</span>
<a name="l00113"></a>00113 <span class="comment"> */</span>
<a name="l00114"></a>00114 
<a name="l00115"></a>00115 <span class="comment">/* System headers. */</span>
<a name="l00116"></a>00116 <span class="preprocessor">#include &lt;assert.h&gt;</span>
<a name="l00117"></a>00117 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00118"></a>00118 <span class="preprocessor">#include &lt;math.h&gt;</span>
<a name="l00119"></a>00119 <span class="preprocessor">#include &lt;float.h&gt;</span>
<a name="l00120"></a>00120 
<a name="l00121"></a>00121 <span class="comment">/* SphinxBase headers. */</span>
<a name="l00122"></a>00122 <span class="preprocessor">#include &lt;sphinxbase/bio.h&gt;</span>
<a name="l00123"></a>00123 <span class="preprocessor">#include &lt;sphinxbase/err.h&gt;</span>
<a name="l00124"></a>00124 <span class="preprocessor">#include &lt;sphinxbase/ckd_alloc.h&gt;</span>
<a name="l00125"></a>00125 
<a name="l00126"></a>00126 <span class="comment">/* Local headesr. */</span>
<a name="l00127"></a>00127 <span class="preprocessor">#include &quot;<a class="code" href="ms__gauden_8h.html" title="(Sphinx 3.0 specific) Gaussian density module.">ms_gauden.h</a>&quot;</span>
<a name="l00128"></a>00128 
<a name="l00129"></a>00129 <span class="preprocessor">#define GAUDEN_PARAM_VERSION    &quot;1.0&quot;</span>
<a name="l00130"></a>00130 <span class="preprocessor"></span>
<a name="l00131"></a>00131 <span class="preprocessor">#ifndef M_PI</span>
<a name="l00132"></a>00132 <span class="preprocessor"></span><span class="preprocessor">#define M_PI    3.1415926535897932385e0</span>
<a name="l00133"></a>00133 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00134"></a>00134 <span class="preprocessor"></span>
<a name="l00135"></a>00135 <span class="preprocessor">#define WORST_DIST      (int32)(0x80000000)</span>
<a name="l00136"></a>00136 <span class="preprocessor"></span>
<a name="l00137"></a>00137 <span class="keywordtype">void</span>
<a name="l00138"></a><a class="code" href="ms__gauden_8h.html#a8efb2c45fd5ac2b34bbff067a15fe6f5">00138</a> gauden_dump(<span class="keyword">const</span> <a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> * g)
<a name="l00139"></a>00139 {
<a name="l00140"></a>00140     int32 c;
<a name="l00141"></a>00141 
<a name="l00142"></a>00142     <span class="keywordflow">for</span> (c = 0; c &lt; g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>; c++)
<a name="l00143"></a>00143         gauden_dump_ind(g, c);
<a name="l00144"></a>00144 }
<a name="l00145"></a>00145 
<a name="l00146"></a>00146 
<a name="l00147"></a>00147 <span class="keywordtype">void</span>
<a name="l00148"></a><a class="code" href="ms__gauden_8h.html#a9489d75ad3dbdc2bb413b43a17f48188">00148</a> gauden_dump_ind(<span class="keyword">const</span> <a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> * g, <span class="keywordtype">int</span> senidx)
<a name="l00149"></a>00149 {
<a name="l00150"></a>00150     int32 f, d, i;
<a name="l00151"></a>00151 
<a name="l00152"></a>00152     <span class="keywordflow">for</span> (f = 0; f &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; f++) {
<a name="l00153"></a>00153         E_INFO(<span class="stringliteral">&quot;Codebook %d, Feature %d (%dx%d):\n&quot;</span>,
<a name="l00154"></a>00154                senidx, f, g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>, g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]);
<a name="l00155"></a>00155 
<a name="l00156"></a>00156         <span class="keywordflow">for</span> (d = 0; d &lt; g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>; d++) {
<a name="l00157"></a>00157             printf(<span class="stringliteral">&quot;m[%3d]&quot;</span>, d);
<a name="l00158"></a>00158             <span class="keywordflow">for</span> (i = 0; i &lt; g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]; i++)
<a name="l00159"></a>00159                 printf(<span class="stringliteral">&quot; %7.4f&quot;</span>, MFCC2FLOAT(g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>[senidx][f][d][i]));
<a name="l00160"></a>00160             printf(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00161"></a>00161         }
<a name="l00162"></a>00162         printf(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00163"></a>00163 
<a name="l00164"></a>00164         <span class="keywordflow">for</span> (d = 0; d &lt; g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>; d++) {
<a name="l00165"></a>00165             printf(<span class="stringliteral">&quot;v[%3d]&quot;</span>, d);
<a name="l00166"></a>00166             <span class="keywordflow">for</span> (i = 0; i &lt; g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]; i++)
<a name="l00167"></a>00167                 printf(<span class="stringliteral">&quot; %d&quot;</span>, (<span class="keywordtype">int</span>)g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>[senidx][f][d][i]);
<a name="l00168"></a>00168             printf(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00169"></a>00169         }
<a name="l00170"></a>00170         printf(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00171"></a>00171 
<a name="l00172"></a>00172         <span class="keywordflow">for</span> (d = 0; d &lt; g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>; d++)
<a name="l00173"></a>00173             printf(<span class="stringliteral">&quot;d[%3d] %d\n&quot;</span>, d, (<span class="keywordtype">int</span>)g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>[senidx][f][d]);
<a name="l00174"></a>00174     }
<a name="l00175"></a>00175     fflush(stderr);
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177 
<a name="l00178"></a>00178 <span class="keyword">static</span> int32
<a name="l00179"></a>00179 gauden_param_read(float32 ***** out_param,      <span class="comment">/* Alloc space iff *out_param == NULL */</span>
<a name="l00180"></a>00180                   int32 * out_n_mgau,
<a name="l00181"></a>00181                   int32 * out_n_feat,
<a name="l00182"></a>00182                   int32 * out_n_density,
<a name="l00183"></a>00183                   int32 ** out_veclen, <span class="keyword">const</span> <span class="keywordtype">char</span> *file_name)
<a name="l00184"></a>00184 {
<a name="l00185"></a>00185     <span class="keywordtype">char</span> tmp;
<a name="l00186"></a>00186     FILE *fp;
<a name="l00187"></a>00187     int32 i, j, k, l, n, blk;
<a name="l00188"></a>00188     int32 n_mgau;
<a name="l00189"></a>00189     int32 n_feat;
<a name="l00190"></a>00190     int32 n_density;
<a name="l00191"></a>00191     int32 *veclen;
<a name="l00192"></a>00192     int32 byteswap, chksum_present;
<a name="l00193"></a>00193     float32 ****out;
<a name="l00194"></a>00194     float32 *buf;
<a name="l00195"></a>00195     <span class="keywordtype">char</span> **argname, **argval;
<a name="l00196"></a>00196     uint32 chksum;
<a name="l00197"></a>00197 
<a name="l00198"></a>00198     E_INFO(<span class="stringliteral">&quot;Reading mixture gaussian parameter: %s\n&quot;</span>, file_name);
<a name="l00199"></a>00199 
<a name="l00200"></a>00200     <span class="keywordflow">if</span> ((fp = fopen(file_name, <span class="stringliteral">&quot;rb&quot;</span>)) == NULL)
<a name="l00201"></a>00201         E_FATAL_SYSTEM(<span class="stringliteral">&quot;Failed to open file &#39;%s&#39; for reading&quot;</span>, file_name);
<a name="l00202"></a>00202 
<a name="l00203"></a>00203     <span class="comment">/* Read header, including argument-value info and 32-bit byteorder magic */</span>
<a name="l00204"></a>00204     <span class="keywordflow">if</span> (bio_readhdr(fp, &amp;argname, &amp;argval, &amp;byteswap) &lt; 0)
<a name="l00205"></a>00205         E_FATAL(<span class="stringliteral">&quot;Failed to read header from file &#39;%s&#39;\n&quot;</span>, file_name);
<a name="l00206"></a>00206 
<a name="l00207"></a>00207     <span class="comment">/* Parse argument-value list */</span>
<a name="l00208"></a>00208     chksum_present = 0;
<a name="l00209"></a>00209     <span class="keywordflow">for</span> (i = 0; argname[i]; i++) {
<a name="l00210"></a>00210         <span class="keywordflow">if</span> (strcmp(argname[i], <span class="stringliteral">&quot;version&quot;</span>) == 0) {
<a name="l00211"></a>00211             <span class="keywordflow">if</span> (strcmp(argval[i], GAUDEN_PARAM_VERSION) != 0)
<a name="l00212"></a>00212                 E_WARN(<span class="stringliteral">&quot;Version mismatch(%s): %s, expecting %s\n&quot;</span>,
<a name="l00213"></a>00213                        file_name, argval[i], GAUDEN_PARAM_VERSION);
<a name="l00214"></a>00214         }
<a name="l00215"></a>00215         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argname[i], <span class="stringliteral">&quot;chksum0&quot;</span>) == 0) {
<a name="l00216"></a>00216             chksum_present = 1; <span class="comment">/* Ignore the associated value */</span>
<a name="l00217"></a>00217         }
<a name="l00218"></a>00218     }
<a name="l00219"></a>00219     bio_hdrarg_free(argname, argval);
<a name="l00220"></a>00220     argname = argval = NULL;
<a name="l00221"></a>00221 
<a name="l00222"></a>00222     chksum = 0;
<a name="l00223"></a>00223 
<a name="l00224"></a>00224     <span class="comment">/* #Codebooks */</span>
<a name="l00225"></a>00225     <span class="keywordflow">if</span> (bio_fread(&amp;n_mgau, <span class="keyword">sizeof</span>(int32), 1, fp, byteswap, &amp;chksum) != 1)
<a name="l00226"></a>00226         E_FATAL(<span class="stringliteral">&quot;fread(%s) (#codebooks) failed\n&quot;</span>, file_name);
<a name="l00227"></a>00227     *out_n_mgau = n_mgau;
<a name="l00228"></a>00228 
<a name="l00229"></a>00229     <span class="comment">/* #Features/codebook */</span>
<a name="l00230"></a>00230     <span class="keywordflow">if</span> (bio_fread(&amp;n_feat, <span class="keyword">sizeof</span>(int32), 1, fp, byteswap, &amp;chksum) != 1)
<a name="l00231"></a>00231         E_FATAL(<span class="stringliteral">&quot;fread(%s) (#features) failed\n&quot;</span>, file_name);
<a name="l00232"></a>00232     *out_n_feat = n_feat;
<a name="l00233"></a>00233 
<a name="l00234"></a>00234     <span class="comment">/* #Gaussian densities/feature in each codebook */</span>
<a name="l00235"></a>00235     <span class="keywordflow">if</span> (bio_fread(&amp;n_density, <span class="keyword">sizeof</span>(int32), 1, fp, byteswap, &amp;chksum) != 1)
<a name="l00236"></a>00236         E_FATAL(<span class="stringliteral">&quot;fread(%s) (#density/codebook) failed\n&quot;</span>, file_name);
<a name="l00237"></a>00237     *out_n_density = n_density;
<a name="l00238"></a>00238 
<a name="l00239"></a>00239     <span class="comment">/* #Dimensions in each feature stream */</span>
<a name="l00240"></a>00240     veclen = ckd_calloc(n_feat, <span class="keyword">sizeof</span>(uint32));
<a name="l00241"></a>00241     *out_veclen = veclen;
<a name="l00242"></a>00242     <span class="keywordflow">if</span> (bio_fread(veclen, <span class="keyword">sizeof</span>(int32), n_feat, fp, byteswap, &amp;chksum) !=
<a name="l00243"></a>00243         n_feat)
<a name="l00244"></a>00244         E_FATAL(<span class="stringliteral">&quot;fread(%s) (feature-lengths) failed\n&quot;</span>, file_name);
<a name="l00245"></a>00245 
<a name="l00246"></a>00246     <span class="comment">/* blk = total vector length of all feature streams */</span>
<a name="l00247"></a>00247     <span class="keywordflow">for</span> (i = 0, blk = 0; i &lt; n_feat; i++)
<a name="l00248"></a>00248         blk += veclen[i];
<a name="l00249"></a>00249 
<a name="l00250"></a>00250     <span class="comment">/* #Floats to follow; for the ENTIRE SET of CODEBOOKS */</span>
<a name="l00251"></a>00251     <span class="keywordflow">if</span> (bio_fread(&amp;n, <span class="keyword">sizeof</span>(int32), 1, fp, byteswap, &amp;chksum) != 1)
<a name="l00252"></a>00252         E_FATAL(<span class="stringliteral">&quot;fread(%s) (total #floats) failed\n&quot;</span>, file_name);
<a name="l00253"></a>00253     <span class="keywordflow">if</span> (n != n_mgau * n_density * blk) {
<a name="l00254"></a>00254         E_FATAL
<a name="l00255"></a>00255             (<span class="stringliteral">&quot;%s: #mfcc_ts(%d) doesn&#39;t match dimensions: %d x %d x %d\n&quot;</span>,
<a name="l00256"></a>00256              file_name, n, n_mgau, n_density, blk);
<a name="l00257"></a>00257     }
<a name="l00258"></a>00258 
<a name="l00259"></a>00259     <span class="comment">/* Allocate memory for mixture gaussian densities if not already allocated */</span>
<a name="l00260"></a>00260     <span class="keywordflow">if</span> (!(*out_param)) {
<a name="l00261"></a>00261         out = (float32 ****) ckd_calloc_3d(n_mgau, n_feat, n_density,
<a name="l00262"></a>00262                                          <span class="keyword">sizeof</span>(float32 *));
<a name="l00263"></a>00263         buf = (float32 *) ckd_calloc(n, <span class="keyword">sizeof</span>(float32));
<a name="l00264"></a>00264         <span class="keywordflow">for</span> (i = 0, l = 0; i &lt; n_mgau; i++) {
<a name="l00265"></a>00265             <span class="keywordflow">for</span> (j = 0; j &lt; n_feat; j++) {
<a name="l00266"></a>00266                 <span class="keywordflow">for</span> (k = 0; k &lt; n_density; k++) {
<a name="l00267"></a>00267                     out[i][j][k] = &amp;buf[l];
<a name="l00268"></a>00268                     l += veclen[j];
<a name="l00269"></a>00269                 }
<a name="l00270"></a>00270             }
<a name="l00271"></a>00271         }
<a name="l00272"></a>00272     }
<a name="l00273"></a>00273     <span class="keywordflow">else</span> {
<a name="l00274"></a>00274         out = (float32 ****) *out_param;
<a name="l00275"></a>00275         buf = out[0][0][0];
<a name="l00276"></a>00276     }
<a name="l00277"></a>00277 
<a name="l00278"></a>00278     <span class="comment">/* Read mixture gaussian densities data */</span>
<a name="l00279"></a>00279     <span class="keywordflow">if</span> (bio_fread(buf, <span class="keyword">sizeof</span>(float32), n, fp, byteswap, &amp;chksum) != n)
<a name="l00280"></a>00280         E_FATAL(<span class="stringliteral">&quot;fread(%s) (densitydata) failed\n&quot;</span>, file_name);
<a name="l00281"></a>00281 
<a name="l00282"></a>00282     <span class="keywordflow">if</span> (chksum_present)
<a name="l00283"></a>00283         bio_verify_chksum(fp, byteswap, chksum);
<a name="l00284"></a>00284 
<a name="l00285"></a>00285     <span class="keywordflow">if</span> (fread(&amp;tmp, 1, 1, fp) == 1)
<a name="l00286"></a>00286         E_FATAL(<span class="stringliteral">&quot;More data than expected in %s\n&quot;</span>, file_name);
<a name="l00287"></a>00287 
<a name="l00288"></a>00288     fclose(fp);
<a name="l00289"></a>00289 
<a name="l00290"></a>00290     *out_param = out;
<a name="l00291"></a>00291 
<a name="l00292"></a>00292     E_INFO(<span class="stringliteral">&quot;%d codebook, %d feature, size: \n&quot;</span>, n_mgau, n_feat);
<a name="l00293"></a>00293     <span class="keywordflow">for</span> (i = 0; i &lt; n_feat; i++)
<a name="l00294"></a>00294         E_INFO(<span class="stringliteral">&quot; %dx%d\n&quot;</span>, n_density, veclen[i]);
<a name="l00295"></a>00295 
<a name="l00296"></a>00296     <span class="keywordflow">return</span> 0;
<a name="l00297"></a>00297 }
<a name="l00298"></a>00298 
<a name="l00299"></a>00299 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00300"></a>00300 gauden_param_free(mfcc_t **** p)
<a name="l00301"></a>00301 {
<a name="l00302"></a>00302     ckd_free(p[0][0][0]);
<a name="l00303"></a>00303     ckd_free_3d(p);
<a name="l00304"></a>00304 }
<a name="l00305"></a>00305 
<a name="l00306"></a>00306 <span class="comment">/*</span>
<a name="l00307"></a>00307 <span class="comment"> * Some of the gaussian density computation can be carried out in advance:</span>
<a name="l00308"></a>00308 <span class="comment"> *      log(determinant) calculation,</span>
<a name="l00309"></a>00309 <span class="comment"> *      1/(2*var) in the exponent,</span>
<a name="l00310"></a>00310 <span class="comment"> * NOTE; The density computation is performed in log domain.</span>
<a name="l00311"></a>00311 <span class="comment"> */</span>
<a name="l00312"></a>00312 <span class="keyword">static</span> int32
<a name="l00313"></a>00313 gauden_dist_precompute(<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> * g, logmath_t *lmath, float32 varfloor)
<a name="l00314"></a>00314 {
<a name="l00315"></a>00315     int32 i, m, f, d, flen;
<a name="l00316"></a>00316     mfcc_t *meanp;
<a name="l00317"></a>00317     mfcc_t *varp;
<a name="l00318"></a>00318     mfcc_t *detp;
<a name="l00319"></a>00319     int32 floored;
<a name="l00320"></a>00320 
<a name="l00321"></a>00321     floored = 0;
<a name="l00322"></a>00322     <span class="comment">/* Allocate space for determinants */</span>
<a name="l00323"></a>00323     g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a> = ckd_calloc_3d(g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>, g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>, g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>, <span class="keyword">sizeof</span>(***g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>));
<a name="l00324"></a>00324 
<a name="l00325"></a>00325     <span class="keywordflow">for</span> (m = 0; m &lt; g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>; m++) {
<a name="l00326"></a>00326         <span class="keywordflow">for</span> (f = 0; f &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; f++) {
<a name="l00327"></a>00327             flen = g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f];
<a name="l00328"></a>00328 
<a name="l00329"></a>00329             <span class="comment">/* Determinants for all variance vectors in g-&gt;[m][f] */</span>
<a name="l00330"></a>00330             <span class="keywordflow">for</span> (d = 0, detp = g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>[m][f]; d &lt; g-&gt;n_density; d++, detp++) {
<a name="l00331"></a>00331                 *detp = 0;
<a name="l00332"></a>00332                 <span class="keywordflow">for</span> (i = 0, varp = g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>[m][f][d], meanp = g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>[m][f][d];
<a name="l00333"></a>00333                      i &lt; flen; i++, varp++, meanp++) {
<a name="l00334"></a>00334                     float32 *fvarp = (float32 *)varp;
<a name="l00335"></a>00335 
<a name="l00336"></a>00336 <span class="preprocessor">#ifdef FIXED_POINT</span>
<a name="l00337"></a>00337 <span class="preprocessor"></span>                    float32 *fmp = (float32 *)meanp;
<a name="l00338"></a>00338                     *meanp = FLOAT2MFCC(*fmp);
<a name="l00339"></a>00339 <span class="preprocessor">#endif</span>
<a name="l00340"></a>00340 <span class="preprocessor"></span>                    <span class="keywordflow">if</span> (*fvarp &lt; varfloor) {
<a name="l00341"></a>00341                         *fvarp = varfloor;
<a name="l00342"></a>00342                         ++floored;
<a name="l00343"></a>00343                     }
<a name="l00344"></a>00344                     *detp += (mfcc_t)logmath_log(lmath,
<a name="l00345"></a>00345                                                  1.0 / sqrt(*fvarp * 2.0 * M_PI));
<a name="l00346"></a>00346                     <span class="comment">/* Precompute this part of the exponential */</span>
<a name="l00347"></a>00347                     *varp = (mfcc_t)logmath_ln_to_log(lmath,
<a name="l00348"></a>00348                                                       (1.0 / (*fvarp * 2.0)));
<a name="l00349"></a>00349                 }
<a name="l00350"></a>00350             }
<a name="l00351"></a>00351         }
<a name="l00352"></a>00352     }
<a name="l00353"></a>00353 
<a name="l00354"></a>00354     E_INFO(<span class="stringliteral">&quot;%d variance values floored\n&quot;</span>, floored);
<a name="l00355"></a>00355 
<a name="l00356"></a>00356     <span class="keywordflow">return</span> 0;
<a name="l00357"></a>00357 }
<a name="l00358"></a>00358 
<a name="l00359"></a>00359 
<a name="l00360"></a>00360 <a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> *
<a name="l00361"></a><a class="code" href="ms__gauden_8h.html#ae4587b0aeb39fd926622a422969ecebc">00361</a> gauden_init(<span class="keywordtype">char</span> <span class="keyword">const</span> *meanfile, <span class="keywordtype">char</span> <span class="keyword">const</span> *varfile, float32 varfloor, logmath_t *lmath)
<a name="l00362"></a>00362 {
<a name="l00363"></a>00363     int32 i, m, f, d, *flen;
<a name="l00364"></a>00364     float32 ****fgau;
<a name="l00365"></a>00365     <a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> *g;
<a name="l00366"></a>00366 
<a name="l00367"></a>00367     assert(meanfile != NULL);
<a name="l00368"></a>00368     assert(varfile != NULL);
<a name="l00369"></a>00369     assert(varfloor &gt; 0.0);
<a name="l00370"></a>00370 
<a name="l00371"></a>00371     g = (<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> *) ckd_calloc(1, <span class="keyword">sizeof</span>(<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a>));
<a name="l00372"></a>00372     g-&gt;<a class="code" href="structgauden__t.html#ae05c9b82d8a586bea347681ebe04bb83" title="log math computation">lmath</a> = lmath;
<a name="l00373"></a>00373 
<a name="l00374"></a>00374     <span class="comment">/* Read means and (diagonal) variances for all mixture gaussians */</span>
<a name="l00375"></a>00375     fgau = NULL;
<a name="l00376"></a>00376     gauden_param_read(&amp;fgau, &amp;g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>, &amp;g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>, &amp;g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>,
<a name="l00377"></a>00377                       &amp;g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>, meanfile);
<a name="l00378"></a>00378     g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a> = (mfcc_t ****)fgau;
<a name="l00379"></a>00379     fgau = NULL;
<a name="l00380"></a>00380     gauden_param_read(&amp;fgau, &amp;m, &amp;f, &amp;d, &amp;flen, varfile);
<a name="l00381"></a>00381     g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a> = (mfcc_t ****)fgau;
<a name="l00382"></a>00382 
<a name="l00383"></a>00383     <span class="comment">/* Verify mean and variance parameter dimensions */</span>
<a name="l00384"></a>00384     <span class="keywordflow">if</span> ((m != g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>) || (f != g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>) || (d != g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>))
<a name="l00385"></a>00385         E_FATAL
<a name="l00386"></a>00386             (<span class="stringliteral">&quot;Mixture-gaussians dimensions for means and variances differ\n&quot;</span>);
<a name="l00387"></a>00387     <span class="keywordflow">for</span> (i = 0; i &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; i++)
<a name="l00388"></a>00388         <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[i] != flen[i])
<a name="l00389"></a>00389             E_FATAL(<span class="stringliteral">&quot;Feature lengths for means and variances differ\n&quot;</span>);
<a name="l00390"></a>00390     ckd_free(flen);
<a name="l00391"></a>00391 
<a name="l00392"></a>00392     <span class="comment">/* Floor variances and precompute variance determinants */</span>
<a name="l00393"></a>00393     gauden_dist_precompute(g, lmath, varfloor);
<a name="l00394"></a>00394 
<a name="l00395"></a>00395     <span class="keywordflow">return</span> g;
<a name="l00396"></a>00396 }
<a name="l00397"></a>00397 
<a name="l00398"></a>00398 <span class="keywordtype">void</span>
<a name="l00399"></a><a class="code" href="ms__gauden_8h.html#a0306e088fa33894ca40182d2392ffe4d">00399</a> gauden_free(<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> * g)
<a name="l00400"></a>00400 {
<a name="l00401"></a>00401     <span class="keywordflow">if</span> (g == NULL)
<a name="l00402"></a>00402         <span class="keywordflow">return</span>;
<a name="l00403"></a>00403     <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>)
<a name="l00404"></a>00404         gauden_param_free(g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>);
<a name="l00405"></a>00405     <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>)
<a name="l00406"></a>00406         gauden_param_free((mfcc_t ****)g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>);
<a name="l00407"></a>00407     <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>)
<a name="l00408"></a>00408         ckd_free_3d(g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>);
<a name="l00409"></a>00409     <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>)
<a name="l00410"></a>00410         ckd_free(g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>);
<a name="l00411"></a>00411     ckd_free(g);
<a name="l00412"></a>00412 }
<a name="l00413"></a>00413 
<a name="l00414"></a>00414 <span class="comment">/* See compute_dist below */</span>
<a name="l00415"></a>00415 <span class="keyword">static</span> int32
<a name="l00416"></a>00416 compute_dist_all(<a class="code" href="structgauden__dist__t.html" title="Structure to store distance (density) values for a given input observation wrt density values in some...">gauden_dist_t</a> * out_dist, mfcc_t* obs, int32 featlen,
<a name="l00417"></a>00417                  mfcc_t ** mean, mfcc_t ** var, mfcc_t * det,
<a name="l00418"></a>00418                  int32 n_density)
<a name="l00419"></a>00419 {
<a name="l00420"></a>00420     int32 i, d;
<a name="l00421"></a>00421 
<a name="l00422"></a>00422     <span class="keywordflow">for</span> (d = 0; d &lt; n_density; ++d) {
<a name="l00423"></a>00423         mfcc_t *m;
<a name="l00424"></a>00424         mfcc_t *v;
<a name="l00425"></a>00425         mfcc_t dval;
<a name="l00426"></a>00426 
<a name="l00427"></a>00427         m = mean[d];
<a name="l00428"></a>00428         v = var[d];
<a name="l00429"></a>00429         dval = det[d];
<a name="l00430"></a>00430 
<a name="l00431"></a>00431         <span class="keywordflow">for</span> (i = 0; i &lt; featlen; i++) {
<a name="l00432"></a>00432             mfcc_t diff;
<a name="l00433"></a>00433 <span class="preprocessor">#ifdef FIXED_POINT</span>
<a name="l00434"></a>00434 <span class="preprocessor"></span>            <span class="comment">/* Have to check for underflows here. */</span>
<a name="l00435"></a>00435             mfcc_t pdval = dval;
<a name="l00436"></a>00436             diff = obs[i] - m[i];
<a name="l00437"></a>00437             dval -= MFCCMUL(MFCCMUL(diff, diff), v[i]);
<a name="l00438"></a>00438             <span class="keywordflow">if</span> (dval &gt; pdval) {
<a name="l00439"></a>00439                 dval = <a class="code" href="hmm_8h.html#a833f100ee293ad0f1c03f2a5ef9be77a" title="Large &amp;quot;bad&amp;quot; score.">WORST_SCORE</a>;
<a name="l00440"></a>00440                 <span class="keywordflow">break</span>;
<a name="l00441"></a>00441             }
<a name="l00442"></a>00442 <span class="preprocessor">#else</span>
<a name="l00443"></a>00443 <span class="preprocessor"></span>            diff = obs[i] - m[i];
<a name="l00444"></a>00444             <span class="comment">/* The compiler really likes this to be a single</span>
<a name="l00445"></a>00445 <span class="comment">             * expression, for whatever reason. */</span>
<a name="l00446"></a>00446             dval -= diff * diff * v[i];
<a name="l00447"></a>00447 <span class="preprocessor">#endif</span>
<a name="l00448"></a>00448 <span class="preprocessor"></span>        }
<a name="l00449"></a>00449 
<a name="l00450"></a>00450         out_dist[d].<a class="code" href="structgauden__dist__t.html#ab08100d4953998dad76bfcf30864c0c5" title="Density value for input observation wrt above codeword; NOTE: result in logs3 domain, but var_t used for speed.">dist</a> = dval;
<a name="l00451"></a>00451         out_dist[d].<a class="code" href="structgauden__dist__t.html#a93e978149bae5b5bf089769458c42ee4" title="Index of codeword (gaussian density)">id</a> = d;
<a name="l00452"></a>00452     }
<a name="l00453"></a>00453 
<a name="l00454"></a>00454     <span class="keywordflow">return</span> 0;
<a name="l00455"></a>00455 }
<a name="l00456"></a>00456 
<a name="l00457"></a>00457 
<a name="l00458"></a>00458 <span class="comment">/*</span>
<a name="l00459"></a>00459 <span class="comment"> * Compute the top-N closest gaussians from the chosen set (mgau,feat)</span>
<a name="l00460"></a>00460 <span class="comment"> * for the given input observation vector.</span>
<a name="l00461"></a>00461 <span class="comment"> */</span>
<a name="l00462"></a>00462 <span class="keyword">static</span> int32
<a name="l00463"></a>00463 compute_dist(<a class="code" href="structgauden__dist__t.html" title="Structure to store distance (density) values for a given input observation wrt density values in some...">gauden_dist_t</a> * out_dist, int32 n_top,
<a name="l00464"></a>00464              mfcc_t * obs, int32 featlen,
<a name="l00465"></a>00465              mfcc_t ** mean, mfcc_t ** var, mfcc_t * det,
<a name="l00466"></a>00466              int32 n_density)
<a name="l00467"></a>00467 {
<a name="l00468"></a>00468     int32 i, j, d;
<a name="l00469"></a>00469     <a class="code" href="structgauden__dist__t.html" title="Structure to store distance (density) values for a given input observation wrt density values in some...">gauden_dist_t</a> *worst;
<a name="l00470"></a>00470 
<a name="l00471"></a>00471     <span class="comment">/* Special case optimization when n_density &lt;= n_top */</span>
<a name="l00472"></a>00472     <span class="keywordflow">if</span> (n_top &gt;= n_density)
<a name="l00473"></a>00473         <span class="keywordflow">return</span> (compute_dist_all
<a name="l00474"></a>00474                 (out_dist, obs, featlen, mean, var, det, n_density));
<a name="l00475"></a>00475 
<a name="l00476"></a>00476     <span class="keywordflow">for</span> (i = 0; i &lt; n_top; i++)
<a name="l00477"></a>00477         out_dist[i].dist = WORST_DIST;
<a name="l00478"></a>00478     worst = &amp;(out_dist[n_top - 1]);
<a name="l00479"></a>00479 
<a name="l00480"></a>00480     <span class="keywordflow">for</span> (d = 0; d &lt; n_density; d++) {
<a name="l00481"></a>00481         mfcc_t *m;
<a name="l00482"></a>00482         mfcc_t *v;
<a name="l00483"></a>00483         mfcc_t dval;
<a name="l00484"></a>00484 
<a name="l00485"></a>00485         m = mean[d];
<a name="l00486"></a>00486         v = var[d];
<a name="l00487"></a>00487         dval = det[d];
<a name="l00488"></a>00488 
<a name="l00489"></a>00489         <span class="keywordflow">for</span> (i = 0; (i &lt; featlen) &amp;&amp; (dval &gt;= worst-&gt;<a class="code" href="structgauden__dist__t.html#ab08100d4953998dad76bfcf30864c0c5" title="Density value for input observation wrt above codeword; NOTE: result in logs3 domain, but var_t used for speed.">dist</a>); i++) {
<a name="l00490"></a>00490             mfcc_t diff;
<a name="l00491"></a>00491 <span class="preprocessor">#ifdef FIXED_POINT</span>
<a name="l00492"></a>00492 <span class="preprocessor"></span>            <span class="comment">/* Have to check for underflows here. */</span>
<a name="l00493"></a>00493             mfcc_t pdval = dval;
<a name="l00494"></a>00494             diff = obs[i] - m[i];
<a name="l00495"></a>00495             dval -= MFCCMUL(MFCCMUL(diff, diff), v[i]);
<a name="l00496"></a>00496             <span class="keywordflow">if</span> (dval &gt; pdval) {
<a name="l00497"></a>00497                 dval = <a class="code" href="hmm_8h.html#a833f100ee293ad0f1c03f2a5ef9be77a" title="Large &amp;quot;bad&amp;quot; score.">WORST_SCORE</a>;
<a name="l00498"></a>00498                 <span class="keywordflow">break</span>;
<a name="l00499"></a>00499             }
<a name="l00500"></a>00500 <span class="preprocessor">#else</span>
<a name="l00501"></a>00501 <span class="preprocessor"></span>            diff = obs[i] - m[i];
<a name="l00502"></a>00502             <span class="comment">/* The compiler really likes this to be a single</span>
<a name="l00503"></a>00503 <span class="comment">             * expression, for whatever reason. */</span>
<a name="l00504"></a>00504             dval -= diff * diff * v[i];
<a name="l00505"></a>00505 <span class="preprocessor">#endif</span>
<a name="l00506"></a>00506 <span class="preprocessor"></span>        }
<a name="l00507"></a>00507 
<a name="l00508"></a>00508         <span class="keywordflow">if</span> ((i &lt; featlen) || (dval &lt; worst-&gt;dist))     <span class="comment">/* Codeword d worse than worst */</span>
<a name="l00509"></a>00509             <span class="keywordflow">continue</span>;
<a name="l00510"></a>00510 
<a name="l00511"></a>00511         <span class="comment">/* Codeword d at least as good as worst so far; insert in the ordered list */</span>
<a name="l00512"></a>00512         <span class="keywordflow">for</span> (i = 0; (i &lt; n_top) &amp;&amp; (dval &lt; out_dist[i].dist); i++);
<a name="l00513"></a>00513         assert(i &lt; n_top);
<a name="l00514"></a>00514         <span class="keywordflow">for</span> (j = n_top - 1; j &gt; i; --j)
<a name="l00515"></a>00515             out_dist[j] = out_dist[j - 1];
<a name="l00516"></a>00516         out_dist[i].<a class="code" href="structgauden__dist__t.html#ab08100d4953998dad76bfcf30864c0c5" title="Density value for input observation wrt above codeword; NOTE: result in logs3 domain, but var_t used for speed.">dist</a> = dval;
<a name="l00517"></a>00517         out_dist[i].<a class="code" href="structgauden__dist__t.html#a93e978149bae5b5bf089769458c42ee4" title="Index of codeword (gaussian density)">id</a> = d;
<a name="l00518"></a>00518     }
<a name="l00519"></a>00519 
<a name="l00520"></a>00520     <span class="keywordflow">return</span> 0;
<a name="l00521"></a>00521 }
<a name="l00522"></a>00522 
<a name="l00523"></a>00523 
<a name="l00524"></a>00524 <span class="comment">/*</span>
<a name="l00525"></a>00525 <span class="comment"> * Compute distances of the input observation from the top N codewords in the given</span>
<a name="l00526"></a>00526 <span class="comment"> * codebook (g-&gt;{mean,var}[mgau]).  The input observation, obs, includes vectors for</span>
<a name="l00527"></a>00527 <span class="comment"> * all features in the codebook.</span>
<a name="l00528"></a>00528 <span class="comment"> */</span>
<a name="l00529"></a>00529 int32
<a name="l00530"></a>00530 <a class="code" href="ms__gauden_8h.html#a68cd25af0a2faf05eacd17afd5407ea4" title="Compute gaussian density values for the given input observation vector wrt the specified mixture gaus...">gauden_dist</a>(<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> * g,
<a name="l00531"></a>00531             <span class="keywordtype">int</span> mgau, int32 n_top, mfcc_t** obs, <a class="code" href="structgauden__dist__t.html" title="Structure to store distance (density) values for a given input observation wrt density values in some...">gauden_dist_t</a> ** out_dist)
<a name="l00532"></a>00532 {
<a name="l00533"></a>00533     int32 f;
<a name="l00534"></a>00534 
<a name="l00535"></a>00535     assert((n_top &gt; 0) &amp;&amp; (n_top &lt;= g-&gt;n_density));
<a name="l00536"></a>00536 
<a name="l00537"></a>00537     <span class="keywordflow">for</span> (f = 0; f &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; f++) {
<a name="l00538"></a>00538         compute_dist(out_dist[f], n_top,
<a name="l00539"></a>00539                      obs[f], g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f],
<a name="l00540"></a>00540                      g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>[mgau][f], g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>[mgau][f], g-&gt;<a class="code" href="structgauden__t.html#a315918a44ff97b95a6fcdf8739d8089b" title="log(determinant) for each variance vector; actually, log(sqrt(2*pi*det))">det</a>[mgau][f],
<a name="l00541"></a>00541                      g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>);
<a name="l00542"></a>00542         E_DEBUG(3, (<span class="stringliteral">&quot;Top CW(%d,%d) = %d %d\n&quot;</span>, mgau, f, out_dist[f][0].<span class="keywordtype">id</span>,
<a name="l00543"></a>00543                     (<span class="keywordtype">int</span>)out_dist[f][0].dist &gt;&gt; <a class="code" href="hmm_8h.html#af94da16e3e5b550b9be05b0f07402cc7" title="Shift count for senone scores.">SENSCR_SHIFT</a>));
<a name="l00544"></a>00544     }
<a name="l00545"></a>00545 
<a name="l00546"></a>00546     <span class="keywordflow">return</span> 0;
<a name="l00547"></a>00547 }
<a name="l00548"></a>00548 
<a name="l00549"></a>00549 int32
<a name="l00550"></a><a class="code" href="ms__gauden_8h.html#ab640722f2c2b1e8bc6e883d9b5b72494">00550</a> gauden_mllr_transform(<a class="code" href="structgauden__t.html" title="Multivariate gaussian mixture density parameters.">gauden_t</a> *g, <a class="code" href="structps__mllr__s.html" title="Feature space linear transform structure.">ps_mllr_t</a> *mllr, cmd_ln_t *config)
<a name="l00551"></a>00551 {
<a name="l00552"></a>00552     int32 i, m, f, d, *flen;
<a name="l00553"></a>00553     float32 ****fgau;
<a name="l00554"></a>00554 
<a name="l00555"></a>00555     <span class="comment">/* Reload means and variances (un-precomputed). */</span>
<a name="l00556"></a>00556     fgau = NULL;
<a name="l00557"></a>00557     gauden_param_read(&amp;fgau, &amp;g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>, &amp;g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>, &amp;g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>,
<a name="l00558"></a>00558                       &amp;g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>, cmd_ln_str_r(config, <span class="stringliteral">&quot;-mean&quot;</span>));
<a name="l00559"></a>00559     g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a> = (mfcc_t ****)fgau;
<a name="l00560"></a>00560     fgau = NULL;
<a name="l00561"></a>00561     gauden_param_read(&amp;fgau, &amp;m, &amp;f, &amp;d, &amp;flen, cmd_ln_str_r(config, <span class="stringliteral">&quot;-var&quot;</span>));
<a name="l00562"></a>00562     g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a> = (mfcc_t ****)fgau;
<a name="l00563"></a>00563 
<a name="l00564"></a>00564     <span class="comment">/* Verify mean and variance parameter dimensions */</span>
<a name="l00565"></a>00565     <span class="keywordflow">if</span> ((m != g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>) || (f != g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>) || (d != g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>))
<a name="l00566"></a>00566         E_FATAL
<a name="l00567"></a>00567             (<span class="stringliteral">&quot;Mixture-gaussians dimensions for means and variances differ\n&quot;</span>);
<a name="l00568"></a>00568     <span class="keywordflow">for</span> (i = 0; i &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; i++)
<a name="l00569"></a>00569         <span class="keywordflow">if</span> (g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[i] != flen[i])
<a name="l00570"></a>00570             E_FATAL(<span class="stringliteral">&quot;Feature lengths for means and variances differ\n&quot;</span>);
<a name="l00571"></a>00571     ckd_free(flen);
<a name="l00572"></a>00572 
<a name="l00573"></a>00573     <span class="comment">/* Transform codebook for each stream s */</span>
<a name="l00574"></a>00574     <span class="keywordflow">for</span> (i = 0; i &lt; g-&gt;<a class="code" href="structgauden__t.html#af15df11bd2f3ab0290e0a33ca15c836c" title="Number codebooks.">n_mgau</a>; ++i) {
<a name="l00575"></a>00575         <span class="keywordflow">for</span> (f = 0; f &lt; g-&gt;<a class="code" href="structgauden__t.html#a6dea221962b4a278dbae4806925a7be0" title="Number feature streams in each codebook.">n_feat</a>; ++f) {
<a name="l00576"></a>00576             float64 *temp;
<a name="l00577"></a>00577             temp = (float64 *) ckd_calloc(g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f], <span class="keyword">sizeof</span>(float64));
<a name="l00578"></a>00578             <span class="comment">/* Transform each density d in selected codebook */</span>
<a name="l00579"></a>00579             <span class="keywordflow">for</span> (d = 0; d &lt; g-&gt;<a class="code" href="structgauden__t.html#ab5104d8fa59bc94b12b36104dd4c19ac" title="Number gaussian densities in each codebook-feature stream.">n_density</a>; d++) {
<a name="l00580"></a>00580                 <span class="keywordtype">int</span> l;
<a name="l00581"></a>00581                 <span class="keywordflow">for</span> (l = 0; l &lt; g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]; l++) {
<a name="l00582"></a>00582                     temp[l] = 0.0;
<a name="l00583"></a>00583                     <span class="keywordflow">for</span> (m = 0; m &lt; g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]; m++) {
<a name="l00584"></a>00584                         <span class="comment">/* FIXME: For now, only one class, hence the zeros below. */</span>
<a name="l00585"></a>00585                         temp[l] += mllr-&gt;<a class="code" href="structps__mllr__s.html#a4cfa5d4c6637282b947b525a673cc3d0" title="Rotation part of mean transformations.">A</a>[f][0][l][m] * g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>[i][f][d][m];
<a name="l00586"></a>00586                     }
<a name="l00587"></a>00587                     temp[l] += mllr-&gt;<a class="code" href="structps__mllr__s.html#a65430ba654d0af5e508715de24077f2d" title="Bias part of mean transformations.">b</a>[f][0][l];
<a name="l00588"></a>00588                 }
<a name="l00589"></a>00589 
<a name="l00590"></a>00590                 <span class="keywordflow">for</span> (l = 0; l &lt; g-&gt;<a class="code" href="structgauden__t.html#aca099d830de926e628ac0b523046b32c" title="feature length for each feature">featlen</a>[f]; l++) {
<a name="l00591"></a>00591                     g-&gt;<a class="code" href="structgauden__t.html#aa76d63009601dcb6b99f17db96167f37" title="mean[codebook][feature][codeword] vector">mean</a>[i][f][d][l] = (float32) temp[l];
<a name="l00592"></a>00592                     g-&gt;<a class="code" href="structgauden__t.html#a172cb69c018608ab8c33d54e6f0fdf19" title="like mean; diagonal covariance vector only">var</a>[i][f][d][l] *= mllr-&gt;<a class="code" href="structps__mllr__s.html#ae92b910e17202389aca99ee5105085b0" title="Diagonal transformation of variances.">h</a>[f][0][l];
<a name="l00593"></a>00593                 }
<a name="l00594"></a>00594             }
<a name="l00595"></a>00595             ckd_free(temp);
<a name="l00596"></a>00596         }
<a name="l00597"></a>00597     }
<a name="l00598"></a>00598 
<a name="l00599"></a>00599     <span class="comment">/* Re-precompute (if we aren&#39;t adapting variances this isn&#39;t</span>
<a name="l00600"></a>00600 <span class="comment">     * actually necessary...) */</span>
<a name="l00601"></a>00601     gauden_dist_precompute(g, g-&gt;<a class="code" href="structgauden__t.html#ae05c9b82d8a586bea347681ebe04bb83" title="log math computation">lmath</a>, cmd_ln_float32_r(config, <span class="stringliteral">&quot;-varfloor&quot;</span>));
<a name="l00602"></a>00602     <span class="keywordflow">return</span> 0;
<a name="l00603"></a>00603 }
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>ms_gauden.c</b>      </li>
      <li class="footer">Generated on Wed Apr 20 2011 for PocketSphinx by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </li>
    </ul>
  </div>

</body>
</html>