annotate src/Modules/Features/ModuleGaussians.cc @ 101:9416e88d7c56

- Pretty-plotting - Test on everything - Generalised beyond standard AMI
author tomwalters
date Tue, 14 Sep 2010 00:18:47 +0000
parents bee31e7ebf4b
children 4abed4cf1e87
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@84 41 m_iParamNComp = parameters_->DefaultInt("gaussians.ncomp", 4);
tomwalters@84 42 m_fParamVar = parameters_->DefaultFloat("gaussians.var", 115.0);
tomwalters@84 43 m_fParamPosteriorExp = parameters_->DefaultFloat("gaussians.posterior_exp",
tomwalters@84 44 6.0);
tomwalters@84 45 m_iParamMaxIt = parameters_->DefaultInt("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@84 49 parameters_->DefaultString("gaussians.priors_converged", "1e-7");
tomwalters@84 50 priors_converged_ = parameters_->GetFloat("gaussians.priors_converged");
tomwalters@84 51 output_positions_ = parameters_->DefaultBool("gaussians.positions", false);
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@84 63 output_component_count_ = 1; // Energy component
tomwalters@0 64 if (input.channel_count() >= 2 * m_iParamNComp) {
tomwalters@84 65 output_component_count_ += (m_iParamNComp - 1);
tomwalters@0 66 } else {
tomwalters@0 67 LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
tomwalters@0 68 "Gaussian features. Either increase the number of filterbank"
tomwalters@0 69 " channels, or decrease the number of Gaussian components"));
tomwalters@0 70 return false;
tomwalters@0 71 }
tomwalters@0 72
tomwalters@84 73 if (output_positions_) {
tomwalters@84 74 output_component_count_ += m_iParamNComp;
tomwalters@84 75 }
tomwalters@84 76
tomwalters@84 77 output_.Initialize(output_component_count_, 1, input.sample_rate());
tomwalters@84 78
tomwalters@0 79 m_iNumChannels = input.channel_count();
tomwalters@0 80 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@0 81
tomwalters@0 82 return true;
tomwalters@0 83 }
tomwalters@0 84
tomwalters@3 85 void ModuleGaussians::ResetInternal() {
tomwalters@0 86 m_pSpectralProfile.clear();
tomwalters@0 87 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
tomwalters@20 88 m_pA.clear();
tomwalters@20 89 m_pA.resize(m_iParamNComp, 0.0f);
tomwalters@20 90 m_pMu.clear();
tomwalters@20 91 m_pMu.resize(m_iParamNComp, 0.0f);
tomwalters@0 92 }
tomwalters@0 93
tomwalters@0 94 void ModuleGaussians::Process(const SignalBank &input) {
tomwalters@1 95 if (!initialized_) {
tomwalters@1 96 LOG_ERROR(_T("Module ModuleGaussians not initialized."));
tomwalters@1 97 return;
tomwalters@1 98 }
tomwalters@0 99 // Calculate spectral profile
tomwalters@84 100 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@84 101 m_pSpectralProfile[ch] = 0.0f;
tomwalters@84 102 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@84 103 m_pSpectralProfile[ch] += input[ch][i];
tomwalters@0 104 }
tomwalters@84 105 m_pSpectralProfile[ch] /= static_cast<float>(input.buffer_length());
tomwalters@1 106 }
tomwalters@1 107
tomwalters@8 108 float spectral_profile_sum = 0.0f;
tomwalters@1 109 for (int i = 0; i < input.channel_count(); ++i) {
tomwalters@1 110 spectral_profile_sum += m_pSpectralProfile[i];
tomwalters@1 111 }
tomwalters@1 112
tomwalters@84 113 // Set the last component of the feature vector to be the log energy
tomwalters@8 114 float logsum = log(spectral_profile_sum);
tomwalters@1 115 if (!isinf(logsum)) {
tomwalters@84 116 output_.set_sample(output_component_count_ - 1, 0, logsum);
tomwalters@1 117 } else {
tomwalters@84 118 output_.set_sample(output_component_count_ - 1, 0, -1000.0);
tomwalters@0 119 }
tomwalters@0 120
tomwalters@84 121 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@84 122 m_pSpectralProfile[ch] = pow(m_pSpectralProfile[ch], 0.8);
tomwalters@0 123 }
tomwalters@0 124
tomwalters@0 125 RubberGMMCore(2, true);
tomwalters@0 126
tomwalters@84 127 float mean1 = m_pMu[0];
tomwalters@84 128 float mean2 = m_pMu[1];
tomwalters@8 129 // LOG_INFO(_T("Orig. mean 0 = %f"), m_pMu[0]);
tomwalters@8 130 // LOG_INFO(_T("Orig. mean 1 = %f"), m_pMu[1]);
tomwalters@8 131 // LOG_INFO(_T("Orig. prob 0 = %f"), m_pA[0]);
tomwalters@8 132 // LOG_INFO(_T("Orig. prob 1 = %f"), m_pA[1]);
tomwalters@0 133
tomwalters@84 134 float a1 = 0.05 * m_pA[0];
tomwalters@84 135 float a2 = 1.0 - 0.25 * m_pA[1];
tomwalters@0 136
tomwalters@8 137 // LOG_INFO(_T("fA1 = %f"), fA1);
tomwalters@8 138 // LOG_INFO(_T("fA2 = %f"), fA2);
tomwalters@2 139
tomwalters@84 140 float gradient = (mean2 - mean1) / (a2 - a1);
tomwalters@84 141 float intercept = mean2 - gradient * a2;
tomwalters@2 142
tomwalters@8 143 // LOG_INFO(_T("fGradient = %f"), fGradient);
tomwalters@8 144 // LOG_INFO(_T("fIntercept = %f"), fIntercept);
tomwalters@0 145
tomwalters@0 146 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@8 147 m_pMu[i] = (static_cast<float>(i)
tomwalters@8 148 / (static_cast<float>(m_iParamNComp) - 1.0f))
tomwalters@84 149 * gradient + intercept;
tomwalters@8 150 // LOG_INFO(_T("mean %d = %f"), i, m_pMu[i]);
tomwalters@0 151 }
tomwalters@0 152
tomwalters@0 153 for (int i = 0; i < m_iParamNComp; ++i) {
tomwalters@8 154 m_pA[i] = 1.0f / static_cast<float>(m_iParamNComp);
tomwalters@0 155 }
tomwalters@0 156
tomwalters@0 157 RubberGMMCore(m_iParamNComp, false);
tomwalters@0 158
tomwalters@84 159 // Amplitudes first
tomwalters@0 160 for (int i = 0; i < m_iParamNComp - 1; ++i) {
tomwalters@0 161 if (!isnan(m_pA[i])) {
tomwalters@0 162 output_.set_sample(i, 0, m_pA[i]);
tomwalters@0 163 } else {
tomwalters@0 164 output_.set_sample(i, 0, 0.0f);
tomwalters@0 165 }
tomwalters@0 166 }
tomwalters@1 167
tomwalters@84 168 // Then means if required
tomwalters@84 169 if (output_positions_) {
tomwalters@84 170 int idx = 0;
tomwalters@84 171 for (int i = m_iParamNComp - 1; i < 2 * m_iParamNComp - 1; ++i) {
tomwalters@84 172 if (!isnan(m_pMu[i])) {
tomwalters@84 173 output_.set_sample(i, 0, m_pMu[idx]);
tomwalters@84 174 } else {
tomwalters@84 175 output_.set_sample(i, 0, 0.0f);
tomwalters@84 176 }
tomwalters@84 177 ++idx;
tomwalters@84 178 }
tomwalters@84 179 }
tomwalters@84 180
tomwalters@0 181 PushOutput();
tomwalters@0 182 }
tomwalters@0 183
tomwalters@0 184 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) {
tomwalters@0 185 int iSizeX = m_iNumChannels;
tomwalters@0 186
tomwalters@0 187 // Normalise the spectral profile
tomwalters@84 188 float SpectralProfileTotal = 0.0f;
tomwalters@0 189 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@84 190 SpectralProfileTotal += m_pSpectralProfile[iCount];
tomwalters@0 191 }
tomwalters@0 192 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@84 193 m_pSpectralProfile[iCount] /= SpectralProfileTotal;
tomwalters@0 194 }
tomwalters@0 195
tomwalters@0 196 if (bDoInit) {
tomwalters@0 197 // Uniformly spaced components
tomwalters@8 198 float dd = (iSizeX - 1.0f) / iNComponents;
tomwalters@0 199 for (int i = 0; i < iNComponents; i++) {
tomwalters@0 200 m_pMu[i] = dd / 2.0f + (i * dd);
tomwalters@0 201 m_pA[i] = 1.0f / iNComponents;
tomwalters@0 202 }
tomwalters@0 203 }
tomwalters@0 204
tomwalters@8 205 vector<float> pA_old;
tomwalters@0 206 pA_old.resize(iNComponents);
tomwalters@8 207 vector<float> pP_mod_X;
tomwalters@0 208 pP_mod_X.resize(iSizeX);
tomwalters@8 209 vector<float> pP_comp;
tomwalters@0 210 pP_comp.resize(iSizeX * iNComponents);
tomwalters@0 211
tomwalters@0 212 for (int iIteration = 0; iIteration < m_iParamMaxIt; iIteration++) {
tomwalters@0 213 // (re)calculate posteriors (component probability given observation)
tomwalters@0 214 // denominator: the model density at all observation points X
tomwalters@0 215 for (int i = 0; i < iSizeX; ++i) {
tomwalters@0 216 pP_mod_X[i] = 0.0f;
tomwalters@0 217 }
tomwalters@0 218
tomwalters@84 219 for (int c = 0; c < iNComponents; c++) {
tomwalters@0 220 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 221 pP_mod_X[iCount] += 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@8 222 * exp((-0.5f)
tomwalters@84 223 * pow(static_cast<float>(iCount+1) - m_pMu[c], 2)
tomwalters@84 224 / m_fParamVar) * m_pA[c];
tomwalters@0 225 }
tomwalters@0 226 }
tomwalters@0 227
tomwalters@0 228 for (int i = 0; i < iSizeX * iNComponents; ++i) {
tomwalters@0 229 pP_comp[i] = 0.0f;
tomwalters@0 230 }
tomwalters@0 231
tomwalters@0 232 for (int i = 0; i < iNComponents; i++) {
tomwalters@0 233 for (int iCount = 0; iCount < iSizeX; iCount++) {
tomwalters@0 234 pP_comp[iCount + i * iSizeX] =
tomwalters@0 235 1.0f / sqrt(2.0f * M_PI * m_fParamVar)
tomwalters@8 236 * exp((-0.5f) * pow((static_cast<float>(iCount + 1) - m_pMu[i]), 2)
tomwalters@8 237 / m_fParamVar);
tomwalters@0 238 pP_comp[iCount + i * iSizeX] =
tomwalters@0 239 pP_comp[iCount + i * iSizeX] * m_pA[i] / pP_mod_X[iCount];
tomwalters@0 240 }
tomwalters@0 241 }
tomwalters@0 242
tomwalters@0 243 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@8 244 float fSum = 0.0f;
tomwalters@0 245 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 246 pP_comp[iCount+i*iSizeX] = pow(pP_comp[iCount + i * iSizeX],
tomwalters@8 247 m_fParamPosteriorExp); // expansion
tomwalters@0 248 fSum += pP_comp[iCount+i*iSizeX];
tomwalters@0 249 }
tomwalters@0 250 for (int i = 0; i < iNComponents; ++i)
tomwalters@0 251 pP_comp[iCount+i*iSizeX] = pP_comp[iCount + i * iSizeX] / fSum;
tomwalters@0 252 // renormalisation
tomwalters@0 253 }
tomwalters@0 254
tomwalters@0 255 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 256 pA_old[i] = m_pA[i];
tomwalters@0 257 m_pA[i] = 0.0f;
tomwalters@0 258 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@0 259 m_pA[i] += pP_comp[iCount + i * iSizeX] * m_pSpectralProfile[iCount];
tomwalters@0 260 }
tomwalters@0 261 }
tomwalters@0 262
tomwalters@0 263 // finish when already converged
tomwalters@8 264 float fPrdist = 0.0f;
tomwalters@0 265 for (int i = 0; i < iNComponents; ++i) {
tomwalters@0 266 fPrdist += pow((m_pA[i] - pA_old[i]), 2);
tomwalters@0 267 }
tomwalters@0 268 fPrdist /= iNComponents;
tomwalters@0 269
tomwalters@84 270 if (fPrdist < priors_converged_) {
tomwalters@8 271 // LOG_INFO("Converged!");
tomwalters@0 272 break;
tomwalters@0 273 }
tomwalters@8 274 // LOG_INFO("Didn't converge!");
tomwalters@2 275
tomwalters@0 276
tomwalters@0 277 // update means (positions)
tomwalters@0 278 for (int i = 0 ; i < iNComponents; ++i) {
tomwalters@8 279 float mu_old = m_pMu[i];
tomwalters@0 280 if (m_pA[i] > 0.0f) {
tomwalters@0 281 m_pMu[i] = 0.0f;
tomwalters@0 282 for (int iCount = 0; iCount < iSizeX; ++iCount) {
tomwalters@0 283 m_pMu[i] += m_pSpectralProfile[iCount]
tomwalters@8 284 * pP_comp[iCount + i * iSizeX]
tomwalters@8 285 * static_cast<float>(iCount + 1);
tomwalters@0 286 }
tomwalters@0 287 m_pMu[i] /= m_pA[i];
tomwalters@0 288 if (isnan(m_pMu[i])) {
tomwalters@0 289 m_pMu[i] = mu_old;
tomwalters@0 290 }
tomwalters@0 291 }
tomwalters@0 292 }
tomwalters@8 293 } // loop over iterations
tomwalters@0 294
tomwalters@0 295 // Ensure they are sorted, using a really simple bubblesort
tomwalters@0 296 bool bSorted = false;
tomwalters@0 297 while (!bSorted) {
tomwalters@0 298 bSorted = true;
tomwalters@0 299 for (int i = 0; i < iNComponents - 1; ++i) {
tomwalters@0 300 if (m_pMu[i] > m_pMu[i + 1]) {
tomwalters@8 301 float fTemp = m_pMu[i];
tomwalters@0 302 m_pMu[i] = m_pMu[i + 1];
tomwalters@0 303 m_pMu[i + 1] = fTemp;
tomwalters@0 304 fTemp = m_pA[i];
tomwalters@0 305 m_pA[i] = m_pA[i + 1];
tomwalters@0 306 m_pA[i + 1] = fTemp;
tomwalters@0 307 bSorted = false;
tomwalters@0 308 }
tomwalters@0 309 }
tomwalters@0 310 }
tomwalters@0 311 return true;
tomwalters@0 312 }
tomwalters@8 313 } // namespace aimc
tomwalters@0 314