annotate trunk/src/Modules/Profile/ModuleSlice.cc @ 304:e4f704f67ca6

-Typo in file path
author tomwalters
date Wed, 24 Feb 2010 15:18:00 +0000
parents 10d0803e37ec
children ed91095d9240
rev   line source
tomwalters@284 1 // Copyright 2010, Thomas Walters
tomwalters@284 2 //
tomwalters@284 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@284 4 // http://www.acousticscale.org/AIMC
tomwalters@284 5 //
tomwalters@284 6 // This program is free software: you can redistribute it and/or modify
tomwalters@284 7 // it under the terms of the GNU General Public License as published by
tomwalters@284 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@284 9 // (at your option) any later version.
tomwalters@284 10 //
tomwalters@284 11 // This program is distributed in the hope that it will be useful,
tomwalters@284 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@284 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@284 14 // GNU General Public License for more details.
tomwalters@284 15 //
tomwalters@284 16 // You should have received a copy of the GNU General Public License
tomwalters@284 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@284 18
tomwalters@284 19 /*!
tomwalters@284 20 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@284 21 * \date created 2010/02/19
tomwalters@284 22 * \version \$Id$
tomwalters@284 23 */
tomwalters@284 24
tomwalters@284 25 #include "Modules/Profile/ModuleSlice.h"
tomwalters@284 26
tomwalters@284 27 namespace aimc {
tomwalters@284 28 ModuleSlice::ModuleSlice(Parameters *params) : Module(params) {
tomwalters@284 29 module_description_ = "Temporal or spectral slice of a 2D image";
tomwalters@284 30 module_identifier_ = "slice";
tomwalters@284 31 module_type_ = "profile";
tomwalters@284 32 module_version_ = "$Id$";
tomwalters@284 33
tomwalters@284 34 // This module will compute the spectral profile unless told otherwise here
tomwalters@284 35 temporal_profile_ = parameters_->DefaultBool("slice.temporal", false);
tomwalters@284 36 // Set slice.all to true to take the profile of the entire image
tomwalters@284 37 take_all_ = parameters_->DefaultBool("slice.all", true);
tomwalters@284 38 // If not taking all, then these give the lower and upper indices of the
tomwalters@284 39 // section to take. They are bounds-checked.
tomwalters@284 40 lower_limit_ = parameters_->DefaultInt("slice.lower_index", 0);
tomwalters@284 41 upper_limit_ = parameters_->DefaultInt("slice.upper_index", 1000);
tomwalters@284 42 // Set to true to normalize the slice taken (ie take the mean value)
tomwalters@284 43 normalize_slice_ = parameters_->DefaultBool("slice.normalize", false);
tomwalters@284 44 }
tomwalters@284 45
tomwalters@284 46 ModuleSlice::~ModuleSlice() {
tomwalters@284 47 }
tomwalters@284 48
tomwalters@284 49 bool ModuleSlice::InitializeInternal(const SignalBank &input) {
tomwalters@284 50 // Copy the parameters of the input signal bank into internal variables, so
tomwalters@284 51 // that they can be checked later.
tomwalters@284 52 sample_rate_ = input.sample_rate();
tomwalters@284 53 buffer_length_ = input.buffer_length();
tomwalters@284 54 channel_count_ = input.channel_count();
tomwalters@284 55
tomwalters@284 56 if (lower_limit_ < 0) {
tomwalters@284 57 lower_limit_ = 0;
tomwalters@284 58 }
tomwalters@284 59
tomwalters@284 60 if (upper_limit_ < 0) {
tomwalters@284 61 upper_limit_ = 0;
tomwalters@284 62 }
tomwalters@284 63
tomwalters@284 64 if (temporal_profile_) {
tomwalters@284 65 if (upper_limit_ > channel_count_) {
tomwalters@284 66 upper_limit_ = channel_count_;
tomwalters@284 67 }
tomwalters@284 68 if (lower_limit_ > channel_count_) {
tomwalters@284 69 lower_limit_ = channel_count_;
tomwalters@284 70 }
tomwalters@284 71 } else {
tomwalters@284 72 if (upper_limit_ > buffer_length_) {
tomwalters@284 73 upper_limit_ = buffer_length_;
tomwalters@284 74 }
tomwalters@284 75 if (lower_limit_ > buffer_length_) {
tomwalters@284 76 lower_limit_ = buffer_length_;
tomwalters@284 77 }
tomwalters@284 78 }
tomwalters@284 79
tomwalters@284 80 slice_length_ = upper_limit_ - lower_limit_;
tomwalters@284 81 if (slice_length_ < 1) {
tomwalters@284 82 slice_length_ = 1;
tomwalters@284 83 }
tomwalters@284 84
tomwalters@284 85 if (temporal_profile_) {
tomwalters@284 86 output_.Initialize(1, buffer_length_, sample_rate_);
tomwalters@284 87 } else {
tomwalters@284 88 output_.Initialize(channel_count_, 1, sample_rate_);
tomwalters@284 89 }
tomwalters@284 90 return true;
tomwalters@284 91 }
tomwalters@284 92
tomwalters@284 93 void ModuleSlice::ResetInternal() {
tomwalters@284 94 }
tomwalters@284 95
tomwalters@284 96 void ModuleSlice::Process(const SignalBank &input) {
tomwalters@284 97 // Check to see if the module has been initialized. If not, processing
tomwalters@284 98 // should not continue.
tomwalters@284 99 if (!initialized_) {
tomwalters@284 100 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tomwalters@284 101 return;
tomwalters@284 102 }
tomwalters@284 103
tomwalters@284 104 // Check that ths input this time is the same as the input passed to
tomwalters@284 105 // Initialize()
tomwalters@284 106 if (buffer_length_ != input.buffer_length()
tomwalters@284 107 || channel_count_ != input.channel_count()) {
tomwalters@284 108 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tomwalters@284 109 "Process() in module %s."), module_identifier_.c_str());
tomwalters@284 110 return;
tomwalters@284 111 }
tomwalters@284 112
tomwalters@287 113 output_.set_start_time(input.start_time());
tomwalters@287 114
tomwalters@284 115 if (temporal_profile_) {
tomwalters@284 116 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@284 117 float val = 0.0f;
tomwalters@284 118 for (int ch = lower_limit_; ch < upper_limit_; ++ch) {
tomwalters@284 119 val += input.sample(ch, i);
tomwalters@284 120 }
tomwalters@284 121 if (normalize_slice_) {
tomwalters@284 122 val /= static_cast<float>(slice_length_);
tomwalters@284 123 }
tomwalters@284 124 output_.set_sample(0, i, val);
tomwalters@284 125 }
tomwalters@284 126 } else {
tomwalters@284 127 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@292 128 output_.set_centre_frequency(ch, input.centre_frequency(ch));
tomwalters@284 129 float val = 0.0f;
tomwalters@284 130 for (int i = lower_limit_; i < upper_limit_; ++i) {
tomwalters@284 131 val += input.sample(ch, i);
tomwalters@284 132 }
tomwalters@284 133 if (normalize_slice_) {
tomwalters@284 134 val /= static_cast<float>(slice_length_);
tomwalters@284 135 }
tomwalters@284 136 output_.set_sample(ch, 0, val);
tomwalters@284 137 }
tomwalters@284 138 }
tomwalters@284 139 PushOutput();
tomwalters@284 140 }
tomwalters@284 141 } // namespace aimc
tomwalters@284 142