00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "MatchBassModel.h"
00022 #include "Metric2.h"
00023
00024
00025 using std::ostringstream;
00026 using std::cout;
00027 using std::endl;
00028
00029 using namespace Marsyas;
00030
00031 MatchBassModel::MatchBassModel(mrs_string name):MarSystem("MatchBassModel", name)
00032 {
00033 isComposite_ = true;
00034 addControls();
00035 }
00036
00037 MatchBassModel::MatchBassModel(const MatchBassModel& a):MarSystem(a)
00038 {
00039 ctrl_nTemplates_ = getctrl("mrs_natural/nTemplates");
00040 ctrl_nDevision_ = getctrl("mrs_natural/nDevision");
00041 ctrl_intervals_ = getctrl("mrs_realvec/intervals");
00042 ctrl_selections_ = getctrl("mrs_realvec/selections");
00043 ctrl_segmentation_ = getctrl("mrs_realvec/segmentation");
00044 ctrl_time_ = getctrl("mrs_realvec/time");
00045 ctrl_freq_ = getctrl("mrs_realvec/freq");
00046 ctrl_lowFreq_ = getctrl("mrs_real/lowFreq");
00047 ctrl_highFreq_ = getctrl("mrs_real/highFreq");
00048 ctrl_rootFreq_ = getctrl("mrs_real/rootFreq");
00049 ctrl_totalDistance_ = getctrl("mrs_real/totalDistance");
00050 ctrl_mode_ = getctrl("mrs_string/mode");
00051 ctrl_calcCovMatrix_ = getctrl("mrs_natural/calcCovMatrix");
00052 ctrl_covMatrix_ = getctrl("mrs_realvec/covMatrix");
00053 ctrl_templates_ = getctrl("mrs_realvec/templates");
00054 ctrl_stdDev_ = getctrl("mrs_real/stdDev");
00055 ctrl_normalize_ = getctrl("mrs_string/normalize");
00056 ctrl_distance_ = getctrl("mrs_realvec/distance");
00057 }
00058
00059 MatchBassModel::~MatchBassModel()
00060 {
00061 }
00062
00063 MarSystem*
00064 MatchBassModel::clone() const
00065 {
00066 return new MatchBassModel(*this);
00067 }
00068
00069 void
00070 MatchBassModel::addControls()
00071 {
00072 addControl("mrs_natural/nTemplates", 3, ctrl_nTemplates_);
00073 addControl("mrs_natural/nDevision", 64, ctrl_nDevision_);
00074 addControl("mrs_realvec/intervals", realvec(), ctrl_intervals_);
00075 addControl("mrs_realvec/selections", realvec(), ctrl_selections_);
00076 addControl("mrs_realvec/segmentation", realvec(), ctrl_segmentation_);
00077 addControl("mrs_realvec/time", realvec(), ctrl_time_);
00078 addControl("mrs_realvec/freq", realvec(), ctrl_freq_);
00079 addControl("mrs_real/lowFreq", 40.0, ctrl_lowFreq_);
00080 addControl("mrs_real/highFreq", 220.0, ctrl_highFreq_);
00081 addControl("mrs_real/rootFreq", 110.0, ctrl_rootFreq_);
00082 addControl("mrs_real/totalDistance", 0.0, ctrl_totalDistance_);
00083 addControl("mrs_string/mode", "estimate", ctrl_mode_);
00084 addControl("mrs_realvec/templates", realvec(), ctrl_templates_);
00085 addControl("mrs_natural/calcCovMatrix", 0, ctrl_calcCovMatrix_);
00086 addControl("mrs_realvec/covMatrix", realvec(), ctrl_covMatrix_);
00087 addControl("mrs_real/stdDev", 1.0, ctrl_stdDev_);
00088 addControl("mrs_string/normalize", "none", ctrl_normalize_);
00089 addControl("mrs_realvec/distance", realvec(), ctrl_distance_);
00090 }
00091
00092 void MatchBassModel::myUpdate(MarControlPtr sender)
00093 {
00094 (void) sender;
00095 mrs_natural i, j, o;
00096 ostringstream oss;
00097 realvec tmpvec;
00098 (void) sender;
00099
00100 K_ = ctrl_nTemplates_->to<mrs_natural>();
00101 I_ = ctrl_nDevision_->to<mrs_natural>();
00102 seg_ = ctrl_segmentation_->to<mrs_realvec>();
00103 time_ = ctrl_time_->to<mrs_realvec>();
00104 freq_ = ctrl_freq_->to<mrs_realvec>();
00105 lowFreq_ = ctrl_lowFreq_->to<mrs_real>();
00106 highFreq_ = ctrl_highFreq_->to<mrs_real>();
00107 rootFreq_ = ctrl_rootFreq_->to<mrs_real>();
00108 templates_ = ctrl_templates_->to<mrs_realvec>();
00109
00110
00111 ctrl_inSamples_->setValue(time_.getSize());
00112 ctrl_inObservations_->setValue(freq_.getSize());
00113 ctrl_onObservations_->setValue(inObservations_, NOUPDATE);
00114 ctrl_onSamples_->setValue(inSamples_, NOUPDATE);
00115 ctrl_osrate_->setValue(inSamples_, NOUPDATE);
00116 for(o=0; o<inObservations_; o++)
00117 oss << "MatchBassModel_" << o << ",";
00118 ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00119
00120
00121 logFreq_.create(freq_.getSize());
00122 for(i=0; i<logFreq_.getSize(); ++i){
00123 logFreq_(i) = log(lowFreq_)+(log(highFreq_)-log(lowFreq_))/(double)(logFreq_.getSize()-1)*(double)i;
00124 }
00125
00126 start_.create(seg_.getSize());
00127 i=0;
00128 j=0;
00129 while(i<inSamples_ && j<seg_.getSize()){
00130 if(seg_(j) <= time_(i)){
00131 start_(j) = i;
00132 j++;
00133 } else {
00134 ++i;
00135 }
00136 }
00137 if(j<seg_.getSize()){
00138 start_.stretch(j+1);
00139 start_(j) = i;
00140 }
00141 i=0;
00142 while(freq_(i) < rootFreq_ && i<inObservations_){
00143 ++i;
00144 }
00145 rootBin_ = i;
00146 i=0;
00147 while(freq_(i) < lowFreq_ && i<inObservations_){
00148 ++i;
00149 }
00150 rootMin_ = i;
00151 i=0;
00152 while(freq_(i) < highFreq_ && i<inObservations_){
00153 ++i;
00154 }
00155 rootMax_ = i;
00156
00157 invec_.create(rootMax_-rootMin_, inSamples_);
00158 d_.create(seg_.getSize()-1);
00159
00160
00161
00162 ctrl_intervals_->setValue(d_);
00163 k_.create(seg_.getSize()-1);
00164 costVector_.create(rootMax_-rootMin_,K_);
00165 distance_.create(K_,seg_.getSize()-1);
00166
00167
00168 if(marsystemsSize_ == 1 && inSamples_ > 0){
00169
00170
00171 i_featVec_.create(rootMax_-rootMin_, I_);
00172 j_featVec_.create((rootMax_-rootMin_)*2, I_*K_);
00173 stackedFeatVecs_.create((rootMax_-rootMin_)*2,1);
00174 marsystems_[0]->setctrl("mrs_natural/inObservations", stackedFeatVecs_.getRows());
00175 marsystems_[0]->setctrl("mrs_natural/inSamples", 1);
00176 marsystems_[0]->setctrl("mrs_real/israte", ctrl_israte_->to<mrs_real>());
00177 oss.clear();
00178 for(i=0; i<2; ++i){
00179 for(o=0; o<rootMax_-rootMin_; o++){
00180 oss << "MatchBassModel_" << o << ",";
00181 }
00182 }
00183 marsystems_[0]->setctrl("mrs_string/inObsNames", oss.str());
00184 marsystems_[0]->update();
00185
00186
00187 MarControlPtr ctrl_childCovMat = marsystems_[0]->getctrl("mrs_realvec/covMatrix");
00188 if(!ctrl_childCovMat.isInvalid())
00189 ctrl_childCovMat->linkTo(ctrl_covMatrix_);
00190 metricResult_.create(1,1);
00191 if(marsystems_[0]->getctrl("mrs_natural/onObservations") != 1 ||
00192 marsystems_[0]->getctrl("mrs_natural/onSamples") != 1){
00193 MRSWARN("MatchBassModel:myUpdate - invalid Child Metric MarSystem (does not output a real value)!");
00194 }
00195 } else if(marsystemsSize_ > 1){
00196 MRSWARN("MatchBassModel:myUpdate - more than one children MarSystem exist! Only one MarSystem should be added as a metric!");
00197 }
00198
00199 }
00200
00201 void
00202 MatchBassModel::myProcess(realvec& in, realvec& out)
00203 {
00204 mrs_natural i, j, k, l, m, d;
00205 mrs_real tmpreal, min;
00206 realvec covMatrix, tmpvec;
00207 if(inSamples_ > 0){
00208 if(marsystemsSize_ == 1){
00209
00210 for(i=0; i<inSamples_; ++i){
00211 for(j=0; j<inObservations_; j++){
00212 out(j,i) = in(j,i);
00213 }
00214 }
00215
00216
00217 for(i=0; i<I_*K_; ++i){
00218 for(j=0; j<(rootMax_-rootMin_)*2; j++){
00219 j_featVec_(j,i) = templates_(j,i);
00220 }
00221 }
00222 for(i=0; i<inSamples_; ++i){
00223 for(j=rootMin_; j<rootMax_; j++){
00224 invec_(j-rootMin_, i) = in(j,i);
00225 }
00226 }
00227 if(ctrl_normalize_->to<mrs_string>() == "MinMax"){
00228 invec_.normObsMinMax();
00229 j_featVec_.normObsMinMax();
00230 } else if(ctrl_normalize_->to<mrs_string>() == "MeanStd"){
00231 invec_.normObs();
00232 j_featVec_.normObs();
00233 }
00234 distance_.stretch(K_,seg_.getSize()-1);
00235
00236
00237 if(ctrl_calcCovMatrix_->to<mrs_natural>() & MatchBassModel::fixedStdDev){
00238 covMatrix.create(rootMax_-rootMin_, rootMax_-rootMin_);
00239 tmpreal = ctrl_stdDev_->to<mrs_real>();
00240 tmpreal *= tmpreal;
00241 for(i=0; i<rootMax_-rootMin_; ++i){
00242 covMatrix(i,i) = tmpreal;
00243 }
00244 } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & MatchBassModel::diagCovMatrix){
00245 covMatrix.create(rootMax_-rootMin_, rootMax_-rootMin_);
00246 invec_.varObs(tmpvec);
00247 for(i=0; i<rootMax_-rootMin_; ++i){
00248 covMatrix(i,i) = tmpvec(i);
00249 }
00250 } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & MatchBassModel::fullCovMatrix){
00251 covMatrix.create(rootMax_-rootMin_, rootMax_-rootMin_);
00252 invec_.covariance(covMatrix);
00253 } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & MatchBassModel::noCovMatrix){
00254 ctrl_covMatrix_->setValue(realvec());
00255 }
00256
00257 totaldis_ = 0.0;
00258 tmpvec.stretch(rootMax_-rootMin_, I_);
00259 for(j=0; j<(int)start_.getSize()-1; j++){
00260 tmpreal = 0.0;
00261 for(i=0; i<rootMax_-rootMin_; ++i){
00262 for(k=0; k<K_; k++){
00263 costVector_(i,k) = 0;
00264 }
00265 }
00266 for(i=0; i<I_; ++i){
00267 for(l=0; l<rootMax_-rootMin_; l++){
00268 i_featVec_(l,i) = 0;
00269 tmpvec(l,i) = 0;
00270 }
00271 }
00272 for(i=0; i<I_; ++i){
00273 for(m=(mrs_natural)(((double)i/I_*(start_(j+1)-start_(j)))+start_(j)); m<(int)((double)(i+1)/I_*(start_(j+1)-start_(j)))+start_(j); m++){
00274 for(l=0; l<rootMax_-rootMin_; l++){
00275 i_featVec_(l,i) += invec_(l, m);
00276 tmpvec(l,i) ++;
00277 }
00278 }
00279 }
00280 for(i=0; i<I_; ++i){
00281 for(l=0; l<rootMax_-rootMin_; l++){
00282 if(tmpvec(l,i) > 0){
00283 i_featVec_(l,i) /= tmpvec(l,i);
00284 }
00285 }
00286 }
00287 for(k=0; k<K_; k++){
00288 for(i=0; i<I_; ++i){
00289 for(l=0; l<rootMax_-rootMin_; l++){
00290 stackedFeatVecs_(l,0) = i_featVec_(l,i);
00291 }
00292 for(d=0; d<rootMax_-rootMin_; d++){
00293 for(l=0; l<rootMax_-rootMin_; l++){
00294 stackedFeatVecs_(l+rootMax_-rootMin_,0) = j_featVec_(l+rootMax_-rootMin_-d,k*I_+i);
00295 }
00296 marsystems_[0]->process(stackedFeatVecs_, metricResult_);
00297 costVector_(d, k) += metricResult_(0,0);
00298 }
00299 }
00300 }
00301 min = costVector_(0,0);
00302 d_(j) = 0;
00303 k_(j) = 0;
00304 for(d=0; d<rootMax_-rootMin_; d++){
00305 for(k=0; k<K_; k++){
00306 if(min>costVector_(d, k)){
00307 min = costVector_(d,k);
00308 d_(j) = d;
00309 k_(j) = k;
00310 }
00311 }
00312 }
00313 totaldis_ += min;
00314 for(k=0; k<K_; k++){
00315 min = costVector_(0,k);
00316 for(d=0; d<rootMax_-rootMin_; d++){
00317 if(min > costVector_(d,k)){
00318 min = costVector_(d,k);
00319 }
00320 }
00321 distance_(k,j) = min;
00322 }
00323 }
00324 ctrl_intervals_->setValue(d_);
00325 ctrl_selections_->setValue(k_);
00326 ctrl_totalDistance_->setValue(totaldis_);
00327 ctrl_distance_->setValue(distance_);
00328 } else {
00329 if(marsystemsSize_ == 0){
00330 MRSWARN("MatchBassModel::myProcess - no Child Metric MarSystem added");
00331 } else {
00332 MRSWARN("MatchBassModel::myProcess - more than on Child MarSystem exists (i.e. invalid metric)");
00333 }
00334 }
00335 } else {
00336 cout << "Not ready" << endl;
00337 }
00338 }