annotate trunk/src/Modules/Features/ModuleGaussians.cc @ 296:fe5ce00a64f5

-Added AIMCopy, a replacement for HTK's HCopy -Set "Id" keyword on all .cc, .h and .py files -Added FileList class to aupport AIMCopy -Added a first go at a Module factory class. It's not to be used at the moment, but it will serve as a reminder to implement a proper factory soon.
author tomwalters
date Tue, 23 Feb 2010 12:47:01 +0000
parents 10d0803e37ec
children 30dde71d0230
rev   line source
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@273 24 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@268 25 * \date created 2008/06/23
tomwalters@296 26 * \version \$Id$
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@278 35 ModuleGaussians::ModuleGaussians(Parameters *params) : Module(params) {
tomwalters@268 36 // Set module metadata
tomwalters@268 37 module_description_ = "Gaussian Fitting to SSI profile";
tomwalters@273 38 module_identifier_ = "gaussians";
tomwalters@268 39 module_type_ = "features";
tomwalters@296 40 module_version_ = "$Id$";
tomwalters@268 41
tomwalters@273 42 m_iParamNComp = parameters_->DefaultInt("features.gaussians.ncomp", 4);
tomwalters@273 43 m_fParamVar = parameters_->DefaultFloat("features.gaussians.var", 115.0);
tomwalters@273 44 m_fParamPosteriorExp =
tomwalters@273 45 parameters_->DefaultFloat("features.gaussians.posterior_exp", 6.0);
tomwalters@273 46 m_iParamMaxIt = parameters_->DefaultInt("features.gaussians.maxit", 250);
tomwalters@268 47
tomwalters@273 48 // The parameters system doesn't support tiny numbers well, to define this
tomwalters@273 49 // variable as a string, then convert it to a float afterwards
tomwalters@273 50 parameters_->DefaultString("features.gaussians.priors_converged", "1e-7");
tomwalters@268 51 m_fParamPriorsConverged =
tomwalters@273 52 parameters_->GetFloat("features.gaussians.priors_converged");
tomwalters@268 53 }
tomwalters@268 54
tomwalters@268 55 ModuleGaussians::~ModuleGaussians() {
tomwalters@268 56 }
tomwalters@268 57
tomwalters@268 58 bool ModuleGaussians::InitializeInternal(const SignalBank &input) {
tomwalters@268 59 m_pA.resize(m_iParamNComp, 0.0f);
tomwalters@268 60 m_pMu.resize(m_iParamNComp, 0.0f);
tomwalters@268 61
tomwalters@268 62 // Assuming the number of channels is greater than twice the number of
tomwalters@268 63 // Gaussian components, this is ok
tomwalters@268 64 if (input.channel_count() >= 2 * m_iParamNComp) {
tomwalters@273 65 output_.Initialize(m_iParamNComp, 1, input.sample_rate());
tomwalters@268 66 } else {
tomwalters@268 67 LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
tomwalters@268 68 "Gaussian features. Either increase the number of filterbank"
tomwalters@268 69 " channels, or decrease the number of Gaussian components"));
tomwalters@268 70 return false;
tomwalters@268 71 }
tomwalters@268 72
tomwalters@268 73 m_iNumChannels = input.channel_count();
tomwalters@268 74 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@268 75
tomwalters@268 76 return true;
tomwalters@268 77 }
tomwalters@268 78
tomwalters@275 79 void ModuleGaussians::ResetInternal() {
tomwalters@268 80 m_pSpectralProfile.clear();
tomwalters@268 81 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@292 82 m_pA.clear();
tomwalters@292 83 m_pA.resize(m_iParamNComp, 0.0f);
tomwalters@292 84 m_pMu.clear();
tomwalters@292 85 m_pMu.resize(m_iParamNComp, 0.0f);
tomwalters@268 86 }
tomwalters@268 87
tomwalters@268 88 void ModuleGaussians::Process(const SignalBank &input) {
tomwalters@273 89 if (!initialized_) {
tomwalters@273 90 LOG_ERROR(_T("Module ModuleGaussians not initialized."));
tomwalters@273 91 return;
tomwalters@273 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@280 103 m_pSpectralProfile[iChannel] /= static_cast<float>(input.buffer_length());
tomwalters@273 104 }
tomwalters@273 105
tomwalters@280 106 float spectral_profile_sum = 0.0f;
tomwalters@273 107 for (int i = 0; i < input.channel_count(); ++i) {
tomwalters@273 108 spectral_profile_sum += m_pSpectralProfile[i];
tomwalters@273 109 }
tomwalters@273 110
tomwalters@280 111 float logsum = log(spectral_profile_sum);
tomwalters@273 112 if (!isinf(logsum)) {
tomwalters@273 113 output_.set_sample(m_iParamNComp - 1, 0, logsum);
tomwalters@273 114 } else {
tomwalters@273 115 output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
tomwalters@268 116 }
tomwalters@268 117
tomwalters@268 118 for (int iChannel = 0;
tomwalters@268 119 iChannel < input.channel_count();
tomwalters@268 120 ++iChannel) {
tomwalters@268 121 m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8);
tomwalters@268 122 }
tomwalters@268 123
tomwalters@268 124 RubberGMMCore(2, true);
tomwalters@268 125
tomwalters@280 126 float fMean1 = m_pMu[0];
tomwalters@280 127 float fMean2 = m_pMu[1];
tomwalters@280 128 // LOG_INFO(_T("Orig. mean 0 = %f"), m_pMu[0]);
tomwalters@280 129 // LOG_INFO(_T("Orig. mean 1 = %f"), m_pMu[1]);
tomwalters@280 130 // LOG_INFO(_T("Orig. prob 0 = %f"), m_pA[0]);
tomwalters@280 131 // LOG_INFO(_T("Orig. prob 1 = %f"), m_pA[1]);
tomwalters@268 132
tomwalters@280 133 float fA1 = 0.05 * m_pA[0];
tomwalters@280 134 float fA2 = 1.0 - 0.25 * m_pA[1];
tomwalters@268 135
tomwalters@280 136 // LOG_INFO(_T("fA1 = %f"), fA1);
tomwalters@280 137 // LOG_INFO(_T("fA2 = %f"), fA2);
tomwalters@274 138
tomwalters@280 139 float fGradient = (fMean2 - fMean1) / (fA2 - fA1);
tomwalters@280 140 float fIntercept = fMean2 - fGradient * fA2;
tomwalters@274 141
tomwalters@280 142 // LOG_INFO(_T("fGradient = %f"), fGradient);
tomwalters@280 143 // LOG_INFO(_T("fIntercept = %f"), fIntercept);
tomwalters@268 144
tomwalters@268 145 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@280 146 m_pMu[i] = (static_cast<float>(i)
tomwalters@280 147 / (static_cast<float>(m_iParamNComp) - 1.0f))
tomwalters@280 148 * fGradient + fIntercept;
tomwalters@280 149 // LOG_INFO(_T("mean %d = %f"), i, m_pMu[i]);
tomwalters@268 150 }
tomwalters@268 151
tomwalters@268 152 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@280 153 m_pA[i] = 1.0f / static_cast<float>(m_iParamNComp);
tomwalters@268 154 }
tomwalters@268 155
tomwalters@268 156 RubberGMMCore(m_iParamNComp, false);
tomwalters@268 157
tomwalters@268 158 for (int i = 0; i < m_iParamNComp - 1; ++i) {
tomwalters@268 159 if (!isnan(m_pA[i])) {
tomwalters@268 160 output_.set_sample(i, 0, m_pA[i]);
tomwalters@268 161 } else {
tomwalters@268 162 output_.set_sample(i, 0, 0.0f);
tomwalters@268 163 }
tomwalters@268 164 }
tomwalters@273 165
tomwalters@268 166 PushOutput();
tomwalters@268 167 }
tomwalters@268 168
tomwalters@268 169 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) {
tomwalters@268 170 int iSizeX = m_iNumChannels;
tomwalters@268 171
tomwalters@268 172 // Normalise the spectral profile
tomwalters@280 173 float fSpectralProfileTotal = 0.0f;
tomwalters@268 174 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@268 175 fSpectralProfileTotal += m_pSpectralProfile[iCount];
tomwalters@268 176 }
tomwalters@268 177 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@268 178 m_pSpectralProfile[iCount] /= fSpectralProfileTotal;
tomwalters@268 179 }
tomwalters@268 180
tomwalters@268 181 if (bDoInit) {
tomwalters@268 182 // Uniformly spaced components
tomwalters@280 183 float dd = (iSizeX - 1.0f) / iNComponents;
tomwalters@268 184 for (int i = 0; i < iNComponents; i++) {
tomwalters@268 185 m_pMu[i] = dd / 2.0f + (i * dd);
tomwalters@268 186 m_pA[i] = 1.0f / iNComponents;
tomwalters@268 187 }
tomwalters@268 188 }
tomwalters@268 189
tomwalters@280 190 vector<float> pA_old;
tomwalters@268 191 pA_old.resize(iNComponents);
tomwalters@280 192 vector<float> pP_mod_X;
tomwalters@268 193 pP_mod_X.resize(iSizeX);
tomwalters@280 194 vector<float> pP_comp;
tomwalters@268 195 pP_comp.resize(iSizeX * iNComponents);
tomwalters@268 196
tomwalters@268 197 for (int iIteration = 0; iIteration < m_iParamMaxIt; iIteration++) {
tomwalters@268 198 // (re)calculate posteriors (component probability given observation)
tomwalters@268 199 // denominator: the model density at all observation points X
tomwalters@268 200 for (int i = 0; i < iSizeX; ++i) {
tomwalters@268 201 pP_mod_X[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_mod_X[iCount] += 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@280 207 * exp((-0.5f)
tomwalters@280 208 * pow(static_cast<float>(iCount+1) - m_pMu[i], 2)
tomwalters@280 209 / m_fParamVar) * m_pA[i];
tomwalters@268 210 }
tomwalters@268 211 }
tomwalters@268 212
tomwalters@268 213 for (int i = 0; i < iSizeX * iNComponents; ++i) {
tomwalters@268 214 pP_comp[i] = 0.0f;
tomwalters@268 215 }
tomwalters@268 216
tomwalters@268 217 for (int i = 0; i < iNComponents; i++) {
tomwalters@268 218 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@268 219 pP_comp[iCount + i * iSizeX] =
tomwalters@268 220 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@280 221 * exp((-0.5f) * pow((static_cast<float>(iCount + 1) - m_pMu[i]), 2)
tomwalters@280 222 / m_fParamVar);
tomwalters@268 223 pP_comp[iCount + i * iSizeX] =
tomwalters@268 224 pP_comp[iCount + i * iSizeX] * m_pA[i] / pP_mod_X[iCount];
tomwalters@268 225 }
tomwalters@268 226 }
tomwalters@268 227
tomwalters@268 228 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@280 229 float fSum = 0.0f;
tomwalters@268 230 for (int i = 0; i < iNComponents; ++i) {
tomwalters@268 231 pP_comp[iCount+i*iSizeX] = pow(pP_comp[iCount + i * iSizeX],
tomwalters@280 232 m_fParamPosteriorExp); // expansion
tomwalters@268 233 fSum += pP_comp[iCount+i*iSizeX];
tomwalters@268 234 }
tomwalters@268 235 for (int i = 0; i < iNComponents; ++i)
tomwalters@268 236 pP_comp[iCount+i*iSizeX] = pP_comp[iCount + i * iSizeX] / fSum;
tomwalters@268 237 // renormalisation
tomwalters@268 238 }
tomwalters@268 239
tomwalters@268 240 for (int i = 0; i < iNComponents; ++i) {
tomwalters@268 241 pA_old[i] = m_pA[i];
tomwalters@268 242 m_pA[i] = 0.0f;
tomwalters@268 243 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@268 244 m_pA[i] += pP_comp[iCount + i * iSizeX] * m_pSpectralProfile[iCount];
tomwalters@268 245 }
tomwalters@268 246 }
tomwalters@268 247
tomwalters@268 248 // finish when already converged
tomwalters@280 249 float fPrdist = 0.0f;
tomwalters@268 250 for (int i = 0; i < iNComponents; ++i) {
tomwalters@268 251 fPrdist += pow((m_pA[i] - pA_old[i]), 2);
tomwalters@268 252 }
tomwalters@268 253 fPrdist /= iNComponents;
tomwalters@268 254
tomwalters@268 255 if (fPrdist < m_fParamPriorsConverged) {
tomwalters@280 256 // LOG_INFO("Converged!");
tomwalters@268 257 break;
tomwalters@268 258 }
tomwalters@280 259 // LOG_INFO("Didn't converge!");
tomwalters@274 260
tomwalters@268 261
tomwalters@268 262 // update means (positions)
tomwalters@268 263 for (int i = 0 ; i < iNComponents; ++i) {
tomwalters@280 264 float mu_old = m_pMu[i];
tomwalters@268 265 if (m_pA[i] > 0.0f) {
tomwalters@268 266 m_pMu[i] = 0.0f;
tomwalters@268 267 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@268 268 m_pMu[i] += m_pSpectralProfile[iCount]
tomwalters@280 269 * pP_comp[iCount + i * iSizeX]
tomwalters@280 270 * static_cast<float>(iCount + 1);
tomwalters@268 271 }
tomwalters@268 272 m_pMu[i] /= m_pA[i];
tomwalters@268 273 if (isnan(m_pMu[i])) {
tomwalters@268 274 m_pMu[i] = mu_old;
tomwalters@268 275 }
tomwalters@268 276 }
tomwalters@268 277 }
tomwalters@280 278 } // loop over iterations
tomwalters@268 279
tomwalters@268 280 // Ensure they are sorted, using a really simple bubblesort
tomwalters@268 281 bool bSorted = false;
tomwalters@268 282 while (!bSorted) {
tomwalters@268 283 bSorted = true;
tomwalters@268 284 for (int i = 0; i < iNComponents - 1; ++i) {
tomwalters@268 285 if (m_pMu[i] > m_pMu[i + 1]) {
tomwalters@280 286 float fTemp = m_pMu[i];
tomwalters@268 287 m_pMu[i] = m_pMu[i + 1];
tomwalters@268 288 m_pMu[i + 1] = fTemp;
tomwalters@268 289 fTemp = m_pA[i];
tomwalters@268 290 m_pA[i] = m_pA[i + 1];
tomwalters@268 291 m_pA[i + 1] = fTemp;
tomwalters@268 292 bSorted = false;
tomwalters@268 293 }
tomwalters@268 294 }
tomwalters@268 295 }
tomwalters@268 296 return true;
tomwalters@268 297 }
tomwalters@280 298 } // namespace aimc
tomwalters@268 299