Marsyas
0.6.0-alpha
|
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 "NormMaxMin.h" 00020 #include "../common_source.h" 00021 00022 using std::ostringstream; 00023 using namespace Marsyas; 00024 00025 NormMaxMin::NormMaxMin(mrs_string name):MarSystem("NormMaxMin",name) 00026 { 00027 00028 init_ = false; 00029 00030 addControls(); 00031 prev_mode_ = "predict"; 00032 } 00033 00034 NormMaxMin::NormMaxMin(const NormMaxMin& a): MarSystem(a) 00035 { 00036 prev_mode_ = "predict"; 00037 00038 lowerPtr_ = getctrl("mrs_real/lower"); 00039 upperPtr_ = getctrl("mrs_real/upper"); 00040 initPtr_ = getctrl("mrs_bool/init"); 00041 maximumsPtr_ = getctrl("mrs_realvec/maximums"); 00042 minimumsPtr_ = getctrl("mrs_realvec/minimums"); 00043 } 00044 00045 NormMaxMin::~NormMaxMin() 00046 { 00047 } 00048 00049 MarSystem* 00050 NormMaxMin::clone() const 00051 { 00052 return new NormMaxMin(*this); 00053 } 00054 00055 void 00056 NormMaxMin::addControls() 00057 { 00058 addctrl("mrs_real/lower", 0.0, lowerPtr_); 00059 addctrl("mrs_real/upper", 1.0, upperPtr_); 00060 maximums_.create(1); 00061 minimums_.create(1); 00062 addctrl("mrs_realvec/maximums", maximums_, maximumsPtr_); 00063 addctrl("mrs_realvec/minimums", minimums_, minimumsPtr_); 00064 setctrlState("mrs_realvec/maximums", true); 00065 setctrlState("mrs_realvec/minimums", true); 00066 addctrl("mrs_string/mode", "train"); 00067 addctrl("mrs_string/domain", "observations"); 00068 00069 addctrl("mrs_natural/ignoreLast", 0); 00070 addctrl("mrs_bool/init", false, initPtr_); 00071 setctrlState("mrs_bool/init", true); 00072 } 00073 00074 void 00075 NormMaxMin::myUpdate(MarControlPtr sender) 00076 { 00077 MRSDIAG("NormMaxMin.cpp - NormMaxMin:myUpdate"); 00078 00079 MarSystem::myUpdate(sender); 00080 00081 inObservations_ = ctrl_inObservations_->to<mrs_natural>(); 00082 00083 init_ = initPtr_->to<mrs_bool>(); 00084 00085 mrs_natural msize = maximumsPtr_->to<mrs_realvec>().getSize(); 00086 mrs_natural nsize = maximums_.getSize(); 00087 00088 if (msize != nsize) 00089 { 00090 maximums_.stretch(msize); 00091 minimums_.stretch(msize); 00092 } 00093 00094 if (!init_) 00095 { 00096 maximums_.stretch(inObservations_); 00097 minimums_.stretch(inObservations_); 00098 00099 maximums_.setval(-MAXREAL); 00100 minimums_.setval(MAXREAL); 00101 maximumsPtr_->setValue(maximums_, NOUPDATE); 00102 minimumsPtr_->setValue(minimums_, NOUPDATE); 00103 } 00104 00105 mrs_string mode = getctrl("mrs_string/mode")->to<mrs_string>(); 00106 if (mode == "predict") 00107 { 00108 minimums_ = minimumsPtr_->to<mrs_realvec>(); 00109 maximums_ = maximumsPtr_->to<mrs_realvec>(); 00110 } 00111 00112 } 00113 00114 void 00115 NormMaxMin::myProcess(realvec& in, realvec& out) 00116 { 00117 init_ = true; 00118 setctrl(initPtr_, init_); 00119 00120 lower_ = lowerPtr_->to<mrs_real>(); 00121 upper_ = upperPtr_->to<mrs_real>(); 00122 00123 if (lower_ > upper_) 00124 { 00125 MRSWARN("Lower is greater than upper"); 00126 return; 00127 } 00128 00129 range_ = upper_ - lower_; 00130 00131 domain_ = getctrl("mrs_string/domain")->to<mrs_string>(); 00132 00133 if (domain_ == "slices") 00134 do_slices(in, out); 00135 else if (domain_ == "samples") 00136 do_samples(in, out); 00137 else 00138 do_observations(in, out); 00139 00140 prev_mode_ = mode_; 00141 } 00142 00143 00144 void 00145 NormMaxMin::do_observations(realvec& in, realvec& out) { 00146 mrs_natural t,o; 00147 init_ = true; 00148 setctrl(initPtr_, init_); 00149 00150 lower_ = lowerPtr_->to<mrs_real>(); 00151 upper_ = upperPtr_->to<mrs_real>(); 00152 00153 mode_ = getctrl("mrs_string/mode")->to<mrs_string>(); 00154 mrs_natural ignoreLast = getctrl("mrs_natural/ignoreLast")->to<mrs_natural>(); 00155 00156 // sness 00157 domain_ = getctrl("mrs_string/domain")->to<mrs_string>(); 00158 00159 range_ = upper_ - lower_; 00160 00161 if (mode_ == "twopass") 00162 { 00163 00164 00165 00166 // first pass calculate min/max limits 00167 for (o=0; o < inObservations_; o++) 00168 for (t = 0; t < inSamples_; t++) 00169 { 00170 if (in(o,t) > maximums_(o)) 00171 maximums_(o) = in(o,t); 00172 if (in(o,t) < minimums_(o)) 00173 minimums_(o) = in(o,t); 00174 out(o,t) = in(o,t); 00175 00176 00177 } 00178 00179 00180 00181 // second pass for normalization 00182 for (o=0; o < inObservations_-ignoreLast; o++) 00183 for (t = 0; t < inSamples_; t++) 00184 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(o)) / (maximums_(o) - minimums_(o))); 00185 00186 } 00187 00188 00189 if ((prev_mode_ == "predict") && (mode_ == "train")) 00190 { 00191 maximums_.setval(-MAXREAL); 00192 minimums_.setval(MAXREAL); 00193 maximumsPtr_->setValue(maximums_, NOUPDATE); 00194 minimumsPtr_->setValue(minimums_, NOUPDATE); 00195 } 00196 00197 if (mode_ == "train") 00198 { 00199 // first pass calculate min/max limits 00200 for (o=0; o < inObservations_; o++) 00201 for (t = 0; t < inSamples_; t++) 00202 { 00203 if (in(o,t) > maximums_(o)) 00204 maximums_(o) = in(o,t); 00205 if (in(o,t) < minimums_(o)) 00206 minimums_(o) = in(o,t); 00207 out(o,t) = in(o,t); 00208 } 00209 00210 setctrl(maximumsPtr_, maximums_); 00211 setctrl(minimumsPtr_, minimums_); 00212 00213 } 00214 00215 00216 if ((prev_mode_ == "train")&&(mode_ == "predict")) 00217 { 00218 maximums_ = maximumsPtr_->to<mrs_realvec>(); 00219 minimums_ = minimumsPtr_->to<mrs_realvec>(); 00220 } 00221 00222 00223 if (mode_ == "predict") 00224 { 00225 // second pass for normalization 00226 for (o=0; o < inObservations_-ignoreLast; o++) 00227 for (t = 0; t < inSamples_; t++) 00228 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(o)) / (maximums_(o) - minimums_(o))); 00229 } 00230 00231 } 00232 00233 00234 void 00235 NormMaxMin::do_samples(realvec& in, realvec& out) 00236 { 00237 mrs_natural o,t; 00238 init_ = true; 00239 setctrl(initPtr_, init_); 00240 00241 lower_ = lowerPtr_->to<mrs_real>(); 00242 upper_ = upperPtr_->to<mrs_real>(); 00243 00244 mode_ = getctrl("mrs_string/mode")->to<mrs_string>(); 00245 mrs_natural ignoreLast = getctrl("mrs_natural/ignoreLast")->to<mrs_natural>(); 00246 00247 domain_ = getctrl("mrs_string/domain")->to<mrs_string>(); 00248 00249 range_ = upper_ - lower_; 00250 00251 if (mode_ == "twopass") 00252 { 00253 // first pass calculate min/max limits 00254 for (t=0; t < inSamples_; t++) 00255 for (o = 0; o < inObservations_; o++) 00256 { 00257 if (in(o,t) > maximums_(t)) 00258 maximums_(t) = in(o,t); 00259 if (in(o,t) < minimums_(t)) 00260 minimums_(t) = in(o,t); 00261 out(o,t) = in(o,t); 00262 } 00263 // second pass for normalization 00264 for (t=0; t < inSamples_-ignoreLast; t++) 00265 for (o = 0; o < inObservations_; o++) 00266 { 00267 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(t)) / (maximums_(t) - minimums_(t))); 00268 } 00269 } 00270 00271 if ((prev_mode_ == "predict") && (mode_ == "train")) 00272 { 00273 maximums_.setval(-MAXREAL); 00274 minimums_.setval(MAXREAL); 00275 maximumsPtr_->setValue(maximums_, NOUPDATE); 00276 minimumsPtr_->setValue(minimums_, NOUPDATE); 00277 } 00278 00279 if (mode_ == "train") 00280 { 00281 // first pass calculate min/max limits 00282 for (t = 0; t < inSamples_; t++) 00283 for (o=0; o < inObservations_; o++) 00284 { 00285 if (in(o,t) > maximums_(t)) 00286 maximums_(t) = in(o,t); 00287 if (in(o,t) < minimums_(t)) 00288 minimums_(t) = in(o,t); 00289 out(o,t) = in(o,t); 00290 00291 } 00292 00293 setctrl(maximumsPtr_, maximums_); 00294 setctrl(minimumsPtr_, minimums_); 00295 } 00296 00297 00298 if ((prev_mode_ == "train")&&(mode_ == "predict")) 00299 { 00300 maximums_ = maximumsPtr_->to<mrs_realvec>(); 00301 minimums_ = minimumsPtr_->to<mrs_realvec>(); 00302 } 00303 00304 00305 if (mode_ == "predict") 00306 { 00307 // second pass for normalization 00308 for (t = 0; t < inSamples_-ignoreLast; t++) 00309 for (o=0; o < inObservations_; o++) 00310 { 00311 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(t)) / (maximums_(t) - minimums_(t))); 00312 } 00313 } 00314 00315 } 00316 00317 void 00318 NormMaxMin::do_slices(realvec& in, realvec& out) 00319 { 00320 00321 mrs_natural o,t; 00322 maximums_(0) = -MAXREAL; 00323 minimums_(0) = MAXREAL; 00324 00325 init_ = true; 00326 setctrl(initPtr_, init_); 00327 00328 lower_ = lowerPtr_->to<mrs_real>(); 00329 upper_ = upperPtr_->to<mrs_real>(); 00330 00331 mode_ = getctrl("mrs_string/mode")->to<mrs_string>(); 00332 mrs_natural ignoreLast = getctrl("mrs_natural/ignoreLast")->to<mrs_natural>(); 00333 00334 domain_ = getctrl("mrs_string/domain")->to<mrs_string>(); 00335 00336 range_ = upper_ - lower_; 00337 00338 if (mode_ == "twopass") { 00339 // first pass calculate min/max limits 00340 for (o=0; o < inObservations_; o++) 00341 for (t = 0; t < inSamples_; t++) 00342 { 00343 if (in(o,t) > maximums_(0)) 00344 maximums_(0) = in(o,t); 00345 if (in(o,t) < minimums_(0)) 00346 minimums_(0) = in(o,t); 00347 out(o,t) = in(o,t); 00348 } 00349 // second pass for normalization 00350 for (o=0; o < inObservations_-ignoreLast; o++) 00351 for (t = 0; t < inSamples_; t++) 00352 { 00353 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(0)) / (maximums_(0) - minimums_(0))); 00354 } 00355 } 00356 00357 if ((prev_mode_ == "predict") && (mode_ == "train")) 00358 { 00359 maximums_.setval(-MAXREAL); 00360 minimums_.setval(MAXREAL); 00361 maximumsPtr_->setValue(maximums_, NOUPDATE); 00362 minimumsPtr_->setValue(minimums_, NOUPDATE); 00363 } 00364 00365 if (mode_ == "train") 00366 { 00367 // first pass calculate min/max limits 00368 for (o=0; o < inObservations_; o++) 00369 for (t = 0; t < inSamples_; t++) 00370 { 00371 if (in(o,t) > maximums_(0)) 00372 maximums_(0) = in(o,t); 00373 if (in(o,t) < minimums_(0)) 00374 minimums_(0) = in(o,t); 00375 out(o,t) = in(o,t); 00376 00377 } 00378 00379 setctrl(maximumsPtr_, maximums_); 00380 setctrl(minimumsPtr_, minimums_); 00381 } 00382 00383 00384 if ((prev_mode_ == "train")&&(mode_ == "predict")) 00385 { 00386 maximums_ = maximumsPtr_->to<mrs_realvec>(); 00387 minimums_ = minimumsPtr_->to<mrs_realvec>(); 00388 } 00389 00390 00391 if (mode_ == "predict") 00392 { 00393 // second pass for normalization 00394 for (o=0; o < inObservations_-ignoreLast; o++) 00395 for (t = 0; t < inSamples_; t++) 00396 { 00397 out(o,t) = lower_ + range_ * ((in(o,t) - minimums_(0)) / (maximums_(0) - minimums_(0))); 00398 } 00399 } 00400 00401 }