Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/AimBoxes.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "AimBoxes.h"
00020 #include "../common_source.h"
00021 
00022 
00023 using std::ostringstream;
00024 using std::vector;
00025 
00026 using namespace Marsyas;
00027 
00028 AimBoxes::AimBoxes(mrs_string name):MarSystem("AimBoxes",name)
00029 {
00030   is_initialized = false;
00031   initialized_israte = 0.0;
00032   initialized_inobservations = 0;
00033   initialized_insamples = 0;
00034   initialized_box_size_spectral = 0;
00035   initialized_box_size_temporal = 0;
00036 
00037   is_reset = false;
00038   addControls();
00039 }
00040 
00041 AimBoxes::AimBoxes(const AimBoxes& a): MarSystem(a)
00042 {
00043   is_initialized = false;
00044   initialized_israte = 0.0;
00045   initialized_inobservations = 0;
00046   initialized_insamples = 0;
00047   initialized_box_size_spectral = 0;
00048   initialized_box_size_temporal = 0;
00049 
00050   is_reset = false;
00051 
00052   ctrl_box_size_spectral_= getctrl("mrs_natural/box_size_spectral");
00053   ctrl_box_size_temporal_= getctrl("mrs_natural/box_size_temporal");
00054 }
00055 
00056 AimBoxes::~AimBoxes()
00057 {
00058 }
00059 
00060 MarSystem*
00061 AimBoxes::clone() const
00062 {
00063   return new AimBoxes(*this);
00064 }
00065 
00066 void
00067 AimBoxes::addControls()
00068 {
00069   addControl("mrs_natural/box_size_spectral", 16 , ctrl_box_size_spectral_);
00070   addControl("mrs_natural/box_size_temporal", 32 , ctrl_box_size_temporal_);
00071 }
00072 
00073 void
00074 AimBoxes::myUpdate(MarControlPtr sender)
00075 {
00076 
00077   // sness - We calculate box_count_ and feature_size_ inside
00078   // InitializeInternal, so in this MarSystem we first of all call
00079   // InitializeInternal, and then update.  We might want to use the
00080   // same pattern in the other Aim modules
00081 
00082   //
00083   // Does the MarSystem need initialization?
00084   //
00085   if (initialized_israte != ctrl_israte_->to<mrs_real>() ||
00086       initialized_inobservations != ctrl_inObservations_->to<mrs_natural>() ||
00087       initialized_insamples != ctrl_inSamples_->to<mrs_natural>() ||
00088       initialized_box_size_spectral != ctrl_box_size_spectral_->to<mrs_natural>() ||
00089       initialized_box_size_temporal != ctrl_box_size_temporal_->to<mrs_natural>()) {
00090     is_initialized = false;
00091   }
00092 
00093   if (!is_initialized) {
00094     InitializeInternal();
00095     is_initialized = true;
00096     initialized_israte = ctrl_israte_->to<mrs_real>();
00097     initialized_inobservations = ctrl_inObservations_->to<mrs_natural>();
00098     initialized_insamples = ctrl_inSamples_->to<mrs_natural>();
00099     initialized_box_size_spectral = ctrl_box_size_spectral_->to<mrs_natural>();
00100     initialized_box_size_temporal = ctrl_box_size_temporal_->to<mrs_natural>();
00101   }
00102 
00103   (void) sender;  //suppress warning of unused parameter(s)
00104   MRSDIAG("AimBoxes.cpp - AimBoxes:myUpdate");
00105   // ctrl_onSamples_->setValue(box_count_, NOUPDATE);
00106   // ctrl_onObservations_->setValue(feature_size_, NOUPDATE);
00107   ctrl_onSamples_->setValue(feature_size_, NOUPDATE);
00108   ctrl_onObservations_->setValue(box_count_, NOUPDATE);
00109   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00110   ctrl_onObsNames_->setValue("AimBoxes_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00111 
00112   // //
00113   // // Does the MarSystem need a reset?
00114   // //
00115   // if (reset_inobservations != ctrl_inObservations_->to<mrs_natural>()) {
00116   //   is_reset = false;
00117   // }
00118 
00119   // if (!is_reset) {
00120   //   ResetInternal();
00121   //   is_reset = true;
00122   //   reset_inobservations = ctrl_inObservations_->to<mrs_natural>();
00123   // }
00124 
00125 }
00126 
00127 void
00128 AimBoxes::InitializeInternal() {
00129   // mrs_real sample_rate = ctrl_israte_->to<mrs_real>();
00130   mrs_natural buffer_length = ctrl_inSamples_->to<mrs_natural>();
00131   mrs_natural channel_count = ctrl_inObservations_->to<mrs_natural>();
00132   mrs_natural channels_height = ctrl_box_size_spectral_->to<mrs_natural>();
00133   mrs_natural temporal_width = ctrl_box_size_temporal_->to<mrs_natural>();
00134 
00135   // If these aren't initialized yet, then the MarSystem hasn't
00136   // updated, so just return.
00137   if ((channels_height == 0) || (temporal_width == 0)) {
00138     return;
00139   }
00140 
00141   box_limits_channels_.clear();
00142   box_limits_time_.clear();
00143 
00144   while (channels_height < channel_count / 2) {
00145     int top = channel_count - 1;
00146     while (top - channels_height >= 0) {
00147       box_limits_channels_.push_back(std::make_pair(top,
00148                                      top - channels_height));
00149       top -= channels_height / 2;
00150     }
00151     channels_height *= 2;
00152   }
00153 
00154   while (temporal_width < buffer_length) {
00155     box_limits_time_.push_back(temporal_width);
00156     temporal_width *= 2;
00157   }
00158 
00159   box_count_ = (int) (box_limits_time_.size() * box_limits_channels_.size());
00160   feature_size_ = ctrl_box_size_spectral_->to<mrs_natural>() + ctrl_box_size_temporal_->to<mrs_natural>();
00161 
00162 }
00163 
00164 void
00165 AimBoxes::ResetInternal() {
00166 }
00167 
00168 void
00169 AimBoxes::myProcess(realvec& in, realvec& out)
00170 {
00171   mrs_natural box_size_temporal = ctrl_box_size_temporal_->to<mrs_natural>();
00172   mrs_natural box_size_spectral = ctrl_box_size_spectral_->to<mrs_natural>();
00173 
00174   int box_index = 0;
00175   for (int c = 0; c < static_cast<int>(box_limits_channels_.size()); ++c) {
00176     for (int s = 0; s < static_cast<int>(box_limits_time_.size()); ++s) {
00177       int pixel_size_channels = (box_limits_channels_[c].first
00178                                  - box_limits_channels_[c].second)
00179                                 / box_size_spectral;
00180       int pixel_size_samples = box_limits_time_[s] / box_size_temporal;
00181       vector<vector<float> > box;
00182       vector<float> line;
00183       line.resize(box_size_temporal, 0.0f);
00184       box.resize(box_size_spectral, line);
00185       for (int i = 0; i < box_size_spectral; ++i) {
00186         for (int j = 0; j < box_size_temporal; ++j) {
00187           float pixel_value = 0.0f;
00188           for (int k = i * pixel_size_channels; k < (i + 1) * pixel_size_channels; ++k) {
00189             for (int l = j * pixel_size_samples; l < (j + 1) * pixel_size_samples; ++l) {
00190               pixel_value += (float) in(k + box_limits_channels_[c].second, l);
00191               // pixel_value += input.sample(k, l);
00192             }
00193           }
00194           pixel_value /= (float) (pixel_size_channels * pixel_size_samples);
00195           box[i][j] = pixel_value;
00196         }
00197       }
00198       int feature_index = 0;
00199       for (int i = 0; i < box_size_spectral; ++i) {
00200         float feature_value = 0.0f;
00201         for (int j = 0; j < box_size_temporal; ++j) {
00202           feature_value += box[i][j];
00203         }
00204         feature_value /= (float)box_size_temporal;
00205         // output_.set_sample(box_index, feature_index, feature_value);
00206         out(box_index, feature_index) = feature_value;
00207         // output_.set_sample(box_index, feature_index, feature_value);
00208         ++feature_index;
00209       }
00210       for (int j = 0; j < box_size_temporal; ++j) {
00211         float feature_value = 0.0f;
00212         for (int i = 0; i < box_size_spectral; ++i) {
00213           feature_value += box[i][j];
00214         }
00215         feature_value /= (float) box_size_spectral;
00216         out(box_index,feature_index) = feature_value;
00217         // output_.set_sample(box_index, feature_index, feature_value);
00218         ++feature_index;
00219       }
00220       ++box_index;
00221     }
00222   }
00223 
00224 }