changeset 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
files SConstruct src/Main/aimc.cc src/Modules/Features/ModuleGaussians.cc src/Modules/Features/ModuleGaussians.h src/Modules/Features/ModuleGaussians_test.py src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat src/Modules/NAP/ModuleHCL.cc src/Support/Module.cc src/Support/Parameters.cc src/Support/Parameters.h src/Support/SignalBank.cc src/Support/SignalBank.h swig/aim_modules.i swig/example.py swig/setup.py
diffstat 15 files changed, 163 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/SConstruct	Fri Feb 12 12:31:23 2010 +0000
+++ b/SConstruct	Mon Feb 15 20:37:26 2010 +0000
@@ -93,11 +93,12 @@
 
 # Sources common to every version
 common_sources = ['Support/Common.cc',
+                  'Support/SignalBank.cc',
                   'Support/Parameters.cc',
                   'Support/Module.cc', 
                   'Modules/BMM/ModulePZFC.cc',
                   'Modules/NAP/ModuleHCL.cc',
-                  'Modules/SAI/ModuleSAI.cc',
+                  #'Modules/SAI/ModuleSAI.cc',
                   'Modules/Features/ModuleGaussians.cc']
     
 if not target_platform == 'win32':
@@ -106,7 +107,7 @@
   common_sources += ['Support/ConvertUTF.c']
 
 # Choose file which contains main()
-sources = common_sources + ['Main/aimc.cpp']
+sources = common_sources + ['Main/aimc.cc']
 
 # Place the build products in the corect place
 env.BuildDir('#' + build_dir, '#', duplicate = 0)
--- a/src/Main/aimc.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Main/aimc.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -16,13 +16,6 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <vector>
-#include <boost/scoped_ptr.hpp>
-
-using std::vector;
-using boost::scoped_ptr;
-
 int main () {
-  scoped_ptr<vector<float> > fred;
-  fred.reset(new vector<float>);
+ // TODO
 }
\ No newline at end of file
--- a/src/Modules/Features/ModuleGaussians.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Modules/Features/ModuleGaussians.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -21,7 +21,7 @@
  */
 
 /*!
- * \author Tom Walters <tcw24@cam.ac.uk>
+ * \author Thomas Walters <tom@acousticscale.org>
  * \date created 2008/06/23
  * \version \$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $
  */
@@ -36,26 +36,21 @@
 : Module(pParam) {
   // Set module metadata
   module_description_ = "Gaussian Fitting to SSI profile";
-  module_identifier_ = "gaussians"; // unique identifier for the module
+  module_identifier_ = "gaussians";
   module_type_ = "features";
   module_version_ = "$Id: ModuleGaussians.cc 2 2010-02-02 12:59:50Z tcw $";
 
-  parameters_->SetDefault("features.gaussians.ncomp", "4");
-  m_iParamNComp = parameters_->GetInt("features.gaussians.ncomp");
+  m_iParamNComp = 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);
 
