Mercurial > hg > aimc
changeset 419:31d2b3f2c13b
- Adding ModuleBoxes
author | tom@acousticscale.org |
---|---|
date | Tue, 26 Oct 2010 04:09:31 +0000 |
parents | b6d5c0cc1849 |
children | 733a11a65f3d |
files | trunk/SConstruct trunk/src/Modules/Features/ModuleBoxes.cc trunk/src/Modules/Features/ModuleBoxes.h |
diffstat | 3 files changed, 213 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/trunk/SConstruct Tue Oct 26 00:03:29 2010 +0000 +++ b/trunk/SConstruct Tue Oct 26 04:09:31 2010 +0000 @@ -51,7 +51,8 @@ 'Modules/Profile/ModuleScaler.cc', 'Modules/Output/FileOutputHTK.cc', 'Modules/Output/FileOutputAIMC.cc', - 'Modules/Features/ModuleGaussians.cc',] + 'Modules/Features/ModuleGaussians.cc', + 'Modules/Features/ModuleBoxes.cc',] #'Modules/Features/ModuleDCT.cc' ] graphics_sources = [ 'Modules/Output/Graphics/GraphAxisSpec.cc',
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/Modules/Features/ModuleBoxes.cc Tue Oct 26 04:09:31 2010 +0000 @@ -0,0 +1,146 @@ +// Copyright 2010, Google +// +// AIM-C: A C++ implementation of the Auditory Image Model +// http://www.acousticscale.org/AIMC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/*! + * \author Thomas Walters <tom@acousticscale.org> + * \date created 2010-06-14 + * \version \$Id$ + */ + +#include "Modules/Features/ModuleBoxes.h" + +namespace aimc { +ModuleBoxes::ModuleBoxes(Parameters *params) : Module(params) { + module_description_ = "'Box-cutting' routine to generate dense features"; + module_identifier_ = "boxes"; + module_type_ = "features"; + module_version_ = "$Id$"; + + box_size_spectral_ = parameters_->DefaultInt("boxes.spectral_size", 16); + box_size_temporal_ = parameters_->DefaultInt("boxes.temporal_size", 32); +} + +ModuleBoxes::~ModuleBoxes() { +} + +bool ModuleBoxes::InitializeInternal(const SignalBank &input) { + // Copy the parameters of the input signal bank into internal variables, so + // that they can be checked later. + sample_rate_ = input.sample_rate(); + buffer_length_ = input.buffer_length(); + channel_count_ = input.channel_count(); + + int channels_height = box_size_spectral_; + while (channels_height < channel_count_ / 2) { + int top = channel_count_ - 1; + while (top - channels_height >= 0) { + box_limits_channels_.push_back(std::make_pair(top, + top - channels_height)); + LOG_INFO("ch: t %d, b %d", top, top - channels_height); + top -= channels_height / 2; + } + channels_height *= 2; + } + + int temporal_width = box_size_temporal_; + while (temporal_width < buffer_length_) { + box_limits_time_.push_back(temporal_width); + LOG_INFO("sp: %d", temporal_width); + temporal_width *= 2; + } + + box_count_ = box_limits_time_.size() * box_limits_channels_.size(); + feature_size_ = box_size_spectral_ + box_size_temporal_; + LOG_INFO("Total box count is %d", box_count_); + LOG_INFO("Total feature size is %d", feature_size_); + + output_.Initialize(box_count_, feature_size_, 1.0f); + return true; +} + +void ModuleBoxes::ResetInternal() { +} + +void ModuleBoxes::Process(const SignalBank &input) { + // Check to see if the module has been initialized. If not, processing + // should not continue. + if (!initialized_) { + LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str()); + return; + } + + // Check that ths input this time is the same as the input passed to + // Initialize() + if (buffer_length_ != input.buffer_length() + || channel_count_ != input.channel_count()) { + LOG_ERROR(_T("Mismatch between input to Initialize() and input to " + "Process() in module %s."), module_identifier_.c_str()); + return; + } + + int box_index = 0; + for (int c = 0; c < static_cast<int>(box_limits_channels_.size()); ++c) { + for (int s = 0; s < static_cast<int>(box_limits_time_.size()); ++s) { + int pixel_size_channels = (box_limits_channels_[c].first + - box_limits_channels_[c].second) + / box_size_spectral_; + int pixel_size_samples = box_limits_time_[s] / box_size_temporal_; + vector<vector<float> > box; + vector<float> line; + line.resize(box_size_temporal_, 0.0f); + box.resize(box_size_spectral_, line); + for (int i = 0; i < box_size_spectral_; ++i) { + for (int j = 0; j < box_size_temporal_; ++j) { + float pixel_value = 0.0f; + for (int k = i * pixel_size_channels; + k < (i + 1) * pixel_size_channels; ++k) { + for (int l = j * pixel_size_samples; + l < (j + 1) * pixel_size_samples; ++l) { + pixel_value += input.sample(k + + box_limits_channels_[c].second, l); + } + } + pixel_value /= (pixel_size_channels * pixel_size_samples); + box[i][j] = pixel_value; + } + } + int feature_index = 0; + for (int i = 0; i < box_size_spectral_; ++i) { + float feature_value = 0.0f; + for (int j = 0; j < box_size_temporal_; ++j) { + feature_value += box[i][j]; + } + feature_value /= box_size_temporal_; + output_.set_sample(box_index, feature_index, feature_value); + ++feature_index; + } + for (int j = 0; j < box_size_temporal_; ++j) { + float feature_value = 0.0f; + for (int i = 0; i < box_size_spectral_; ++i) { + feature_value += box[i][j]; + } + feature_value /= box_size_spectral_; + output_.set_sample(box_index, feature_index, feature_value); + ++feature_index; + } + ++box_index; + } + } + + PushOutput(); +} +} // namespace aimc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/Modules/Features/ModuleBoxes.h Tue Oct 26 04:09:31 2010 +0000 @@ -0,0 +1,65 @@ +// Copyright 2010, Google +// +// AIM-C: A C++ implementation of the Auditory Image Model +// http://www.acousticscale.org/AIMC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/*! + * \author Thomas Walters <tom@acousticscale.org> + * \date created 2010-06-14 + * \version \$Id$ + */ + +#ifndef AIMC_MODULES_FEATURES_BOXES_H_ +#define AIMC_MODULES_FEATURES_BOXES_H_ + +#include <utility> +#include <vector> +#include "Support/Module.h" + +namespace aimc { +using std::pair; +class ModuleBoxes : public Module { + public: + explicit ModuleBoxes(Parameters *pParam); + virtual ~ModuleBoxes(); + + /*! \brief Process a buffer + */ + virtual void Process(const SignalBank &input); + + private: + /*! \brief Reset the internal state of the module + */ + virtual void ResetInternal(); + + /*! \brief Prepare the module + * \param input Input signal + * \param output true on success false on failure + */ + virtual bool InitializeInternal(const SignalBank &input); + + float sample_rate_; + int buffer_length_; + int channel_count_; + int box_size_spectral_; + int box_size_temporal_; + vector<int> box_limits_time_; + vector<pair<int, int> > box_limits_channels_; + int box_count_; + int feature_size_; +}; +} // namespace aimc + +#endif // AIMC_MODULES_FEATURES_BOXES_H_