SphinxBase  5prealpha
cmn_live.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 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 
42 #ifdef _MSC_VER
43 #pragma warning (disable: 4244)
44 #endif
45 
46 #include "sphinxbase/ckd_alloc.h"
47 #include "sphinxbase/err.h"
48 #include "sphinxbase/cmn.h"
49 
50 void
51 cmn_live_set(cmn_t *cmn, mfcc_t const * vec)
52 {
53  int32 i;
54 
55  E_INFO("Update from < ");
56  for (i = 0; i < cmn->veclen; i++)
57  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
58  E_INFOCONT(">\n");
59 
60  for (i = 0; i < cmn->veclen; i++) {
61  cmn->cmn_mean[i] = vec[i];
62  cmn->sum[i] = vec[i] * CMN_WIN;
63  }
64  cmn->nframe = CMN_WIN;
65 
66  E_INFO("Update to < ");
67  for (i = 0; i < cmn->veclen; i++)
68  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
69  E_INFOCONT(">\n");
70 }
71 
72 void
73 cmn_live_get(cmn_t *cmn, mfcc_t * vec)
74 {
75  int32 i;
76 
77  for (i = 0; i < cmn->veclen; i++)
78  vec[i] = cmn->cmn_mean[i];
79 
80 }
81 
82 static void
83 cmn_live_shiftwin(cmn_t *cmn)
84 {
85  mfcc_t sf;
86  int32 i;
87 
88  E_INFO("Update from < ");
89  for (i = 0; i < cmn->veclen; i++)
90  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
91  E_INFOCONT(">\n");
92 
93  sf = FLOAT2MFCC(1.0) / cmn->nframe;
94  for (i = 0; i < cmn->veclen; i++)
95  cmn->cmn_mean[i] = cmn->sum[i] / cmn->nframe; /* sum[i] * sf */
96 
97  /* Make the accumulation decay exponentially */
98  if (cmn->nframe >= CMN_WIN_HWM) {
99  sf = CMN_WIN * sf;
100  for (i = 0; i < cmn->veclen; i++)
101  cmn->sum[i] = MFCCMUL(cmn->sum[i], sf);
102  cmn->nframe = CMN_WIN;
103  }
104 
105  E_INFO("Update to < ");
106  for (i = 0; i < cmn->veclen; i++)
107  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
108  E_INFOCONT(">\n");
109 }
110 
111 void
113 {
114  mfcc_t sf;
115  int32 i;
116 
117  if (cmn->nframe <= 0)
118  return;
119 
120  E_INFO("Update from < ");
121  for (i = 0; i < cmn->veclen; i++)
122  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
123  E_INFOCONT(">\n");
124 
125  /* Update mean buffer */
126  sf = FLOAT2MFCC(1.0) / cmn->nframe;
127  for (i = 0; i < cmn->veclen; i++)
128  cmn->cmn_mean[i] = cmn->sum[i] / cmn->nframe; /* sum[i] * sf; */
129 
130  /* Make the accumulation decay exponentially */
131  if (cmn->nframe > CMN_WIN_HWM) {
132  sf = CMN_WIN * sf;
133  for (i = 0; i < cmn->veclen; i++)
134  cmn->sum[i] = MFCCMUL(cmn->sum[i], sf);
135  cmn->nframe = CMN_WIN;
136  }
137 
138  E_INFO("Update to < ");
139  for (i = 0; i < cmn->veclen; i++)
140  E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
141  E_INFOCONT(">\n");
142 }
143 
144 void
145 cmn_live(cmn_t *cmn, mfcc_t **incep, int32 varnorm, int32 nfr)
146 {
147  int32 i, j;
148 
149  if (nfr <= 0)
150  return;
151 
152  if (varnorm)
153  E_FATAL
154  ("Variance normalization not implemented in live mode decode\n");
155 
156  for (i = 0; i < nfr; i++) {
157 
158  /* Skip zero energy frames */
159  if (incep[i][0] < 0)
160  continue;
161 
162  for (j = 0; j < cmn->veclen; j++) {
163  cmn->sum[j] += incep[i][j];
164  incep[i][j] -= cmn->cmn_mean[j];
165  }
166 
167  ++cmn->nframe;
168  }
169 
170  /* Shift buffer down if we have more than CMN_WIN_HWM frames */
171  if (cmn->nframe > CMN_WIN_HWM)
172  cmn_live_shiftwin(cmn);
173 }
#define E_INFO(...)
Print logging information to standard error stream.
Definition: err.h:114
SPHINXBASE_EXPORT void cmn_live_set(cmn_t *cmn, mfcc_t const *vec)
Set the live mean.
Definition: cmn_live.c:51
Sphinx&#39;s memory allocation/deallocation routines.
Apply Cepstral Mean Normalization (CMN) to the set of input mfc frames.
#define E_INFOCONT(...)
Continue printing the information to standard error stream.
Definition: err.h:119
SPHINXBASE_EXPORT void cmn(cmn_t *cmn, mfcc_t **mfc, int32 varnorm, int32 n_frame)
CMN for the whole sentence.
Definition: cmn.c:100
Implementation of logging routines.
SPHINXBASE_EXPORT void cmn_live_get(cmn_t *cmn, mfcc_t *vec)
Get the live mean.
Definition: cmn_live.c:73
wrapper of operation of the cepstral mean normalization.
Definition: cmn.h:128
SPHINXBASE_EXPORT void cmn_live(cmn_t *cmn, mfcc_t **incep, int32 varnorm, int32 nfr)
CMN for one block of data, using live mean.
Definition: cmn_live.c:145
#define E_FATAL(...)
Exit with non-zero status after error message.
Definition: err.h:81
mfcc_t * cmn_mean
Temporary variable: current means.
Definition: cmn.h:129
mfcc_t * sum
The sum of the cmn frames.
Definition: cmn.h:131
SPHINXBASE_EXPORT void cmn_live_update(cmn_t *cmn)
Update live mean based on observed data.
Definition: cmn_live.c:112
int32 veclen
Length of cepstral vector.
Definition: cmn.h:133
int32 nframe
Number of frames.
Definition: cmn.h:132