PocketSphinx 5prealpha
vector.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
21 *
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * ====================================================================
35 *
36 */
37
38/*
39 * vector.c
40 *
41 * **********************************************
42 * CMU ARPA Speech Project
43 *
44 * Copyright (c) 1997 Carnegie Mellon University.
45 * ALL RIGHTS RESERVED.
46 * **********************************************
47 *
48 * HISTORY
49 *
50 * 22-Nov-2004 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
51 * Imported from s3.2, for supporting s3 format continuous
52 * acoustic models.
53 *
54 * 10-Mar-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
55 * Added vector_accum(), vector_vqlabel(), and vector_vqgen().
56 *
57 * 09-Mar-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
58 * Added vector_is_zero(), vector_cmp(), and vector_dist_eucl().
59 * Changed the name vector_dist_eval to vector_dist_maha.
60 *
61 * 07-Oct-98 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
62 * Added distance computation related functions.
63 *
64 * 12-Nov-95 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
65 * Copied from Eric Thayer.
66 */
67
68/* System headers. */
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <assert.h>
73#include <math.h>
74
75/* SphinxBase headers. */
76#include <sphinxbase/err.h>
77#include <sphinxbase/ckd_alloc.h>
78#include <sphinxbase/bitvec.h>
79
80/* Local headers. */
81#include "vector.h"
82
83#if defined(_WIN32)
84#define srandom srand
85#define random rand
86#endif
87
88
89float64
90vector_sum_norm(float32 * vec, int32 len)
91{
92 float64 sum, f;
93 int32 i;
94
95 sum = 0.0;
96 for (i = 0; i < len; i++)
97 sum += vec[i];
98
99 if (sum != 0.0) {
100 f = 1.0 / sum;
101 for (i = 0; i < len; i++)
102 vec[i] *= f;
103 }
104
105 return sum;
106}
107
108
109void
110vector_floor(float32 * vec, int32 len, float64 flr)
111{
112 int32 i;
113
114 for (i = 0; i < len; i++)
115 if (vec[i] < flr)
116 vec[i] = (float32) flr;
117}
118
119
120void
121vector_nz_floor(float32 * vec, int32 len, float64 flr)
122{
123 int32 i;
124
125 for (i = 0; i < len; i++)
126 if ((vec[i] != 0.0) && (vec[i] < flr))
127 vec[i] = (float32) flr;
128}
129
130
131void
132vector_print(FILE * fp, vector_t v, int32 dim)
133{
134 int32 i;
135
136 for (i = 0; i < dim; i++)
137 fprintf(fp, " %11.4e", v[i]);
138 fprintf(fp, "\n");
139 fflush(fp);
140}
141
142
143int32
144vector_is_zero(float32 * vec, int32 len)
145{
146 int32 i;
147
148 for (i = 0; (i < len) && (vec[i] == 0.0); i++);
149 return (i == len); /* TRUE iff all mean values are 0.0 */
150}