annotate src/Modules/Profile/ModuleSlice.cc @ 113:9d12efd43513

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