tomwalters@268
|
1 // Copyright 2008-2010, Thomas Walters
|
tomwalters@268
|
2 //
|
tomwalters@268
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@268
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@268
|
5 //
|
tomwalters@268
|
6 // This program is free software: you can redistribute it and/or modify
|
tomwalters@268
|
7 // it under the terms of the GNU General Public License as published by
|
tomwalters@268
|
8 // the Free Software Foundation, either version 3 of the License, or
|
tomwalters@268
|
9 // (at your option) any later version.
|
tomwalters@268
|
10 //
|
tomwalters@268
|
11 // This program is distributed in the hope that it will be useful,
|
tomwalters@268
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@268
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@268
|
14 // GNU General Public License for more details.
|
tomwalters@268
|
15 //
|
tomwalters@268
|
16 // You should have received a copy of the GNU General Public License
|
tomwalters@268
|
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@268
|
18
|
tomwalters@268
|
19 /*! \file
|
tomwalters@268
|
20 * \brief Gaussian features - based on MATLAB code by Christian Feldbauer
|
tomwalters@268
|
21 */
|
tomwalters@268
|
22
|
tomwalters@268
|
23 /*!
|
tomwalters@268
|
24 * \author Tom Walters <tcw24@cam.ac.uk>
|
tomwalters@268
|
25 * \date created 2008/06/23
|
tomwalters@268
|
26 * \version \$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $
|
tomwalters@268
|
27 */
|
tomwalters@268
|
28
|
tomwalters@268
|
29 #include <math.h>
|
tomwalters@268
|
30
|
tomwalters@268
|
31 #include "Modules/Features/ModuleGaussians.h"
|
tomwalters@268
|
32 #include "Support/Common.h"
|
tomwalters@268
|
33
|
tomwalters@268
|
34 namespace aimc {
|
tomwalters@268
|
35 ModuleGaussians::ModuleGaussians(Parameters *pParam)
|
tomwalters@268
|
36 : Module(pParam) {
|
tomwalters@268
|
37 // Set module metadata
|
tomwalters@268
|
38 module_description_ = "Gaussian Fitting to SSI profile";
|
tomwalters@268
|
39 module_identifier_ = "gaussians"; // unique identifier for the module
|
tomwalters@268
|
40 module_type_ = "features";
|
tomwalters@268
|
41 module_version_ = "$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $";
|
tomwalters@268
|
42
|
tomwalters@268
|
43 parameters_->SetDefault("features.gaussians.ncomp", "4");
|
tomwalters@268
|
44 m_iParamNComp = parameters_->GetInt("features.gaussians.ncomp");
|
tomwalters@268
|
45
|
tomwalters@268
|
46 parameters_->SetDefault("features.gaussians.var", "115.0");
|
tomwalters@268
|
47 m_fParamVar = parameters_->GetFloat("features.gaussians.var");
|
tomwalters@268
|
48
|
tomwalters@268
|
49 parameters_->SetDefault("features.gaussians.posterior_exp", "6.0");
|
tomwalters@268
|
50 m_fParamPosteriorExp =
|
tomwalters@268
|
51 parameters_->GetFloat("features.gaussians.posterior_exp");
|
tomwalters@268
|
52
|
tomwalters@268
|
53 parameters_->SetDefault("features.gaussians.maxit", "250");
|
tomwalters@268
|
54 m_iParamMaxIt = parameters_->GetInt("features.gaussians.maxit");
|
tomwalters@268
|
55
|
tomwalters@268
|
56 parameters_->SetDefault("features.gaussians.priors_converged", "1e-7");
|
tomwalters@268
|
57 m_fParamPriorsConverged =
|
tomwalters@268
|
58 parameters_->GetInt("features.gaussians.priors_converged");
|
tomwalters@268
|
59 }
|
tomwalters@268
|
60
|
tomwalters@268
|
61 ModuleGaussians::~ModuleGaussians() {
|
tomwalters@268
|
62 }
|
tomwalters@268
|
63
|
tomwalters@268
|
64 bool ModuleGaussians::InitializeInternal(const SignalBank &input) {
|
tomwalters@268
|
65 m_pA.resize(m_iParamNComp, 0.0f);
|
tomwalters@268
|
66 m_pMu.resize(m_iParamNComp, 0.0f);
|
tomwalters@268
|
67
|
tomwalters@268
|
68 // Assuming the number of channels is greater than twice the number of
|
tomwalters@268
|
69 // Gaussian components, this is ok
|
tomwalters@268
|
70 if (input.channel_count() >= 2 * m_iParamNComp) {
|
tomwalters@268
|
71 output_.Initialize(1, m_iParamNComp, input.sample_rate());
|
tomwalters@268
|
72 } else {
|
tomwalters@268
|
73 LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
|
tomwalters@268
|
74 "Gaussian features. Either increase the number of filterbank"
|
tomwalters@268
|
75 " channels, or decrease the number of Gaussian components"));
|
tomwalters@268
|
76 return false;
|
tomwalters@268
|
77 }
|
tomwalters@268
|
78
|
tomwalters@268
|
79 m_iNumChannels = input.channel_count();
|
tomwalters@268
|
80 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
|
tomwalters@268
|
81
|
tomwalters@268
|
82 return true;
|
tomwalters@268
|
83 }
|
tomwalters@268
|
84
|
tomwalters@268
|
85 void ModuleGaussians::Reset() {
|
tomwalters@268
|
86 m_pSpectralProfile.clear();
|
tomwalters@268
|
87 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
|
tomwalters@268
|
88 }
|
tomwalters@268
|
89
|
tomwalters@268
|
90 void ModuleGaussians::Process(const SignalBank &input) {
|
tomwalters@268
|
91 int iAudCh = 0;
|
tomwalters@268
|
92
|
tomwalters@268
|
93 // Calculate spectral profile
|
tomwalters@268
|
94 for (int iChannel = 0;
|
tomwalters@268
|
95 iChannel < input.channel_count();
|
tomwalters@268
|
96 ++iChannel) {
|
tomwalters@268
|
97 m_pSpectralProfile[iChannel] = 0.0f;
|
tomwalters@268
|
98 for (int iSample = 0;
|
tomwalters@268
|
99 iSample < input.buffer_length();
|
tomwalters@268
|
100 ++iSample) {
|
tomwalters@268
|
101 m_pSpectralProfile[iChannel] += input[iChannel][iSample];
|
tomwalters@268
|
102 }
|
tomwalters@268
|
103 }
|
tomwalters@268
|
104
|
tomwalters@268
|
105 for (int iChannel = 0;
|
tomwalters@268
|
106 iChannel < input.channel_count();
|
tomwalters@268
|
107 ++iChannel) {
|
tomwalters@268
|
108 m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8);
|
tomwalters@268
|
109 }
|
tomwalters@268
|
110
|
tomwalters@268
|
111 float spectral_profile_sum = 0.0f;
|
tomwalters@268
|
112 for (int i = 0; i < input.channel_count(); ++i) {
|
tomwalters@268
|
113 spectral_profile_sum += m_pSpectralProfile[i];
|
tomwalters@268
|
114 }
|
tomwalters@268
|
115
|
tomwalters@268
|
116 RubberGMMCore(2, true);
|
tomwalters@268
|
117
|
tomwalters@268
|
118 float fMean1 = m_pMu[0];
|
tomwalters@268
|
119 float fMean2 = m_pMu[1];
|
tomwalters@268
|
120
|
tomwalters@268
|
121 float fA1 = 0.05 * m_pA[0];
|
tomwalters@268
|
122 float fA2 = 1.0 - 0.25 * m_pA[1];
|
tomwalters@268
|
123
|
tomwalters@268
|
124 float fGradient = (fMean2 - fMean1) / (fA2 - fA1);
|
tomwalters@268
|
125 float fIntercept = fMean2 - fGradient * fA2;
|
tomwalters@268
|
126
|
tomwalters@268
|
127 for (int i = 0; i < m_iParamNComp; ++i) {
|
tomwalters@268
|
128 m_pMu[i] = ((float)i / (float)m_iParamNComp - 1.0f)
|
tomwalters@268
|
129 * -fGradient + fIntercept;
|
tomwalters@268
|
130 }
|
tomwalters@268
|
131
|
tomwalters@268
|
132 for (int i = 0; i < m_iParamNComp; ++i) {
|
tomwalters@268
|
133 m_pA[i] = 1.0f / (float)m_iParamNComp;
|
tomwalters@268
|
134 }
|
tomwalters@268
|
135
|
tomwalters@268
|
136 RubberGMMCore(m_iParamNComp, false);
|
tomwalters@268
|
137
|
tomwalters@268
|
138 for (int i = 0; i < m_iParamNComp - 1; ++i) {
|
tomwalters@268
|
139 if (!isnan(m_pA[i])) {
|
tomwalters@268
|
140 output_.set_sample(i, 0, m_pA[i]);
|
tomwalters@268
|
141 } else {
|
tomwalters@268
|
142 output_.set_sample(i, 0, 0.0f);
|
tomwalters@268
|
143 }
|
tomwalters@268
|
144 }
|
tomwalters@268
|
145 /*for (int i = m_iParamNComp; i < m_iParamNComp * 2; ++i) {
|
tomwalters@268
|
146 m_pOutputData->getSignal(i)->setSample(iAudCh, 0, m_pMu[i-m_iParamNComp]);
|
tomwalters@268
|
147 }*/
|
tomwalters@268
|
148 double logsum = log(spectral_profile_sum);
|
tomwalters@268
|
149 if (!isinf(logsum)) {
|
tomwalters@268
|
150 output_.set_sample(m_iParamNComp - 1, 0, logsum);
|
tomwalters@268
|
151 } else {
|
tomwalters@268
|
152 output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
|
tomwalters@268
|
153 }
|
tomwalters@268
|
154 PushOutput();
|
tomwalters@268
|
155 }
|
tomwalters@268
|
156
|
tomwalters@268
|
157 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) {
|
tomwalters@268
|
158 int iSizeX = m_iNumChannels;
|
tomwalters@268
|
159
|
tomwalters@268
|
160 // Normalise the spectral profile
|
tomwalters@268
|
161 float fSpectralProfileTotal = 0.0f;
|
tomwalters@268
|
162 for (int iCount = 0; iCount < iSizeX; iCount++) {
|
tomwalters@268
|
163 fSpectralProfileTotal += m_pSpectralProfile[iCount];
|
tomwalters@268
|
164 }
|
tomwalters@268
|
165 for (int iCount = 0; iCount < iSizeX; iCount++) {
|
tomwalters@268
|
166 m_pSpectralProfile[iCount] /= fSpectralProfileTotal;
|
tomwalters@268
|
167 }
|
tomwalters@268
|
168
|
tomwalters@268
|
169 if (bDoInit) {
|
tomwalters@268
|
170 // Uniformly spaced components
|
tomwalters@268
|
171 float dd = (iSizeX - 1.0f) / iNComponents;
|
tomwalters@268
|
172 for (int i = 0; i < iNComponents; i++) {
|
tomwalters@268
|
173 m_pMu[i] = dd / 2.0f + (i * dd);
|
tomwalters@268
|
174 m_pA[i] = 1.0f / iNComponents;
|
tomwalters@268
|
175 }
|
tomwalters@268
|
176 }
|
tomwalters@268
|
177
|
tomwalters@268
|
178 vector<float> pA_old;
|
tomwalters@268
|
179 pA_old.resize(iNComponents);
|
tomwalters@268
|
180 vector<float> pP_mod_X;
|
tomwalters@268
|
181 pP_mod_X.resize(iSizeX);
|
tomwalters@268
|
182 vector<float> pP_comp;
|
tomwalters@268
|
183 pP_comp.resize(iSizeX * iNComponents);
|
tomwalters@268
|
184
|
tomwalters@268
|
185 for (int iIteration = 0; iIteration < m_iParamMaxIt; iIteration++) {
|
tomwalters@268
|
186 // (re)calculate posteriors (component probability given observation)
|
tomwalters@268
|
187 // denominator: the model density at all observation points X
|
tomwalters@268
|
188 for (int i = 0; i < iSizeX; ++i) {
|
tomwalters@268
|
189 pP_mod_X[i] = 0.0f;
|
tomwalters@268
|
190 }
|
tomwalters@268
|
191
|
tomwalters@268
|
192 for (int i = 0; i < iNComponents; i++) {
|
tomwalters@268
|
193 for (int iCount = 0; iCount < iSizeX; iCount++) {
|
tomwalters@268
|
194 pP_mod_X[iCount] += 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
|
tomwalters@268
|
195 * exp((-0.5f) * pow(((float)iCount-m_pMu[i]), 2)
|
tomwalters@268
|
196 / m_fParamVar) * m_pA[i];
|
tomwalters@268
|
197 }
|
tomwalters@268
|
198 }
|
tomwalters@268
|
199
|
tomwalters@268
|
200 for (int i = 0; i < iSizeX * iNComponents; ++i) {
|
tomwalters@268
|
201 pP_comp[i] = 0.0f;
|
tomwalters@268
|
202 }
|
tomwalters@268
|
203
|
tomwalters@268
|
204 for (int i = 0; i < iNComponents; i++) {
|
tomwalters@268
|
205 for (int iCount = 0; iCount < iSizeX; iCount++) {
|
tomwalters@268
|
206 pP_comp[iCount + i * iSizeX] =
|
tomwalters@268
|
207 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
|
tomwalters@268
|
208 * exp((-0.5f) * pow(((float)iCount - m_pMu[i]), 2) / m_fParamVar);
|
tomwalters@268
|
209 pP_comp[iCount + i * iSizeX] =
|
tomwalters@268
|
210 pP_comp[iCount + i * iSizeX] * m_pA[i] / pP_mod_X[iCount];
|
tomwalters@268
|
211 }
|
tomwalters@268
|
212 }
|
tomwalters@268
|
213
|
tomwalters@268
|
214 for (int iCount = 0; iCount < iSizeX; ++iCount) {
|
tomwalters@268
|
215 float fSum = 0.0f;
|
tomwalters@268
|
216 for (int i = 0; i < iNComponents; ++i) {
|
tomwalters@268
|
217 pP_comp[iCount+i*iSizeX] = pow(pP_comp[iCount + i * iSizeX],
|
tomwalters@268
|
218 m_fParamPosteriorExp); // expansion
|
tomwalters@268
|
219 fSum += pP_comp[iCount+i*iSizeX];
|
tomwalters@268
|
220 }
|
tomwalters@268
|
221 for (int i = 0; i < iNComponents; ++i)
|
tomwalters@268
|
222 pP_comp[iCount+i*iSizeX] = pP_comp[iCount + i * iSizeX] / fSum;
|
tomwalters@268
|
223 // renormalisation
|
tomwalters@268
|
224 }
|
tomwalters@268
|
225
|
tomwalters@268
|
226 for (int i = 0; i < iNComponents; ++i) {
|
tomwalters@268
|
227 pA_old[i] = m_pA[i];
|
tomwalters@268
|
228 m_pA[i] = 0.0f;
|
tomwalters@268
|
229 for (int iCount = 0; iCount < iSizeX; ++iCount) {
|
tomwalters@268
|
230 m_pA[i] += pP_comp[iCount + i * iSizeX] * m_pSpectralProfile[iCount];
|
tomwalters@268
|
231 }
|
tomwalters@268
|
232 }
|
tomwalters@268
|
233
|
tomwalters@268
|
234 // finish when already converged
|
tomwalters@268
|
235 float fPrdist = 0.0f;
|
tomwalters@268
|
236 for (int i = 0; i < iNComponents; ++i) {
|
tomwalters@268
|
237 fPrdist += pow((m_pA[i] - pA_old[i]), 2);
|
tomwalters@268
|
238 }
|
tomwalters@268
|
239 fPrdist /= iNComponents;
|
tomwalters@268
|
240
|
tomwalters@268
|
241 if (fPrdist < m_fParamPriorsConverged) {
|
tomwalters@268
|
242 LOG_INFO("Converged!");
|
tomwalters@268
|
243 break;
|
tomwalters@268
|
244 }
|
tomwalters@268
|
245
|
tomwalters@268
|
246 // update means (positions)
|
tomwalters@268
|
247 for (int i = 0 ; i < iNComponents; ++i) {
|
tomwalters@268
|
248 float mu_old = m_pMu[i];
|
tomwalters@268
|
249 if (m_pA[i] > 0.0f) {
|
tomwalters@268
|
250 m_pMu[i] = 0.0f;
|
tomwalters@268
|
251 for (int iCount = 0; iCount < iSizeX; ++iCount) {
|
tomwalters@268
|
252 m_pMu[i] += m_pSpectralProfile[iCount]
|
tomwalters@268
|
253 * pP_comp[iCount + i * iSizeX] * (float)iCount;
|
tomwalters@268
|
254 }
|
tomwalters@268
|
255 m_pMu[i] /= m_pA[i];
|
tomwalters@268
|
256 if (isnan(m_pMu[i])) {
|
tomwalters@268
|
257 m_pMu[i] = mu_old;
|
tomwalters@268
|
258 }
|
tomwalters@268
|
259 }
|
tomwalters@268
|
260 }
|
tomwalters@268
|
261 } // loop over iterations
|
tomwalters@268
|
262
|
tomwalters@268
|
263 // Ensure they are sorted, using a really simple bubblesort
|
tomwalters@268
|
264 bool bSorted = false;
|
tomwalters@268
|
265 while (!bSorted) {
|
tomwalters@268
|
266 bSorted = true;
|
tomwalters@268
|
267 for (int i = 0; i < iNComponents - 1; ++i) {
|
tomwalters@268
|
268 if (m_pMu[i] > m_pMu[i + 1]) {
|
tomwalters@268
|
269 float fTemp = m_pMu[i];
|
tomwalters@268
|
270 m_pMu[i] = m_pMu[i + 1];
|
tomwalters@268
|
271 m_pMu[i + 1] = fTemp;
|
tomwalters@268
|
272 fTemp = m_pA[i];
|
tomwalters@268
|
273 m_pA[i] = m_pA[i + 1];
|
tomwalters@268
|
274 m_pA[i + 1] = fTemp;
|
tomwalters@268
|
275 bSorted = false;
|
tomwalters@268
|
276 }
|
tomwalters@268
|
277 }
|
tomwalters@268
|
278 }
|
tomwalters@268
|
279 return true;
|
tomwalters@268
|
280 }
|
tomwalters@268
|
281 } //namespace aimc
|
tomwalters@268
|
282
|