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