tomwalters@284: // Copyright 2010, Thomas Walters
tomwalters@284: //
tomwalters@284: // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@284: // http://www.acousticscale.org/AIMC
tomwalters@284: //
tomwalters@284: // This program is free software: you can redistribute it and/or modify
tomwalters@284: // it under the terms of the GNU General Public License as published by
tomwalters@284: // the Free Software Foundation, either version 3 of the License, or
tomwalters@284: // (at your option) any later version.
tomwalters@284: //
tomwalters@284: // This program is distributed in the hope that it will be useful,
tomwalters@284: // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@284: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@284: // GNU General Public License for more details.
tomwalters@284: //
tomwalters@284: // You should have received a copy of the GNU General Public License
tomwalters@284: // along with this program. If not, see .
tomwalters@284:
tomwalters@284: /*!
tomwalters@284: * \author Thomas Walters
tomwalters@284: * \date created 2010/02/19
tomwalters@284: * \version \$Id$
tomwalters@284: */
tomwalters@284:
tomwalters@284: #include "Modules/Profile/ModuleSlice.h"
tomwalters@284:
tomwalters@284: namespace aimc {
tomwalters@284: ModuleSlice::ModuleSlice(Parameters *params) : Module(params) {
tomwalters@284: module_description_ = "Temporal or spectral slice of a 2D image";
tomwalters@284: module_identifier_ = "slice";
tomwalters@284: module_type_ = "profile";
tomwalters@284: module_version_ = "$Id$";
tomwalters@284:
tomwalters@284: // This module will compute the spectral profile unless told otherwise here
tomwalters@284: temporal_profile_ = parameters_->DefaultBool("slice.temporal", false);
tomwalters@284: // Set slice.all to true to take the profile of the entire image
tomwalters@284: take_all_ = parameters_->DefaultBool("slice.all", true);
tomwalters@284: // If not taking all, then these give the lower and upper indices of the
tomwalters@284: // section to take. They are bounds-checked.
tomwalters@284: lower_limit_ = parameters_->DefaultInt("slice.lower_index", 0);
tomwalters@284: upper_limit_ = parameters_->DefaultInt("slice.upper_index", 1000);
tomwalters@284: // Set to true to normalize the slice taken (ie take the mean value)
tomwalters@284: normalize_slice_ = parameters_->DefaultBool("slice.normalize", false);
tomwalters@284: }
tomwalters@284:
tomwalters@284: ModuleSlice::~ModuleSlice() {
tomwalters@284: }
tomwalters@284:
tomwalters@284: bool ModuleSlice::InitializeInternal(const SignalBank &input) {
tomwalters@284: // Copy the parameters of the input signal bank into internal variables, so
tomwalters@284: // that they can be checked later.
tomwalters@284: sample_rate_ = input.sample_rate();
tomwalters@284: buffer_length_ = input.buffer_length();
tomwalters@284: channel_count_ = input.channel_count();
tomwalters@284:
tomwalters@305: if (lower_limit_ < 0 || take_all_) {
tomwalters@284: lower_limit_ = 0;
tomwalters@284: }
tomwalters@284:
tomwalters@284: if (upper_limit_ < 0) {
tomwalters@284: upper_limit_ = 0;
tomwalters@284: }
tomwalters@284:
tomwalters@284: if (temporal_profile_) {
tomwalters@305: if (upper_limit_ > channel_count_ || take_all_) {
tomwalters@284: upper_limit_ = channel_count_;
tomwalters@284: }
tomwalters@284: if (lower_limit_ > channel_count_) {
tomwalters@284: lower_limit_ = channel_count_;
tomwalters@284: }
tomwalters@284: } else {
tomwalters@305: if (upper_limit_ > buffer_length_ || take_all_) {
tomwalters@284: upper_limit_ = buffer_length_;
tomwalters@284: }
tomwalters@284: if (lower_limit_ > buffer_length_) {
tomwalters@284: lower_limit_ = buffer_length_;
tomwalters@284: }
tomwalters@284: }
tomwalters@284:
tomwalters@284: slice_length_ = upper_limit_ - lower_limit_;
tomwalters@284: if (slice_length_ < 1) {
tomwalters@284: slice_length_ = 1;
tomwalters@284: }
tomwalters@284:
tomwalters@284: if (temporal_profile_) {
tomwalters@284: output_.Initialize(1, buffer_length_, sample_rate_);
tomwalters@284: } else {
tomwalters@284: output_.Initialize(channel_count_, 1, sample_rate_);
tomwalters@284: }
tomwalters@284: return true;
tomwalters@284: }
tomwalters@284:
tomwalters@284: void ModuleSlice::ResetInternal() {
tomwalters@284: }
tomwalters@284:
tomwalters@284: void ModuleSlice::Process(const SignalBank &input) {
tomwalters@284: // Check to see if the module has been initialized. If not, processing
tomwalters@284: // should not continue.
tomwalters@284: if (!initialized_) {
tomwalters@284: LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tomwalters@284: return;
tomwalters@284: }
tomwalters@284:
tomwalters@284: // Check that ths input this time is the same as the input passed to
tomwalters@284: // Initialize()
tomwalters@284: if (buffer_length_ != input.buffer_length()
tomwalters@284: || channel_count_ != input.channel_count()) {
tomwalters@284: LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tomwalters@284: "Process() in module %s."), module_identifier_.c_str());
tomwalters@284: return;
tomwalters@284: }
tomwalters@284:
tomwalters@287: output_.set_start_time(input.start_time());
tomwalters@287:
tomwalters@284: if (temporal_profile_) {
tomwalters@284: for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@284: float val = 0.0f;
tomwalters@284: for (int ch = lower_limit_; ch < upper_limit_; ++ch) {
tomwalters@284: val += input.sample(ch, i);
tomwalters@284: }
tomwalters@284: if (normalize_slice_) {
tomwalters@284: val /= static_cast(slice_length_);
tomwalters@284: }
tomwalters@284: output_.set_sample(0, i, val);
tomwalters@284: }
tomwalters@284: } else {
tomwalters@284: for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@292: output_.set_centre_frequency(ch, input.centre_frequency(ch));
tomwalters@284: float val = 0.0f;
tomwalters@284: for (int i = lower_limit_; i < upper_limit_; ++i) {
tomwalters@284: val += input.sample(ch, i);
tomwalters@284: }
tomwalters@284: if (normalize_slice_) {
tomwalters@284: val /= static_cast(slice_length_);
tomwalters@284: }
tomwalters@284: output_.set_sample(ch, 0, val);
tomwalters@284: }
tomwalters@284: }
tomwalters@284: PushOutput();
tomwalters@284: }
tomwalters@284: } // namespace aimc
tomwalters@284: