Initial import to Tizen
[profile/ivi/sphinxbase.git] / src / libsphinxbase / feat / 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     sf = FLOAT2MFCC(1.0) / cmn->nframe;
100     for (i = 0; i < cmn->veclen; i++)
101         cmn->cmn_mean[i] = cmn->sum[i] / cmn->nframe; /* sum[i] * sf */
102
103     /* Make the accumulation decay exponentially */
104     if (cmn->nframe >= CMN_WIN_HWM) {
105         sf = CMN_WIN * sf;
106         for (i = 0; i < cmn->veclen; i++)
107             cmn->sum[i] = MFCCMUL(cmn->sum[i], sf);
108         cmn->nframe = CMN_WIN;
109     }
110 }
111
112 void
113 cmn_prior_update(cmn_t *cmn)
114 {
115     mfcc_t sf;
116     int32 i;
117
118     if (cmn->nframe <= 0)
119         return;
120
121     E_INFO("cmn_prior_update: from < ");
122     for (i = 0; i < cmn->veclen; i++)
123         E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
124     E_INFOCONT(">\n");
125
126     /* Update mean buffer */
127     sf = FLOAT2MFCC(1.0) / cmn->nframe;
128     for (i = 0; i < cmn->veclen; i++)
129         cmn->cmn_mean[i] = cmn->sum[i] / cmn->nframe; /* sum[i] * sf; */
130
131     /* Make the accumulation decay exponentially */
132     if (cmn->nframe > CMN_WIN_HWM) {
133         sf = CMN_WIN * sf;
134         for (i = 0; i < cmn->veclen; i++)
135             cmn->sum[i] = MFCCMUL(cmn->sum[i], sf);
136         cmn->nframe = CMN_WIN;
137     }
138
139     E_INFO("cmn_prior_update: to   < ");
140     for (i = 0; i < cmn->veclen; i++)
141         E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
142     E_INFOCONT(">\n");
143 }
144
145 void
146 cmn_prior(cmn_t *cmn, mfcc_t **incep, int32 varnorm, int32 nfr)
147 {
148     int32 i, j;
149
150     if (varnorm)
151         E_FATAL
152             ("Variance normalization not implemented in live mode decode\n");
153
154     if (nfr <= 0)
155         return;
156
157     for (i = 0; i < nfr; i++) {
158         for (j = 0; j < cmn->veclen; j++) {
159             cmn->sum[j] += incep[i][j];
160             incep[i][j] -= cmn->cmn_mean[j];
161         }
162         ++cmn->nframe;
163     }
164
165     /* Shift buffer down if we have more than CMN_WIN_HWM frames */
166     if (cmn->nframe > CMN_WIN_HWM)
167         cmn_prior_shiftwin(cmn);
168 }