-  parameters_->SetDefault("features.gaussians.var", "115.0");
-  m_fParamVar = parameters_->GetFloat("features.gaussians.var");
-
-  parameters_->SetDefault("features.gaussians.posterior_exp", "6.0");
-  m_fParamPosteriorExp =
-    parameters_->GetFloat("features.gaussians.posterior_exp");
-
-  parameters_->SetDefault("features.gaussians.maxit", "250");
-  m_iParamMaxIt = parameters_->GetInt("features.gaussians.maxit");
-
-  parameters_->SetDefault("features.gaussians.priors_converged", "1e-7");
+  // 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_->GetInt("features.gaussians.priors_converged");
+    parameters_->GetFloat("features.gaussians.priors_converged");
 }
 
 ModuleGaussians::~ModuleGaussians() {
@@ -68,7 +63,7 @@
   // Assuming the number of channels is greater than twice the number of
   // Gaussian components, this is ok
   if (input.channel_count() >= 2 * m_iParamNComp) {
-    output_.Initialize(1, m_iParamNComp, input.sample_rate());
+    output_.Initialize(m_iParamNComp, 1, input.sample_rate());
   } else {
     LOG_ERROR(_T("Too few channels in filterbank to produce sensible "
                  "Gaussian features. Either increase the number of filterbank"
@@ -88,8 +83,10 @@
 }
 
 void ModuleGaussians::Process(const SignalBank &input) {
-  int iAudCh = 0;
-
+  if (!initialized_) {
+    LOG_ERROR(_T("Module ModuleGaussians not initialized."));
+    return;
+  }
   // Calculate spectral profile
   for (int iChannel = 0;
        iChannel < input.channel_count();
@@ -100,6 +97,19 @@
          ++iSample) {
       m_pSpectralProfile[iChannel] += input[iChannel][iSample];
     }
+    m_pSpectralProfile[iChannel] /= static_cast<float>(input.buffer_length());
+  }
+
+  float spectral_profile_sum = 0.0f;
+  for (int i = 0; i < input.channel_count(); ++i) {
+    spectral_profile_sum += m_pSpectralProfile[i];
+  }
+
+  double logsum = log(spectral_profile_sum);
+  if (!isinf(logsum)) {
+    output_.set_sample(m_iParamNComp - 1, 0, logsum);
+  } else {
+    output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
   }
 
   for (int iChannel = 0;
@@ -108,11 +118,6 @@
     m_pSpectralProfile[iChannel] = pow(m_pSpectralProfile[iChannel], 0.8);
   }
 
-  float spectral_profile_sum = 0.0f;
-  for (int i = 0; i < input.channel_count(); ++i) {
-    spectral_profile_sum += m_pSpectralProfile[i];
-  }
-
   RubberGMMCore(2, true);
 
   float fMean1 = m_pMu[0];
@@ -142,15 +147,7 @@
       output_.set_sample(i, 0, 0.0f);
     }
   }
-  /*for (int i = m_iParamNComp; i < m_iParamNComp * 2; ++i) {
-    m_pOutputData->getSignal(i)->setSample(iAudCh, 0, m_pMu[i-m_iParamNComp]);
-  }*/
-  double logsum = log(spectral_profile_sum);
-  if (!isinf(logsum)) {
-    output_.set_sample(m_iParamNComp - 1, 0, logsum);
-  } else {
-    output_.set_sample(m_iParamNComp - 1, 0, -1000.0);
-  }
+
   PushOutput();
 }
 
--- a/src/Modules/Features/ModuleGaussians.h	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Modules/Features/ModuleGaussians.h	Mon Feb 15 20:37:26 2010 +0000
@@ -74,7 +74,7 @@
 
   /*! \brief Maximum Number of iterations
    */
