SphinxBase 0.6
cmn.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 * cmn.c -- Various forms of cepstral mean normalization
39 *
40 * **********************************************
41 * CMU ARPA Speech Project
42 *
43 * Copyright (c) 1996 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
46 *
47 * HISTORY
48 * $Log$
49 * Revision 1.14 2006/02/24 15:57:47 egouvea
50 * Removed cmn = NULL from the cmn_free(), since it's pointless (my bad!).
51 *
52 * Removed cmn_prior, which was surrounded by #if 0/#endif, since the
53 * function is already in cmn_prior.c
54 *
55 * Revision 1.13 2006/02/23 03:47:49 arthchan2003
56 * Used Evandro's changes. Resolved conflicts.
57 *
58 *
59 * Revision 1.12 2006/02/23 00:48:23 egouvea
60 * Replaced loops resetting vectors with the more efficient memset()
61 *
62 * Revision 1.11 2006/02/22 23:43:55 arthchan2003
63 * Merged from the branch SPHINX3_5_2_RCI_IRII_BRANCH: Put data structure into the cmn_t structure.
64 *
65 * Revision 1.10.4.2 2005/10/17 04:45:57 arthchan2003
66 * Free stuffs in cmn and feat corectly.
67 *
68 * Revision 1.10.4.1 2005/07/05 06:25:08 arthchan2003
69 * Fixed dox-doc.
70 *
71 * Revision 1.10 2005/06/21 19:28:00 arthchan2003
72 * 1, Fixed doxygen documentation. 2, Added $ keyword.
73 *
74 * Revision 1.3 2005/03/30 01:22:46 archan
75 * Fixed mistakes in last updates. Add
76 *
77 *
78 * 20.Apr.2001 RAH (rhoughton@mediasite.com, ricky.houghton@cs.cmu.edu)
79 * Added cmn_free() and moved *mean and *var out global space and named them cmn_mean and cmn_var
80 *
81 * 28-Apr-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
82 * Changed the name norm_mean() to cmn().
83 *
84 * 19-Jun-1996 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
85 * Changed to compute CMN over ALL dimensions of cep instead of 1..12.
86 *
87 * 04-Nov-1995 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
88 * Created.
89 */
90
91
92#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
95#include <assert.h>
96#include <math.h>
97#ifdef HAVE_CONFIG_H
98#include <config.h>
99#endif
100
101#ifdef _MSC_VER
102#pragma warning (disable: 4244)
103#endif
104
105#include "sphinxbase/ckd_alloc.h"
106#include "sphinxbase/err.h"
107#include "sphinxbase/cmn.h"
108
109/* NOTE! These must match the enum in cmn.h */
110const char *cmn_type_str[] = {
111 "none",
112 "current",
113 "prior"
114};
115static const int n_cmn_type_str = sizeof(cmn_type_str)/sizeof(cmn_type_str[0]);
116
118cmn_type_from_str(const char *str)
119{
120 int i;
121
122 for (i = 0; i < n_cmn_type_str; ++i) {
123 if (0 == strcmp(str, cmn_type_str[i]))
124 return (cmn_type_t)i;
125 }
126 E_FATAL("Unknown CMN type '%s'\n", str);
127 return CMN_NONE;
128}
129
130cmn_t *
131cmn_init(int32 veclen)
132{
133 cmn_t *cmn;
134 cmn = (cmn_t *) ckd_calloc(1, sizeof(cmn_t));
135 cmn->veclen = veclen;
136 cmn->cmn_mean = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
137 cmn->cmn_var = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
138 cmn->sum = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
139 /* A front-end dependent magic number */
140 cmn->cmn_mean[0] = FLOAT2MFCC(12.0);
141 cmn->nframe = 0;
142 E_INFO("mean[0]= %.2f, mean[1..%d]= 0.0\n",
143 MFCC2FLOAT(cmn->cmn_mean[0]), veclen - 1);
144
145 return cmn;
146}
147
148
149void
150cmn(cmn_t *cmn, mfcc_t ** mfc, int32 varnorm, int32 n_frame)
151{
152 mfcc_t *mfcp;
153 mfcc_t t;
154 int32 i, f;
155
156 assert(mfc != NULL);
157
158 if (n_frame <= 0)
159 return;
160
161 /* If cmn->cmn_mean wasn't NULL, we need to zero the contents */
162 memset(cmn->cmn_mean, 0, cmn->veclen * sizeof(mfcc_t));
163
164 /* Find mean cep vector for this utterance */
165 for (f = 0; f < n_frame; f++) {
166 mfcp = mfc[f];
167 for (i = 0; i < cmn->veclen; i++) {
168 cmn->cmn_mean[i] += mfcp[i];
169 }
170 }
171
172 for (i = 0; i < cmn->veclen; i++)
173 cmn->cmn_mean[i] /= n_frame;
174
175 E_INFO("CMN: ");
176 for (i = 0; i < cmn->veclen; i++)
177 E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
178 E_INFOCONT("\n");
179 if (!varnorm) {
180 /* Subtract mean from each cep vector */
181 for (f = 0; f < n_frame; f++) {
182 mfcp = mfc[f];
183 for (i = 0; i < cmn->veclen; i++)
184 mfcp[i] -= cmn->cmn_mean[i];
185 }
186 }
187 else {
188 /* Scale cep vectors to have unit variance along each dimension, and subtract means */
189 /* If cmn->cmn_var wasn't NULL, we need to zero the contents */
190 memset(cmn->cmn_var, 0, cmn->veclen * sizeof(mfcc_t));
191
192 for (f = 0; f < n_frame; f++) {
193 mfcp = mfc[f];
194
195 for (i = 0; i < cmn->veclen; i++) {
196 t = mfcp[i] - cmn->cmn_mean[i];
197 cmn->cmn_var[i] += MFCCMUL(t, t);
198 }
199 }
200 for (i = 0; i < cmn->veclen; i++)
201 /* Inverse Std. Dev, RAH added type case from sqrt */
202 cmn->cmn_var[i] = FLOAT2MFCC(sqrt((float64)n_frame / MFCC2FLOAT(cmn->cmn_var[i])));
203
204 for (f = 0; f < n_frame; f++) {
205 mfcp = mfc[f];
206 for (i = 0; i < cmn->veclen; i++)
207 mfcp[i] = MFCCMUL((mfcp[i] - cmn->cmn_mean[i]), cmn->cmn_var[i]);
208 }
209 }
210}
211
212/*
213 * RAH, free previously allocated memory
214 */
215void
216cmn_free(cmn_t * cmn)
217{
218 if (cmn != NULL) {
219 if (cmn->cmn_var)
220 ckd_free((void *) cmn->cmn_var);
221
222 if (cmn->cmn_mean)
223 ckd_free((void *) cmn->cmn_mean);
224
225 if (cmn->sum)
226 ckd_free((void *) cmn->sum);
227
228 ckd_free((void *) cmn);
229 }
230}
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Definition ckd_alloc.c:241
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
Definition ckd_alloc.h:248
Apply Cepstral Mean Normalization (CMN) to the set of input mfc frames.
enum cmn_type_e cmn_type_t
Types of cepstral mean normalization to apply to the features.
SPHINXBASE_EXPORT void cmn(cmn_t *cmn, mfcc_t **mfc, int32 varnorm, int32 n_frame)
CMN for the whole sentence.
Definition cmn.c:150
SPHINXBASE_EXPORT cmn_type_t cmn_type_from_str(const char *str)
Convert string representation (from command-line) to cmn_type_t.
Definition cmn.c:118
SPHINXBASE_EXPORT const char * cmn_type_str[]
String representations of cmn_type_t values.
Definition cmn.c:110
Implementation of logging routines.
#define E_FATAL
Exit with non-zero status after error message.
Definition err.h:127
#define E_INFO
Print logging information to standard error stream.
Definition err.h:147
#define E_INFOCONT
Print logging information without header, to standard error stream.
Definition err.h:153
wrapper of operation of the cepstral mean normalization.
Definition cmn.h:128