annotate src/Modules/Features/ModuleGaussians.cc @ 47:2204b3a05a28

-Reinstated original parameter-setting
author tomwalters
date Mon, 07 Jun 2010 08:34:49 +0000
parents c5f5e9569863
children e914b02b31b0
rev   line source
tomwalters@0 1 // Copyright 2008-2010, Thomas Walters
tomwalters@0 2 //
tomwalters@0 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@0 4 // http://www.acousticscale.org/AIMC
tomwalters@0 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@0 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@0 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@0 17
tomwalters@0 18 /*! \file
tomwalters@0 19 * \brief Gaussian features - based on MATLAB code by Christian Feldbauer
tomwalters@0 20 */
tomwalters@0 21
tomwalters@0 22 /*!
tomwalters@1 23 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@0 24 * \date created 2008/06/23
tomwalters@23 25 * \version \$Id$
tomwalters@0 26 */
tomwalters@0 27
tomwalters@0 28 #include <math.h>
tomwalters@0 29
tomwalters@0 30 #include "Modules/Features/ModuleGaussians.h"
tomwalters@0 31 #include "Support/Common.h"
tomwalters@0 32
tomwalters@0 33 namespace aimc {
tomwalters@6 34 ModuleGaussians::ModuleGaussians(Parameters *params) : Module(params) {
tomwalters@0 35 // Set module metadata
tomwalters@0 36 module_description_ = "Gaussian Fitting to SSI profile";
tomwalters@1 37 module_identifier_ = "gaussians";
tomwalters@0 38 module_type_ = "features";
tomwalters@23 39 module_version_ = "$Id$";
tomwalters@0 40
tomwalters@1 41 m_iParamNComp = parameters_->DefaultInt("features.gaussians.ncomp", 4);
tomwalters@1 42 m_fParamVar = parameters_->DefaultFloat("features.gaussians.var", 115.0);
tomwalters@1 43 m_fParamPosteriorExp =
tomwalters@1 44 parameters_->DefaultFloat("features.gaussians.posterior_exp", 6.0);
tomwalters@1 45 m_iParamMaxIt = parameters_->DefaultInt("features.gaussians.maxit", 250);
tomwalters@0 46
tomwalters@1 47 // The parameters system doesn't support tiny numbers well, to define this
tomwalters@1 48 // variable as a string, then convert it to a float afterwards
tomwalters@1 49 parameters_->DefaultString("features.gaussians.priors_converged", "1e-7");
tomwalters@0 50 m_fParamPriorsConverged =
tomwalters@1 51 parameters_->GetFloat("features.gaussians.priors_converged");
tomwalters@0 52 }
tomwalters@0 53
tomwalters@0 54 ModuleGaussians::~ModuleGaussians() {
tomwalters@0 55 }
tomwalters@0 56
tomwalters@0 57 bool ModuleGaussians::InitializeInternal(const SignalBank &input) {
tomwalters@0 58 m_pA.resize(m_iParamNComp, 0.0f);
tomwalters@0 59 m_pMu.resize(m_iParamNComp, 0.0f);
tomwalters@0 60
tomwalters@0 61 // Assuming the number of channels is greater than twice the number of
tomwalters@0 62 // Gaussian components, this is ok
tomwalters@0 63 if (input.channel_count() >= 2 * m_iParamNComp) {
tomwalters@1 64 output_.Initialize(m_iParamNComp, 1, input.sample_rate());
tomwalters@0 65 } else {
tomwalters@0 66 LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
tomwalters@0 67 "Gaussian features. Either increase the number of filterbank"
tomwalters@0 68 " channels, or decrease the number of Gaussian components"));
tomwalters@0 69 return false;
tomwalters@0 70 }
tomwalters@0 71
tomwalters@0 72 m_iNumChannels = input.channel_count();
tomwalters@0 73 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@0 74
tomwalters@0 75 return true;
tomwalters@0 76 }
tomwalters@0 77
tomwalters@3 78 void ModuleGaussians::ResetInternal() {
tomwalters@0 79 m_pSpectralProfile.clear();
tomwalters@0 80 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@20 81 m_pA.clear();
tomwalters@20 82 m_pA.resize(m_iParamNComp, 0.0f);
tomwalters@20 83 m_pMu.clear();
tomwalters@20 84 m_pMu.resize(m_iParamNComp, 0.0f);
tomwalters@0 85 }
tomwalters@0 86
tomwalters@0 87 void ModuleGaussians::Process(const SignalBank &input) {
tomwalters@1 88 if (!initialized_) {
tomwalters@1 89 LOG_ERROR(_T("Module ModuleGaussians not initialized."));
tomwalters@1 90 return;
tomwalters@1 91 }
tomwalters@0 92 // Calculate spectral profile
tomwalters@0 93 for (int iChannel = 0;
tomwalters@0 94 iChannel < input.channel_count();
tomwalters@0 95 ++iChannel) {
tomwalters@0 96 m_pSpectralProfile[iChannel] = 0.0f;
tomwalters@0 97 for (int iSample = 0;
tomwalters@0 98 iSample < input.buffer_length();
tomwalters@0 99 ++iSample) {
tomwalters@0 100 m_pSpectralProfile[iChannel] += input[iChannel][iSample];
tomwalters@0 101 }
tomwalters@8 102 m_pSpectralProfile[iChannel] /= static_cast<float>(input.buffer_length());
tomwalters@1 103 }
tomwalters@1 104
tomwalters@8 105 float spectral_profile_sum = 0.0f;
tomwalters@1 106 for (int i = 0; i < input.channel_count(); ++i) {
tomwalters@1 107 spectral_profile_sum += m_pSpectralProfile[i];
tomwalters@1 108 }
tomwalters@1 109
tomwalters@8 110 float logsum = log(spectral_profile_sum);
tomwalters@1 111 if (!isinf(logsum)) {
tomwalters@1 112 output_.set_sample(m_iParamNComp - 1, 0, logsum);
tomwalters@1 113 } else {
tomwalters@1 114 output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
tomwalters@0 115 }
tomwalters@0 116
tomwalters@0 117 for (int iChannel = 0;
tomwalters@0 118 iChannel < input.channel_count();
tomwalters@0 119 ++iChannel) {
tomwalters@0 120 m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8);
tomwalters@0 121 }
tomwalters@0 122
tomwalters@0 123 RubberGMMCore(2, true);
tomwalters@0 124
tomwalters@8 125 float fMean1 = m_pMu[0];
tomwalters@8 126 float fMean2 = m_pMu[1];
tomwalters@8 127 // LOG_INFO(_T("Orig. mean 0 = %f"), m_pMu[0]);
tomwalters@8 128 // LOG_INFO(_T("Orig. mean 1 = %f"), m_pMu[1]);
tomwalters@8 129 // LOG_INFO(_T("Orig. prob 0 = %f"), m_pA[0]);
tomwalters@8 130 // LOG_INFO(_T("Orig. prob 1 = %f"), m_pA[1]);
tomwalters@0 131
tomwalters@8 132 float fA1 = 0.05 * m_pA[0];
tomwalters@8 133 float fA2 = 1.0 - 0.25 * m_pA[1];
tomwalters@0 134
tomwalters@8 135 // LOG_INFO(_T("fA1 = %f"), fA1);
tomwalters@8 136 // LOG_INFO(_T("fA2 = %f"), fA2);
tomwalters@2 137
tomwalters@8 138 float fGradient = (fMean2 - fMean1) / (fA2 - fA1);
tomwalters@8 139 float fIntercept = fMean2 - fGradient * fA2;
tomwalters@2 140
tomwalters@8 141 // LOG_INFO(_T("fGradient = %f"), fGradient);
tomwalters@8 142 // LOG_INFO(_T("fIntercept = %f"), fIntercept);
tomwalters@0 143
tomwalters@0 144 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@8 145 m_pMu[i] = (static_cast<float>(i)
tomwalters@8 146 / (static_cast<float>(m_iParamNComp) - 1.0f))
tomwalters@8 147 * fGradient + fIntercept;
tomwalters@8 148 // LOG_INFO(_T("mean %d = %f"), i, m_pMu[i]);
tomwalters@0 149 }
tomwalters@0 150
tomwalters@0 151 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@8 152 m_pA[i] = 1.0f / static_cast<float>(m_iParamNComp);
tomwalters@0 153 }
tomwalters@0 154
tomwalters@0 155 RubberGMMCore(m_iParamNComp, false);
tomwalters@0 156
tomwalters@0 157 for (int i = 0; i < m_iParamNComp - 1; ++i) {
tomwalters@0 158 if (!isnan(m_pA[i])) {
tomwalters@0 159 output_.set_sample(i, 0, m_pA[i]);
tomwalters@0 160 } else {
tomwalters@0 161 output_.set_sample(i, 0, 0.0f);
tomwalters@0 162 }
tomwalters@0 163 }
tomwalters@1 164
tomwalters@0 165 PushOutput();
tomwalters@0 166 }
tomwalters@0 167
tomwalters@0 168 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) {
tomwalters@0 169 int iSizeX = m_iNumChannels;
tomwalters@0 170
tomwalters@0 171 // Normalise the spectral profile
tomwalters@8 172 float fSpectralProfileTotal = 0.0f;
tomwalters@0 173 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 174 fSpectralProfileTotal += m_pSpectralProfile[iCount];
tomwalters@0 175 }
tomwalters@0 176 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 177 m_pSpectralProfile[iCount] /= fSpectralProfileTotal;
tomwalters@0 178 }
tomwalters@0 179
tomwalters@0 180 if (bDoInit) {
tomwalters@0 181 // Uniformly spaced components
tomwalters@8 182 float dd = (iSizeX - 1.0f) / iNComponents;
tomwalters@0 183 for (int i = 0; i < iNComponents; i++) {
tomwalters@0 184 m_pMu[i] = dd / 2.0f + (i * dd);
tomwalters@0 185 m_pA[i] = 1.0f / iNComponents;
tomwalters@0 186 }
tomwalters@0 187 }
tomwalters@0 188
tomwalters@8 189 vector<float> pA_old;
tomwalters@0 190 pA_old.resize(iNComponents);
tomwalters@8 191 vector<float> pP_mod_X;
tomwalters@0 192 pP_mod_X.resize(iSizeX);
tomwalters@8 193 vector<float> pP_comp;
tomwalters@0 194 pP_comp.resize(iSizeX * iNComponents);
tomwalters@0 195
tomwalters@0 196 for (int iIteration = 0; iIteration < m_iParamMaxIt; iIteration++) {
tomwalters@0 197 // (re)calculate posteriors (component probability given observation)
tomwalters@0 198 // denominator: the model density at all observation points X
tomwalters@0 199 for (int i = 0; i < iSizeX; ++i) {
tomwalters@0 200 pP_mod_X[i] = 0.0f;
tomwalters@0 201 }
tomwalters@0 202
tomwalters@0 203 for (int i = 0; i < iNComponents; i++) {
tomwalters@0 204 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 205 pP_mod_X[iCount] += 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@8 206 * exp((-0.5f)
tomwalters@8 207 * pow(static_cast<float>(iCount+1) - m_pMu[i], 2)
tomwalters@8 208 / m_fParamVar) * m_pA[i];
tomwalters@0 209 }
tomwalters@0 210 }
tomwalters@0 211
tomwalters@0 212 for (int i = 0; i < iSizeX * iNComponents; ++i) {
tomwalters@0 213 pP_comp[i] = 0.0f;
tomwalters@0 214 }
tomwalters@0 215
tomwalters@0 216 for (int i = 0; i < iNComponents; i++) {
tomwalters@0 217 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 218 pP_comp[iCount + i * iSizeX] =
tomwalters@0 219 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@8 220 * exp((-0.5f) * pow((static_cast<float>(iCount + 1) - m_pMu[i]), 2)
tomwalters@8 221 / m_fParamVar);
tomwalters@0 222 pP_comp[iCount + i * iSizeX] =
tomwalters@0 223 pP_comp[iCount + i * iSizeX] * m_pA[i] / pP_mod_X[iCount];
tomwalters@0 224 }
tomwalters@0 225 }
tomwalters@0 226
tomwalters@0 227 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@8 228 float fSum = 0.0f;
tomwalters@0 229 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 230 pP_comp[iCount+i*iSizeX] = pow(pP_comp[iCount + i * iSizeX],
tomwalters@8 231 m_fParamPosteriorExp); // expansion
tomwalters@0 232 fSum += pP_comp[iCount+i*iSizeX];
tomwalters@0 233 }
tomwalters@0 234 for (int i = 0; i < iNComponents; ++i)
tomwalters@0 235 pP_comp[iCount+i*iSizeX] = pP_comp[iCount + i * iSizeX] / fSum;
tomwalters@0 236 // renormalisation
tomwalters@0 237 }
tomwalters@0 238
tomwalters@0 239 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 240 pA_old[i] = m_pA[i];
tomwalters@0 241 m_pA[i] = 0.0f;
tomwalters@0 242 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@0 243 m_pA[i] += pP_comp[iCount + i * iSizeX] * m_pSpectralProfile[iCount];
tomwalters@0 244 }
tomwalters@0 245 }
tomwalters@0 246
tomwalters@0 247 // finish when already converged
tomwalters@8 248 float fPrdist = 0.0f;
tomwalters@0 249 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 250 fPrdist += pow((m_pA[i] - pA_old[i]), 2);
tomwalters@0 251 }
tomwalters@0 252 fPrdist /= iNComponents;
tomwalters@0 253
tomwalters@0 254 if (fPrdist < m_fParamPriorsConverged) {
tomwalters@8 255 // LOG_INFO("Converged!");
tomwalters@0 256 break;
tomwalters@0 257 }
tomwalters@8 258 // LOG_INFO("Didn't converge!");
tomwalters@2 259
tomwalters@0 260
tomwalters@0 261 // update means (positions)
tomwalters@0 262 for (int i = 0 ; i < iNComponents; ++i) {
tomwalters@8 263 float mu_old = m_pMu[i];
tomwalters@0 264 if (m_pA[i] > 0.0f) {
tomwalters@0 265 m_pMu[i] = 0.0f;
tomwalters@0 266 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@0 267 m_pMu[i] += m_pSpectralProfile[iCount]
tomwalters@8 268 * pP_comp[iCount + i * iSizeX]
tomwalters@8 269 * static_cast<float>(iCount + 1);
tomwalters@0 270 }
tomwalters@0 271 m_pMu[i] /= m_pA[i];
tomwalters@0 272 if (isnan(m_pMu[i])) {
tomwalters@0 273 m_pMu[i] = mu_old;
tomwalters@0 274 }
tomwalters@0 275 }
tomwalters@0 276 }
tomwalters@8 277 } // loop over iterations
tomwalters@0 278
tomwalters@0 279 // Ensure they are sorted, using a really simple bubblesort
tomwalters@0 280 bool bSorted = false;
tomwalters@0 281 while (!bSorted) {
tomwalters@0 282 bSorted = true;
tomwalters@0 283 for (int i = 0; i < iNComponents - 1; ++i) {
tomwalters@0 284 if (m_pMu[i] > m_pMu[i + 1]) {
tomwalters@8 285 float fTemp = m_pMu[i];
tomwalters@0 286 m_pMu[i] = m_pMu[i + 1];
tomwalters@0 287 m_pMu[i + 1] = fTemp;
tomwalters@0 288 fTemp = m_pA[i];
tomwalters@0 289 m_pA[i] = m_pA[i + 1];
tomwalters@0 290 m_pA[i + 1] = fTemp;
tomwalters@0 291 bSorted = false;
tomwalters@0 292 }
tomwalters@0 293 }
tomwalters@0 294 }
tomwalters@0 295 return true;
tomwalters@0 296 }
tomwalters@8 297 } // namespace aimc
tomwalters@0 298