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