PocketSphinx  0.6
src/libpocketsphinx/vector.c
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 
00038 /*
00039  * vector.c
00040  *
00041  * **********************************************
00042  * CMU ARPA Speech Project
00043  *
00044  * Copyright (c) 1997 Carnegie Mellon University.
00045  * ALL RIGHTS RESERVED.
00046  * **********************************************
00047  * 
00048  * HISTORY
00049  * 
00050  * 22-Nov-2004  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00051  *              Imported from s3.2, for supporting s3 format continuous
00052  *              acoustic models.
00053  * 
00054  * 10-Mar-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00055  *              Added vector_accum(), vector_vqlabel(), and vector_vqgen().
00056  * 
00057  * 09-Mar-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00058  *              Added vector_is_zero(), vector_cmp(), and vector_dist_eucl().
00059  *              Changed the name vector_dist_eval to vector_dist_maha.
00060  * 
00061  * 07-Oct-98    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00062  *              Added distance computation related functions.
00063  * 
00064  * 12-Nov-95    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00065  *              Copied from Eric Thayer.
00066  */
00067 
00068 /* System headers. */
00069 #include <stdio.h>
00070 #include <stdlib.h>
00071 #include <string.h>
00072 #include <assert.h>
00073 #include <math.h>
00074 
00075 /* SphinxBase headers. */
00076 #include <sphinxbase/err.h>
00077 #include <sphinxbase/ckd_alloc.h>
00078 #include <sphinxbase/bitvec.h>
00079 
00080 /* Local headers. */
00081 #include "vector.h"
00082 
00083 #if (WIN32)
00084 #define srandom srand
00085 #define random  rand
00086 #endif
00087 
00088 
00089 float64
00090 vector_sum_norm(float32 * vec, int32 len)
00091 {
00092     float64 sum, f;
00093     int32 i;
00094 
00095     sum = 0.0;
00096     for (i = 0; i < len; i++)
00097         sum += vec[i];
00098 
00099     if (sum != 0.0) {
00100         f = 1.0 / sum;
00101         for (i = 0; i < len; i++)
00102             vec[i] *= f;
00103     }
00104 
00105     return sum;
00106 }
00107 
00108 
00109 void
00110 vector_floor(float32 * vec, int32 len, float64 flr)
00111 {
00112     int32 i;
00113 
00114     for (i = 0; i < len; i++)
00115         if (vec[i] < flr)
00116             vec[i] = (float32) flr;
00117 }
00118 
00119 
00120 void
00121 vector_nz_floor(float32 * vec, int32 len, float64 flr)
00122 {
00123     int32 i;
00124 
00125     for (i = 0; i < len; i++)
00126         if ((vec[i] != 0.0) && (vec[i] < flr))
00127             vec[i] = (float32) flr;
00128 }
00129 
00130 
00131 void
00132 vector_print(FILE * fp, vector_t v, int32 dim)
00133 {
00134     int32 i;
00135 
00136     for (i = 0; i < dim; i++)
00137         fprintf(fp, " %11.4e", v[i]);
00138     fprintf(fp, "\n");
00139     fflush(fp);
00140 }
00141 
00142 
00143 int32
00144 vector_is_zero(float32 * vec, int32 len)
00145 {
00146     int32 i;
00147 
00148     for (i = 0; (i < len) && (vec[i] == 0.0); i++);
00149     return (i == len);          /* TRUE iff all mean values are 0.0 */
00150 }