annotate src/Modules/Features/ModuleBoxes.cc @ 153:9a98efa01965

small change in python aimc data write
author hamel.phil
date Mon, 10 Jan 2011 16:01:10 +0000
parents 5d4b269b67d2
children
rev   line source
tom@134 1 // Copyright 2010, Google
tom@134 2 //
tom@134 3 // AIM-C: A C++ implementation of the Auditory Image Model
tom@134 4 // http://www.acousticscale.org/AIMC
tom@134 5 //
tom@134 6 // Licensed under the Apache License, Version 2.0 (the "License");
tom@134 7 // you may not use this file except in compliance with the License.
tom@134 8 // You may obtain a copy of the License at
tom@134 9 //
tom@134 10 // http://www.apache.org/licenses/LICENSE-2.0
tom@134 11 //
tom@134 12 // Unless required by applicable law or agreed to in writing, software
tom@134 13 // distributed under the License is distributed on an "AS IS" BASIS,
tom@134 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@134 15 // See the License for the specific language governing permissions and
tom@134 16 // limitations under the License.
tom@134 17
tom@134 18 /*!
tom@134 19 * \author Thomas Walters <tom@acousticscale.org>
tom@134 20 * \date created 2010-06-14
tom@134 21 * \version \$Id$
tom@134 22 */
tom@134 23
tom@134 24 #include "Modules/Features/ModuleBoxes.h"
tom@134 25
tom@134 26 namespace aimc {
tom@134 27 ModuleBoxes::ModuleBoxes(Parameters *params) : Module(params) {
tom@134 28 module_description_ = "'Box-cutting' routine to generate dense features";
tom@134 29 module_identifier_ = "boxes";
tom@134 30 module_type_ = "features";
tom@134 31 module_version_ = "$Id$";
tom@134 32
tom@134 33 box_size_spectral_ = parameters_->DefaultInt("boxes.spectral_size", 16);
tom@134 34 box_size_temporal_ = parameters_->DefaultInt("boxes.temporal_size", 32);
tom@134 35 }
tom@134 36
tom@134 37 ModuleBoxes::~ModuleBoxes() {
tom@134 38 }
tom@134 39
tom@134 40 bool ModuleBoxes::InitializeInternal(const SignalBank &input) {
tom@134 41 // Copy the parameters of the input signal bank into internal variables, so
tom@134 42 // that they can be checked later.
tom@134 43 sample_rate_ = input.sample_rate();
tom@134 44 buffer_length_ = input.buffer_length();
tom@134 45 channel_count_ = input.channel_count();
tom@134 46
tom@134 47 int channels_height = box_size_spectral_;
tom@134 48 while (channels_height < channel_count_ / 2) {
tom@134 49 int top = channel_count_ - 1;
tom@134 50 while (top - channels_height >= 0) {
tom@134 51 box_limits_channels_.push_back(std::make_pair(top,
tom@134 52 top - channels_height));
tom@134 53 LOG_INFO("ch: t %d, b %d", top, top - channels_height);
tom@134 54 top -= channels_height / 2;
tom@134 55 }
tom@134 56 channels_height *= 2;
tom@134 57 }
tom@134 58
tom@134 59 int temporal_width = box_size_temporal_;
tom@134 60 while (temporal_width < buffer_length_) {
tom@134 61 box_limits_time_.push_back(temporal_width);
tom@134 62 LOG_INFO("sp: %d", temporal_width);
tom@134 63 temporal_width *= 2;
tom@134 64 }
tom@134 65
tom@134 66 box_count_ = box_limits_time_.size() * box_limits_channels_.size();
tom@134 67 feature_size_ = box_size_spectral_ + box_size_temporal_;
tom@134 68 LOG_INFO("Total box count is %d", box_count_);
tom@134 69 LOG_INFO("Total feature size is %d", feature_size_);
tom@134 70
tom@134 71 output_.Initialize(box_count_, feature_size_, 1.0f);
tom@134 72 return true;
tom@134 73 }
tom@134 74
tom@134 75 void ModuleBoxes::ResetInternal() {
tom@134 76 }
tom@134 77
tom@134 78 void ModuleBoxes::Process(const SignalBank &input) {
tom@134 79 // Check to see if the module has been initialized. If not, processing
tom@134 80 // should not continue.
tom@134 81 if (!initialized_) {
tom@134 82 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tom@134 83 return;
tom@134 84 }
tom@134 85
tom@134 86 // Check that ths input this time is the same as the input passed to
tom@134 87 // Initialize()
tom@134 88 if (buffer_length_ != input.buffer_length()
tom@134 89 || channel_count_ != input.channel_count()) {
tom@134 90 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tom@134 91 "Process() in module %s."), module_identifier_.c_str());
tom@134 92 return;
tom@134 93 }
tom@134 94
tom@134 95 int box_index = 0;
tom@134 96 for (int c = 0; c < static_cast<int>(box_limits_channels_.size()); ++c) {
tom@134 97 for (int s = 0; s < static_cast<int>(box_limits_time_.size()); ++s) {
tom@134 98 int pixel_size_channels = (box_limits_channels_[c].first
tom@134 99 - box_limits_channels_[c].second)
tom@134 100 / box_size_spectral_;
tom@134 101 int pixel_size_samples = box_limits_time_[s] / box_size_temporal_;
tom@134 102 vector<vector<float> > box;
tom@134 103 vector<float> line;
tom@134 104 line.resize(box_size_temporal_, 0.0f);
tom@134 105 box.resize(box_size_spectral_, line);
tom@134 106 for (int i = 0; i < box_size_spectral_; ++i) {
tom@134 107 for (int j = 0; j < box_size_temporal_; ++j) {
tom@134 108 float pixel_value = 0.0f;
tom@134 109 for (int k = i * pixel_size_channels;
tom@134 110 k < (i + 1) * pixel_size_channels; ++k) {
tom@134 111 for (int l = j * pixel_size_samples;
tom@134 112 l < (j + 1) * pixel_size_samples; ++l) {
tom@134 113 pixel_value += input.sample(k
tom@134 114 + box_limits_channels_[c].second, l);
tom@134 115 }
tom@134 116 }
tom@134 117 pixel_value /= (pixel_size_channels * pixel_size_samples);
tom@134 118 box[i][j] = pixel_value;
tom@134 119 }
tom@134 120 }
tom@134 121 int feature_index = 0;
tom@134 122 for (int i = 0; i < box_size_spectral_; ++i) {
tom@134 123 float feature_value = 0.0f;
tom@134 124 for (int j = 0; j < box_size_temporal_; ++j) {
tom@134 125 feature_value += box[i][j];
tom@134 126 }
tom@134 127 feature_value /= box_size_temporal_;
tom@134 128 output_.set_sample(box_index, feature_index, feature_value);
tom@134 129 ++feature_index;
tom@134 130 }
tom@134 131 for (int j = 0; j < box_size_temporal_; ++j) {
tom@134 132 float feature_value = 0.0f;
tom@134 133 for (int i = 0; i < box_size_spectral_; ++i) {
tom@134 134 feature_value += box[i][j];
tom@134 135 }
tom@134 136 feature_value /= box_size_spectral_;
tom@134 137 output_.set_sample(box_index, feature_index, feature_value);
tom@134 138 ++feature_index;
tom@134 139 }
tom@134 140 ++box_index;
tom@134 141 }
tom@134 142 }
tom@134 143
tom@134 144 PushOutput();
tom@134 145 }
tom@134 146 } // namespace aimc