-  unsigned int m_iParamMaxIt;
+  int m_iParamMaxIt;
 
   /*! \brief convergence criterion
    */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Modules/Features/ModuleGaussians_test.py	Mon Feb 15 20:37:26 2010 +0000
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+# encoding: utf-8
+#
+# AIM-C: A C++ implementation of the Auditory Image Model
+# http://www.acousticscale.org/AIMC
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+ModuleGaussians_test.py
+
+Created by Thomas Walters on 2010-02-15.
+Copyright 2010 Thomas Walters <tom@acousticscale.org>
+Test for the Gaussians module. Runs a number of pre-computed SAI profiles
+through the module, and tests them against the equivalent output from the
+MATLAB rubber_GMM code.
+"""
+
+import aimc
+import matplotlib
+import pylab
+import scipy
+
+def main():
+  data_file = "src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat"
+  data = scipy.io.loadmat(data_file)
+  
+  given_profiles = data["Templates"]
+  matlab_features = data["feature"]
+  
+  
+  
+  pass
+
+
+if __name__ == '__main__':
+  main()
Binary file src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat has changed
--- a/src/Modules/NAP/ModuleHCL.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Modules/NAP/ModuleHCL.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -37,15 +37,10 @@
                         "and lowpass filtering";
   module_version_ = "$Id: ModuleHCL.cc 4 2010-02-03 18:44:58Z tcw $";
 
-  parameters_->SetDefault("nap.do_lowpass", "false");
-  parameters_->SetDefault("nap.do_log_compression", "false");
-  parameters_->SetDefault("nap.lowpass_cutoff", "1200.0");
-  parameters_->SetDefault("nap.lowpass_order", "2");
-
-  do_lowpass_ = parameters_->GetBool("nap.do_lowpass");
-  do_log_ = parameters_->GetBool("nap.do_log_compression");
-  lowpass_cutoff_ = parameters_->GetFloat("nap.lowpass_cutoff");
-  lowpass_order_ = parameters_->GetInt("nap.lowpass_order");
+  do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", false);
+  do_log_ = parameters_->DefaultBool("nap.do_log_compression", false);
+  lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0);
+  lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2);
 }
 
 ModuleHCL::~ModuleHCL() {
--- a/src/Support/Module.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Support/Module.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -50,7 +50,7 @@
     LOG_ERROR("Input SignalBank not valid");
     return false;
   }
-    if (!InitializeInternal(input)) {
+  if (!InitializeInternal(input)) {
     LOG_ERROR("Module initialization failed");
     return false;
   }
--- a/src/Support/Parameters.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Support/Parameters.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -47,41 +47,41 @@
 const char * Parameters::DefaultString(const char* sName, const char* val) {
   AIM_ASSERT(m_pIni);
   if (!IsSet(sName)) {
-    m_pIni->SetValue(m_SDefaultIniSection, sName, val);
+    SetString(sName, val);
   }
-  return m_pIni->GetString(sName);
+  return GetString(sName);
 }
 
 int Parameters::DefaultInt(const char* sName, int val) {
   AIM_ASSERT(m_pIni);
   if (!IsSet(sName)) {
-    m_pIni->SetInt(m_SDefaultIniSection, sName, val);
+    SetInt(sName, val);
   }
-  return m_pIni->GetInt(sName);
+  return GetInt(sName);
 }
 
 unsigned int Parameters::DefaultUInt(const char* sName, unsigned int val) {
   AIM_ASSERT(m_pIni);
   if (!IsSet(sName)) {
-    m_pIni->SetUInt(m_SDefaultIniSection, sName, val);
+    SetUInt(sName, val);
   }
-  return m_pIni->GetUInt(sName);
+  return GetUInt(sName);
 }
 
 float Parameters::DefaultFloat(const char* sName, float val) {
   AIM_ASSERT(m_pIni);
   if (!IsSet(sName)) {
-    m_pIni->SetFloat(m_SDefaultIniSection, sName, val);
+    SetFloat(sName, val);
   }
-  return m_pIni->GetFloat(sName);
+  return GetFloat(sName);
 }
 
 bool Parameters::DefaultBool(const char* sName, bool val) {
   AIM_ASSERT(m_pIni);
   if (!IsSet(sName)) {
-    m_pIni->SetBool(m_SDefaultIniSection, sName, val);
+    SetBool(sName, val);
   }
-  return m_pIni->GetBool(sName);
+  return GetBool(sName);
 }
 
 void Parameters::SetString(const char *sName, const char *val) {
@@ -264,4 +264,17 @@
 
   return true;
 }
+
+std::string Parameters::WriteString() {
+  AIM_ASSERT(m_pIni);
+  SI_Error siErr;
+  std::string output_string;
+
+  if ((siErr = m_pIni->Save(output_string)) < 0 ) {
+    LOG_ERROR(_T("Couldn't write parameters to string"));
+    return false;
+  }
+  return output_string;
+}
+
 }  // namespace aimc
--- a/src/Support/Parameters.h	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Support/Parameters.h	Mon Feb 15 20:37:26 2010 +0000
@@ -27,6 +27,8 @@
 #ifndef _AIMC_SUPPORT_PARAMETERS_H_
 #define _AIMC_SUPPORT_PARAMETERS_H_
 
+#include <string>
+
 // If not _WINDOWS, please compile in Support/ConvertUTF.c
 #ifdef _UNICODE
 // Here we want to use the ANSI version of all the non wxWidgets stuff, but
@@ -170,6 +172,12 @@
    */
   bool Delete(const char *sName);
 
+  /*! \brief Append parameters to a string
+   * \param sName pointer to a string
+   * \return true on success
+   */
+   std::string WriteString();
+
   //! \brief Maximum length of a parameter name in characters
   static const unsigned int MaxParamNameLength = 128;
 
--- a/src/Support/SignalBank.cc	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Support/SignalBank.cc	Mon Feb 15 20:37:26 2010 +0000
@@ -114,15 +114,11 @@
   return true;
 }
 
-inline const vector<float> &SignalBank::operator[](int channel) const {
-  return signals_[channel];
-}
-
-inline float SignalBank::sample(int channel, int index) const {
+float SignalBank::sample(int channel, int index) const {
   return signals_[channel][index];
 }
 
-inline void SignalBank::set_sample(int channel, int index, float value) {
+void SignalBank::set_sample(int channel, int index, float value) {
   signals_[channel][index] = value;
 }
 
--- a/src/Support/SignalBank.h	Fri Feb 12 12:31:23 2010 +0000
+++ b/src/Support/SignalBank.h	Mon Feb 15 20:37:26 2010 +0000
@@ -54,9 +54,13 @@
    */
   bool Initialize(const SignalBank &input);
   bool Validate() const;
-  inline const vector<float> &operator[](int channel) const;
-  inline float sample(int channel, int index) const;
-  inline void set_sample(int channel, int index, float value);
+
+  inline const vector<float> &operator[](int channel) const {
+    return signals_[channel];
+  };
+
+  float sample(int channel, int index) const;
+  void set_sample(int channel, int index, float value);
   const deque<int> &strobes(int channel) const;
   float sample_rate() const;
   int buffer_length() const;
--- a/swig/aim_modules.i	Fri Feb 12 12:31:23 2010 +0000
+++ b/swig/aim_modules.i	Mon Feb 15 20:37:26 2010 +0000
@@ -16,7 +16,8 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-%module aimc 
+%module aimc
+%include "std_string.i"
 %{
 #include "Support/Common.h"
 #include "Support/Module.h"
@@ -32,8 +33,13 @@
 	~Parameters();
 	bool Load(const char *sParamFilename);
 	bool Save(const char *sParamFilename);
+  std::string WriteString();
 	bool Merge(const char *sParamFilename);
-  void SetDefault(const char* sName, const char* value);
+	const char* DefaultString(const char *sName, const char *val);
+	int DefaultInt(const char *sName, int val);
+	unsigned int DefaultUInt(const char *sName, unsigned int val);
+	float DefaultFloat(const char *sName, float val);
+	bool DefaultBool(const char *sName, bool val);
 	void SetString(const char *sName, const char *val);
 	void SetInt(const char *sName, int val);
 	void SetUInt(const char *sName, unsigned int val);
@@ -52,21 +58,22 @@
 
 class SignalBank {
  public:
-  SignalBank();
-  bool Initialize(int channel_count, int signal_length, float sample_rate);
-  bool Validate() const;
-  inline const vector<float> &operator[](int channel) const;
-  void set_sample(int channel, int sample, float value);
-  float sample_rate() const;
-  int buffer_length() const;
-  int start_time() const;
-  void set_start_time(int start_time);
-  float get_centre_frequency(int i) const;
-  void set_centre_frequency(int i, float cf);
-  bool initialized() const;
-  int channel_count() const;
- private:
-  DISALLOW_COPY_AND_ASSIGN(SignalBank);
+   SignalBank();
+    ~SignalBank();
+    bool Initialize(int channel_count, int signal_length, float sample_rate);
+    bool Initialize(const SignalBank &input);
+    bool Validate() const;
+    const vector<float> &operator[](int channel) const;
+    float sample(int channel, int index) const;
+    void set_sample(int channel, int index, float value);
+    float sample_rate() const;
+    int buffer_length() const;
+    int start_time() const;
+    void set_start_time(int start_time);
+    float get_centre_frequency(int i) const;
+    void set_centre_frequency(int i, float cf);
+    bool initialized() const;
+    int channel_count() const;
 };
 
 class Module {
--- a/swig/example.py	Fri Feb 12 12:31:23 2010 +0000
+++ b/swig/example.py	Mon Feb 15 20:37:26 2010 +0000
@@ -2,7 +2,7 @@
 # Copyright 2010, Thomas Walters
 #
 # AIM-C: A C++ implementation of the Auditory Image Model
-# http:#www.acousticscale.org/AIMC
+# http://www.acousticscale.org/AIMC
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -15,23 +15,13 @@
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http:#www.gnu.org/licenses/>.
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import aimc
-import matplotlib
+params = aimc.Parameters()
+mod_gauss = aimc.ModuleGaussians(params)
+sig = aimc.SignalBank()
+sig.Initialize(115, 1, 44100)
+mod_gauss.Initialize(sig)
+mod_gauss.Process(sig)
 
-parameters = aimc.Parameters()
-parameters.LoadOnly('defaults.cfg')
-
-buffer_length = 1000
-sample_rate = 44100
-
-input_signal = aimc.SignalBank()
-input_signal.Initialize(1, buffer_length, sample_rate)
-
-filterbank = aimc.ModuleBMMPZFC(parameters)
-filterbank.Initialize(input_signal)
-
-filterbank_output = filterbank.getOutputBank()
-
-#for s in range(0, buffer_length):
--- a/swig/setup.py	Fri Feb 12 12:31:23 2010 +0000
+++ b/swig/setup.py	Mon Feb 15 20:37:26 2010 +0000
@@ -2,7 +2,7 @@
 # Copyright 2010, Thomas Walters
 #
 # AIM-C: A C++ implementation of the Auditory Image Model
-# http:#www.acousticscale.org/AIMC
+# http://www.acousticscale.org/AIMC
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -15,7 +15,7 @@
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http:#www.gnu.org/licenses/>.
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 setup.py file for SWIG wrappers around aimc
@@ -27,7 +27,8 @@
                         sources = ['aim_modules.i',
                                    '../src/Support/Common.cc',
                                    '../src/Support/Parameters.cc',
-                                   '../src/Support/Module.cc', 
+                                   '../src/Support/SignalBank.cc', 
+                                   '../src/Support/Module.cc',
                                    '../src/Modules/Features/ModuleGaussians.cc'],
                         swig_opts = ['-c++','-I../src/'], 
                         include_dirs=['../src/']