# HG changeset patch # User tomwalters # Date 1281612491 0 # Node ID 0db637cac96b6f5bededdb481a2404b90f9e4a79 # Parent 578ec2d32170a7353870922ce97de712f3f815c0 - Added as-yet-unfinished support for a proper configuraiton file format - Added a couple of pythin scripts to generate HMM configuration files - Variable name changes and other cosmetic things - Added the option for the noise generation to do pink noise (untested) diff -r 578ec2d32170 -r 0db637cac96b experiments/scripts/cnbh-syllables/feature_generation/nap_profile_features.aimcopycfg --- a/experiments/scripts/cnbh-syllables/feature_generation/nap_profile_features.aimcopycfg Thu Aug 12 09:15:49 2010 +0000 +++ b/experiments/scripts/cnbh-syllables/feature_generation/nap_profile_features.aimcopycfg Thu Aug 12 11:28:11 2010 +0000 @@ -7,10 +7,11 @@ module2.name = GammatoneFilterbank module2.id = gt -module2.child1 = SmoothNAP +module2.child1 = StandardNAP +module2.child2 = SmoothNAP module3.name = SmoothNAP -module3.id = hcl +module3.id = nap module3.parameters = <<DefaultInt("features.gaussians.ncomp", 4); - m_fParamVar = parameters_->DefaultFloat("features.gaussians.var", 115.0); - m_fParamPosteriorExp = - parameters_->DefaultFloat("features.gaussians.posterior_exp", 6.0); - m_iParamMaxIt = parameters_->DefaultInt("features.gaussians.maxit", 250); + m_iParamNComp = parameters_->DefaultInt("gaussians.ncomp", 4); + m_fParamVar = parameters_->DefaultFloat("gaussians.var", 115.0); + m_fParamPosteriorExp = parameters_->DefaultFloat("gaussians.posterior_exp", + 6.0); + m_iParamMaxIt = parameters_->DefaultInt("gaussians.maxit", 250); // The parameters system doesn't support tiny numbers well, to define this // variable as a string, then convert it to a float afterwards - parameters_->DefaultString("features.gaussians.priors_converged", "1e-7"); - m_fParamPriorsConverged = - parameters_->GetFloat("features.gaussians.priors_converged"); + parameters_->DefaultString("gaussians.priors_converged", "1e-7"); + priors_converged_ = parameters_->GetFloat("gaussians.priors_converged"); + output_positions_ = parameters_->DefaultBool("gaussians.positions", false); } ModuleGaussians::~ModuleGaussians() { @@ -60,8 +60,9 @@ // Assuming the number of channels is greater than twice the number of // Gaussian components, this is ok + output_component_count_ = 1; // Energy component if (input.channel_count() >= 2 * m_iParamNComp) { - output_.Initialize(m_iParamNComp, 1, input.sample_rate()); + output_component_count_ += (m_iParamNComp - 1); } else { LOG_ERROR(_T("Too few channels in filterbank to produce sensible " "Gaussian features. Either increase the number of filterbank" @@ -69,6 +70,12 @@ return false; } + if (output_positions_) { + output_component_count_ += m_iParamNComp; + } + + output_.Initialize(output_component_count_, 1, input.sample_rate()); + m_iNumChannels = input.channel_count(); m_pSpectralProfile.resize(m_iNumChannels, 0.0f); @@ -90,16 +97,12 @@ return; } // Calculate spectral profile - for (int iChannel = 0; - iChannel < input.channel_count(); - ++iChannel) { - m_pSpectralProfile[iChannel] = 0.0f; - for (int iSample = 0; - iSample < input.buffer_length(); - ++iSample) { - m_pSpectralProfile[iChannel] += input[iChannel][iSample]; + for (int ch = 0; ch < input.channel_count(); ++ch) { + m_pSpectralProfile[ch] = 0.0f; + for (int i = 0; i < input.buffer_length(); ++i) { + m_pSpectralProfile[ch] += input[ch][i]; } - m_pSpectralProfile[iChannel] /= static_cast(input.buffer_length()); + m_pSpectralProfile[ch] /= static_cast(input.buffer_length()); } float spectral_profile_sum = 0.0f; @@ -107,36 +110,35 @@ spectral_profile_sum += m_pSpectralProfile[i]; } + // Set the last component of the feature vector to be the log energy float logsum = log(spectral_profile_sum); if (!isinf(logsum)) { - output_.set_sample(m_iParamNComp - 1, 0, logsum); + output_.set_sample(output_component_count_ - 1, 0, logsum); } else { - output_.set_sample(m_iParamNComp - 1, 0, -1000.0); + output_.set_sample(output_component_count_ - 1, 0, -1000.0); } - for (int iChannel = 0; - iChannel < input.channel_count(); - ++iChannel) { - m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8f); + for (int ch = 0; ch < input.channel_count(); ++ch) { + m_pSpectralProfile[ch] = pow(m_pSpectralProfile[ch], 0.8); } RubberGMMCore(2, true); - float fMean1 = m_pMu[0]; - float fMean2 = m_pMu[1]; + float mean1 = m_pMu[0]; + float mean2 = m_pMu[1]; // LOG_INFO(_T("Orig. mean 0 = %f"), m_pMu[0]); // LOG_INFO(_T("Orig. mean 1 = %f"), m_pMu[1]); // LOG_INFO(_T("Orig. prob 0 = %f"), m_pA[0]); // LOG_INFO(_T("Orig. prob 1 = %f"), m_pA[1]); - float fA1 = 0.05 * m_pA[0]; - float fA2 = 1.0 - 0.25 * m_pA[1]; + float a1 = 0.05 * m_pA[0]; + float a2 = 1.0 - 0.25 * m_pA[1]; // LOG_INFO(_T("fA1 = %f"), fA1); // LOG_INFO(_T("fA2 = %f"), fA2); - float fGradient = (fMean2 - fMean1) / (fA2 - fA1); - float fIntercept = fMean2 - fGradient * fA2; + float gradient = (mean2 - mean1) / (a2 - a1); + float intercept = mean2 - gradient * a2; // LOG_INFO(_T("fGradient = %f"), fGradient); // LOG_INFO(_T("fIntercept = %f"), fIntercept); @@ -144,7 +146,7 @@ for (int i = 0; i < m_iParamNComp; ++i) { m_pMu[i] = (static_cast(i) / (static_cast(m_iParamNComp) - 1.0f)) - * fGradient + fIntercept; + * gradient + intercept; // LOG_INFO(_T("mean %d = %f"), i, m_pMu[i]); } @@ -154,6 +156,7 @@ RubberGMMCore(m_iParamNComp, false); + // Amplitudes first for (int i = 0; i < m_iParamNComp - 1; ++i) { if (!isnan(m_pA[i])) { output_.set_sample(i, 0, m_pA[i]); @@ -162,6 +165,19 @@ } } + // Then means if required + if (output_positions_) { + int idx = 0; + for (int i = m_iParamNComp - 1; i < 2 * m_iParamNComp - 1; ++i) { + if (!isnan(m_pMu[i])) { + output_.set_sample(i, 0, m_pMu[idx]); + } else { + output_.set_sample(i, 0, 0.0f); + } + ++idx; + } + } + PushOutput(); } @@ -169,12 +185,12 @@ int iSizeX = m_iNumChannels; // Normalise the spectral profile - float fSpectralProfileTotal = 0.0f; + float SpectralProfileTotal = 0.0f; for (int iCount = 0; iCount < iSizeX; iCount++) { - fSpectralProfileTotal += m_pSpectralProfile[iCount]; + SpectralProfileTotal += m_pSpectralProfile[iCount]; } for (int iCount = 0; iCount < iSizeX; iCount++) { - m_pSpectralProfile[iCount] /= fSpectralProfileTotal; + m_pSpectralProfile[iCount] /= SpectralProfileTotal; } if (bDoInit) { @@ -200,12 +216,12 @@ pP_mod_X[i] = 0.0f; } - for (int i = 0; i < iNComponents; i++) { + for (int c = 0; c < iNComponents; c++) { for (int iCount = 0; iCount < iSizeX; iCount++) { pP_mod_X[iCount] += 1.0f / sqrt(2.0f * M_PI * m_fParamVar) * exp((-0.5f) - * pow(static_cast(iCount+1) - m_pMu[i], 2) - / m_fParamVar) * m_pA[i]; + * pow(static_cast(iCount+1) - m_pMu[c], 2) + / m_fParamVar) * m_pA[c]; } } @@ -251,7 +267,7 @@ } fPrdist /= iNComponents; - if (fPrdist < m_fParamPriorsConverged) { + if (fPrdist < priors_converged_) { // LOG_INFO("Converged!"); break; } diff -r 578ec2d32170 -r 0db637cac96b src/Support/ModuleTree.cc --- a/src/Support/ModuleTree.cc Thu Aug 12 09:15:49 2010 +0000 +++ b/src/Support/ModuleTree.cc Thu Aug 12 11:28:11 2010 +0000 @@ -24,152 +24,7 @@ * \version \$Id: $ */ -#include "Support/ModuleFactory.h" -#include "Support/Module.h" -#include "Support/ModuleTree.h" - namespace aimc { -using std::endl; -ModuleTree::ModuleTree() : root_module_(NULL), - initialized_(false) { + ParseConfigFile(string ) -} - -bool ModuleTree::LoadConfigFile(const string &filename) { - config_.Load(filename.c_str()); - return ConstructTree(); -} - -bool ModuleTree::LoadConfigText(const string &config) { - config_.Parse(config.c_str()); - return ConstructTree(); -} - -bool ModuleTree::ConstructTree() { - // Make two passes over the configuration file. - // The first pass creates all the named modules with their parameters. - bool done = false; - bool error = false; - int module_number = 1; - LOG_INFO("Parsing tree..."); - while (!done) { - char module_name_var[Parameters::MaxParamNameLength]; - char module_id_var[Parameters::MaxParamNameLength]; - char module_parameters_var[Parameters::MaxParamNameLength]; - sprintf(module_name_var, "module%d.name", module_number); - sprintf(module_id_var, "module%d.id", module_number); - sprintf(module_parameters_var, "module%d.parameters", module_number); - if (config_.IsSet(module_name_var)) { - string module_name(config_.GetString(module_name_var)); - LOG_INFO("Module number %d, name %s", module_number, module_name.c_str()); - if (config_.IsSet(module_id_var)) { - const char* id = config_.GetString(module_id_var); - const char* parameters = config_.DefaultString(module_parameters_var, ""); - linked_ptr params(new Parameters); - parameters_[module_name] = params; - parameters_[module_name]->Parse(parameters); - linked_ptr module(ModuleFactory::Create(id, parameters_[module_name].get())); - if (module.get() != NULL) { - module->set_instance_name(module_name); - modules_[module_name] = module; - } else { - LOG_ERROR("Module name: %s of type %s not created", module_name.c_str(), id); - done = true; - error = true; - } - if (module_number == 1) { - root_module_ = module.get(); - } - } else { - LOG_ERROR("id field missing for module named %s", module_name.c_str()); - error = true; - done = true; - } - } else { - done = true; - } - ++module_number; - } - // The second pass connects up all the modules into a tree. - char module_child_var[Parameters::MaxParamNameLength]; - char module_name_var[Parameters::MaxParamNameLength]; - LOG_INFO("A total of %d modules", modules_.size()); - for (unsigned int i = 1; i < modules_.size() + 1; ++i) { - int child_number = 1; - done = false; - sprintf(module_name_var, "module%d.name", i); - if (config_.IsSet(module_name_var)) { - string module_name(config_.GetString(module_name_var)); - while (!done) { - sprintf(module_child_var, "module%d.child%d", i, child_number); - LOG_INFO("Trying %s", module_child_var); - if (config_.IsSet(module_child_var)) { - string child(config_.GetString(module_child_var)); - if ((modules_.find(module_name) != modules_.end()) - && (modules_.find(child) != modules_.end())) { - modules_[module_name]->AddTarget(modules_[child].get()); - } else { - LOG_ERROR("Module name not found"); - } - } else { - done = true; - } - ++child_number; - } - } else { - LOG_ERROR("field missing for entry %s", module_name_var); - error = true; - break; - } - } - return !error; -} - -bool ModuleTree::Initialize(Parameters *global_parameters) { - if (root_module_ == NULL) { - return false; - } - // Dummy signal bank for the root module. - s_.Initialize(1, 1, 1); - initialized_ = root_module_->Initialize(s_, global_parameters); - return initialized_; -} - -void ModuleTree::Reset() { - if (root_module_ == NULL) { - return; - } - root_module_->Reset(); -} - -void ModuleTree::PrintConfiguration(ostream &out) { - if (root_module_ == NULL) { - return; - } - root_module_->PrintTargets(out); - root_module_->PrintConfiguration(out); -} - -void ModuleTree::Process() { - if (root_module_ == NULL) { - return; - } - if (!initialized_) { - LOG_ERROR(_T("Module tree not initialized.")); - return; - } - while (!root_module_->done()) { - root_module_->Process(s_); - } -} - -void ModuleTree::MakeDotGraph(ostream &out) { - if (root_module_ == NULL) { - return; - } - out << "digraph G {" << endl; - root_module_->PrintTargetsForDot(out); - out << "}" << endl; -} - -} // namespace aimc +} // namespace aimc \ No newline at end of file diff -r 578ec2d32170 -r 0db637cac96b src/Support/ModuleTree.h --- a/src/Support/ModuleTree.h Thu Aug 12 09:15:49 2010 +0000 +++ b/src/Support/ModuleTree.h Thu Aug 12 11:28:11 2010 +0000 @@ -24,47 +24,20 @@ * \version \$Id: $ */ -#include -#include #include -#include "Support/Common.h" -#include "Support/Module.h" -#include "Support/Parameters.h" -#include "Support/SignalBank.h" -#include "Support/linked_ptr.h" - namespace aimc { using std::string; -using std::map; -using std::ostream; - class ModuleTree { public: - ModuleTree(); - bool LoadConfigFile(const string &filename); - bool LoadConfigText(const string &config_text); - string GetFullConfig(); - bool Initialize(Parameters *global_parameters); - void Reset(); - void PrintConfiguration(ostream &out); - void Process(); - void MakeDotGraph(ostream &out); - void set_output_filename_prefix(const string &prefix) { - output_filename_prefix_ = prefix; - }; + bool ParseConfigFile(const string &filename); + bool ParseConfigText(const string &config_text); + void set_output_filename_prefix(const string &prefix); string output_filename_prefix() { return output_filename_prefix_; - }; + } private: - bool ConstructTree(); - Parameters config_; - SignalBank s_; string output_filename_prefix_; - map > modules_; - Module *root_module_; - map > parameters_; - bool initialized_; DISALLOW_COPY_AND_ASSIGN(ModuleTree); }; } // namespace aimc \ No newline at end of file