comparison src/Modules/Features/ModuleGaussians.cc @ 1:bc394a985042

- Fixed the python SWIG wrappers - Added stub test for the Gaussian features, and test data - Fixed build errors
author tomwalters
date Mon, 15 Feb 2010 20:37:26 +0000
parents 582cbe817f2c
children e91769e84be1
comparison
equal deleted inserted replaced
0:582cbe817f2c 1:bc394a985042
19 /*! \file 19 /*! \file
20 * \brief Gaussian features - based on MATLAB code by Christian Feldbauer 20 * \brief Gaussian features - based on MATLAB code by Christian Feldbauer
21 */ 21 */
22 22
23 /*! 23 /*!
24 * \author Tom Walters <tcw24@cam.ac.uk> 24 * \author Thomas Walters <tom@acousticscale.org>
25 * \date created 2008/06/23 25 * \date created 2008/06/23
26 * \version \$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $ 26 * \version \$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $
27 */ 27 */
28 28
29 #include <math.h> 29 #include <math.h>
34 namespace aimc { 34 namespace aimc {
35 ModuleGaussians::ModuleGaussians(Parameters *pParam) 35 ModuleGaussians::ModuleGaussians(Parameters *pParam)
36 : Module(pParam) { 36 : Module(pParam) {
37 // Set module metadata 37 // Set module metadata
38 module_description_ = "Gaussian Fitting to SSI profile"; 38 module_description_ = "Gaussian Fitting to SSI profile";
39 module_identifier_ = "gaussians"; // unique identifier for the module 39 module_identifier_ = "gaussians";
40 module_type_ = "features"; 40 module_type_ = "features";
41 module_version_ = "$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $"; 41 module_version_ = "$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $";
42 42
43 parameters_->SetDefault("features.gaussians.ncomp", "4"); 43 m_iParamNComp = parameters_->DefaultInt("features.gaussians.ncomp", 4);
44 m_iParamNComp = parameters_->GetInt("features.gaussians.ncomp"); 44 m_fParamVar = parameters_->DefaultFloat("features.gaussians.var", 115.0);
45
46 parameters_->SetDefault("features.gaussians.var", "115.0");
47 m_fParamVar = parameters_->GetFloat("features.gaussians.var");
48
49 parameters_->SetDefault("features.gaussians.posterior_exp", "6.0");
50 m_fParamPosteriorExp = 45 m_fParamPosteriorExp =
51 parameters_->GetFloat("features.gaussians.posterior_exp"); 46 parameters_->DefaultFloat("features.gaussians.posterior_exp", 6.0);
52 47 m_iParamMaxIt = parameters_->DefaultInt("features.gaussians.maxit", 250);
53 parameters_->SetDefault("features.gaussians.maxit", "250"); 48
54 m_iParamMaxIt = parameters_->GetInt("features.gaussians.maxit"); 49 // The parameters system doesn't support tiny numbers well, to define this
55 50 // variable as a string, then convert it to a float afterwards
56 parameters_->SetDefault("features.gaussians.priors_converged", "1e-7"); 51 parameters_->DefaultString("features.gaussians.priors_converged", "1e-7");
57 m_fParamPriorsConverged = 52 m_fParamPriorsConverged =
58 parameters_->GetInt("features.gaussians.priors_converged"); 53 parameters_->GetFloat("features.gaussians.priors_converged");
59 } 54 }
60 55
61 ModuleGaussians::~ModuleGaussians() { 56 ModuleGaussians::~ModuleGaussians() {
62 } 57 }
63 58
66 m_pMu.resize(m_iParamNComp, 0.0f); 61 m_pMu.resize(m_iParamNComp, 0.0f);
67 62
68 // Assuming the number of channels is greater than twice the number of 63 // Assuming the number of channels is greater than twice the number of
69 // Gaussian components, this is ok 64 // Gaussian components, this is ok
70 if (input.channel_count() >= 2 * m_iParamNComp) { 65 if (input.channel_count() >= 2 * m_iParamNComp) {
71 output_.Initialize(1, m_iParamNComp, input.sample_rate()); 66 output_.Initialize(m_iParamNComp, 1, input.sample_rate());
72 } else { 67 } else {
73 LOG_ERROR(_T("Too few channels in filterbank to produce sensible " 68 LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
74 "Gaussian features. Either increase the number of filterbank" 69 "Gaussian features. Either increase the number of filterbank"
75 " channels, or decrease the number of Gaussian components")); 70 " channels, or decrease the number of Gaussian components"));
76 return false; 71 return false;
86 m_pSpectralProfile.clear(); 81 m_pSpectralProfile.clear();
87 m_pSpectralProfile.resize(m_iNumChannels, 0.0f); 82 m_pSpectralProfile.resize(m_iNumChannels, 0.0f);
88 } 83 }
89 84
90 void ModuleGaussians::Process(const SignalBank &input) { 85 void ModuleGaussians::Process(const SignalBank &input) {
91 int iAudCh = 0; 86 if (!initialized_) {
92 87 LOG_ERROR(_T("Module ModuleGaussians not initialized."));
88 return;
89 }
93 // Calculate spectral profile 90 // Calculate spectral profile
94 for (int iChannel = 0; 91 for (int iChannel = 0;
95 iChannel < input.channel_count(); 92 iChannel < input.channel_count();
96 ++iChannel) { 93 ++iChannel) {
97 m_pSpectralProfile[iChannel] = 0.0f; 94 m_pSpectralProfile[iChannel] = 0.0f;
98 for (int iSample = 0; 95 for (int iSample = 0;
99 iSample < input.buffer_length(); 96 iSample < input.buffer_length();
100 ++iSample) { 97 ++iSample) {
101 m_pSpectralProfile[iChannel] += input[iChannel][iSample]; 98 m_pSpectralProfile[iChannel] += input[iChannel][iSample];
102 } 99 }
100 m_pSpectralProfile[iChannel] /= static_cast<float>(input.buffer_length());
101 }
102
103 float spectral_profile_sum = 0.0f;
104 for (int i = 0; i < input.channel_count(); ++i) {
105 spectral_profile_sum += m_pSpectralProfile[i];
106 }
107
108 double logsum = log(spectral_profile_sum);
109 if (!isinf(logsum)) {
110 output_.set_sample(m_iParamNComp - 1, 0, logsum);
111 } else {
112 output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
103 } 113 }
104 114
105 for (int iChannel = 0; 115 for (int iChannel = 0;
106 iChannel < input.channel_count(); 116 iChannel < input.channel_count();
107 ++iChannel) { 117 ++iChannel) {
108 m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8); 118 m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8);
109 }
110
111 float spectral_profile_sum = 0.0f;
112 for (int i = 0; i < input.channel_count(); ++i) {
113 spectral_profile_sum += m_pSpectralProfile[i];
114 } 119 }
115 120
116 RubberGMMCore(2, true); 121 RubberGMMCore(2, true);
117 122
118 float fMean1 = m_pMu[0]; 123 float fMean1 = m_pMu[0];
140 output_.set_sample(i, 0, m_pA[i]); 145 output_.set_sample(i, 0, m_pA[i]);
141 } else { 146 } else {
142 output_.set_sample(i, 0, 0.0f); 147 output_.set_sample(i, 0, 0.0f);
143 } 148 }
144 } 149 }
145 /*for (int i = m_iParamNComp; i < m_iParamNComp * 2; ++i) { 150
146 m_pOutputData->getSignal(i)->setSample(iAudCh, 0, m_pMu[i-m_iParamNComp]);
147 }*/
148 double logsum = log(spectral_profile_sum);
149 if (!isinf(logsum)) {
150 output_.set_sample(m_iParamNComp - 1, 0, logsum);
151 } else {
152 output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
153 }
154 PushOutput(); 151 PushOutput();
155 } 152 }
156 153
157 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) { 154 bool ModuleGaussians::RubberGMMCore(int iNComponents, bool bDoInit) {
158 int iSizeX = m_iNumChannels; 155 int iSizeX = m_iNumChannels;