Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/DTW.cpp
Go to the documentation of this file.
00001 #include "DTW.h"
00002 
00003 using std::ostringstream;
00004 using namespace Marsyas;
00005 
00006 DTW::DTW(mrs_string name):MarSystem("DTW", name)
00007 {
00008   addControls();
00009 }
00010 
00011 DTW::DTW(const DTW& a):MarSystem(a)
00012 {
00013   ctrl_mode_ = getctrl("mrs_string/mode");
00014   ctrl_localPath_ = getctrl("mrs_string/localPath");
00015   ctrl_startPos_ = getctrl("mrs_string/startPos");
00016   ctrl_lastPos_ = getctrl("mrs_string/lastPos");
00017   ctrl_totalDis_ = getctrl("mrs_real/totalDistance");
00018   ctrl_sizes_ = getctrl("mrs_realvec/sizes");
00019   ctrl_weight_ = getctrl("mrs_bool/weight");
00020 }
00021 
00022 DTW::~DTW()
00023 {
00024 }
00025 
00026 MarSystem*
00027 DTW::clone() const
00028 {
00029   return new DTW(*this);
00030 }
00031 
00032 void
00033 DTW::addControls()
00034 {
00035   totalDis_ = 0;
00036   addControl("mrs_string/mode", "normal", ctrl_mode_);
00037   addControl("mrs_string/localPath", "normal", ctrl_localPath_);
00038   addControl("mrs_string/startPos", "zero", ctrl_startPos_);
00039   addControl("mrs_string/lastPos", "end", ctrl_lastPos_);
00040   addControl("mrs_real/totalDistance", totalDis_, ctrl_totalDis_);
00041   addControl("mrs_realvec/sizes", realvec(), ctrl_sizes_);
00042   addControl("mrs_bool/weight", false, ctrl_weight_);
00043 }
00044 
00045 void DTW::myUpdate(MarControlPtr sender)
00046 {
00047   (void) sender;  //suppress warning of unused parameter(s)
00048 
00049   ctrl_onSamples_->setValue(2,NOUPDATE);
00050   ctrl_onObservations_->setValue(ctrl_inSamples_+ctrl_inObservations_, NOUPDATE);
00051   ctrl_osrate_->setValue(ctrl_osrate_,NOUPDATE);
00052   ostringstream oss;
00053   for(mrs_natural o=0; o<ctrl_onObservations_->to<mrs_natural>(); ++o)
00054     oss << "DTW_" << o << ",";
00055   ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00056 
00057   MarControlAccessor acc(ctrl_sizes_);
00058   realvec& tmpvec = acc.to<mrs_realvec>();
00059   if(tmpvec.getRows() == 1 && tmpvec.getCols() >= 2)
00060   {
00061     sizes_.create(tmpvec.getCols());
00062     for(mrs_natural i=0; i<tmpvec.getCols(); ++i)
00063     {
00064       sizes_(i) = (mrs_natural)tmpvec(0,i);
00065     }
00066   }
00067   else if(tmpvec.getRows() >= 2 && tmpvec.getCols() == 1)
00068   {
00069     sizes_.create(tmpvec.getRows());
00070     for(mrs_natural i=0; i<tmpvec.getRows(); ++i)
00071     {
00072       sizes_(i) = (mrs_natural)tmpvec(i,0);
00073     }
00074   }
00075 
00076   alignment_.create(ctrl_inObservations_->to<mrs_natural>(), ctrl_inSamples_->to<mrs_natural>());
00077   if(ctrl_localPath_->to<mrs_string>() == "normal")
00078   {
00079     costMatrix_.create(ctrl_inObservations_->to<mrs_natural>(), 2);
00080     matrixPos_.create(2);
00081   }
00082   else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00083   {
00084     costMatrix_.create(ctrl_inObservations_->to<mrs_natural>(), 3);
00085     matrixPos_.create(3);
00086   }
00087   if(ctrl_mode_->to<mrs_string>() == "OnePass")
00088   {
00089     mrs_natural nTemplates = sizes_.getSize()-1;
00090     beginPos_.create(nTemplates);
00091     endPos_.create(nTemplates);
00092     beginPos_(0) = 0;
00093     for(mrs_natural l=1; l<nTemplates; l++)
00094     {
00095       beginPos_(l) = sizes_(l) + beginPos_(l-1);
00096     }
00097     for(mrs_natural l=0; l<nTemplates; l++)
00098     {
00099       endPos_(l) = beginPos_(l) + sizes_(l+1);
00100 
00101     }
00102   }
00103 }
00104 
00105 void
00106 DTW::myProcess(realvec& in, realvec& out)
00107 {
00108   mrs_natural i, j, k, l;
00109   j = 0;
00110 
00111   mrs_real nObs = in.getRows();
00112   mrs_real nSmp = in.getCols();
00113   mrs_real tmpReal = 0.0;
00114   mrs_bool weight = ctrl_weight_->to<mrs_bool>();
00115 
00116   if(inSamples_ > 0)
00117   {
00118     if(ctrl_mode_->to<mrs_string>() == "normal")
00119     {
00120       if(ctrl_localPath_->to<mrs_string>() == "normal" || ((nSmp > 2*nObs || nObs > 2*nSmp) && ctrl_localPath_->to<mrs_string>() == "diagonal"))
00121       {
00122         if((nSmp > 2*nObs || nObs > 2*nSmp) && ctrl_localPath_->to<mrs_string>() == "diagonal") {
00123           MRSWARN("DTW::myProcess - invalid local path control: diagonal (processes with normal local path)");
00124         }
00125 
00126         for(i=0; i<2; ++i)
00127         {
00128           matrixPos_(i) = i;
00129         }
00130         // |vertical:1, /diagonal:2, _horizonal:3
00131 
00132         if(ctrl_startPos_->to<mrs_string>() == "zero")
00133         {
00134           // copying first SimilarityMatrix
00135           costMatrix_(0,(mrs_natural)matrixPos_(0)) = in(0,0);
00136           alignment_(0,0) = 0;
00137           // calculating other cost of the first col
00138           for(j=1; j<nObs; j++)
00139           {
00140             costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0)+costMatrix_(j-1,(mrs_natural)matrixPos_(0));
00141             alignment_(j,0) = 1;
00142           }
00143         }
00144         else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00145         {
00146           // copying first col of SimilarityMatrix
00147           for(j=0; j<nObs; j++)
00148           {
00149             costMatrix_(j, (mrs_natural)matrixPos_(0)) = in(j,0);
00150             alignment_(j,0) = 0;
00151           }
00152         }
00153         // after first col
00154         for(i=1; i<nSmp; ++i)
00155         {
00156           costMatrix_(0,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(0,i);
00157           alignment_(0,i) = 3;
00158           for(j=1; j<nObs; j++)
00159           {
00160             costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00161             alignment_(j,i) = 1;
00162             tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i);
00163             if(weight)
00164               tmpReal += in(j,i);
00165             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00166             {
00167               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00168               alignment_(j,i) = 2;
00169             }
00170             tmpReal = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in(j,i);
00171             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00172             {
00173               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00174               alignment_(j,i) = 3;
00175             }
00176           }
00177           matrixPos_(0) = 1-matrixPos_(0);
00178           matrixPos_(1) = 1-matrixPos_(1);
00179         }
00180 
00181         // backtrace
00182         for(i=0; i<out.getRows(); ++i)
00183         {
00184           for(j=0; j<out.getCols(); j++)
00185           {
00186             out(i,j) = -1;
00187           }
00188         }
00189         if(ctrl_lastPos_->to<mrs_string>() == "end")
00190         {
00191           totalDis_ = costMatrix_((mrs_natural)(nObs-1),(mrs_natural)matrixPos_(0));
00192           ctrl_totalDis_->setValue(totalDis_);
00193           i = (mrs_natural)(nSmp-1);
00194           j = (mrs_natural)(nObs-1);
00195         }
00196         else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00197         {
00198           tmpReal = costMatrix_(0, (mrs_natural)matrixPos_(0));
00199           j = 0;
00200           for(i=1; i<nObs; ++i)
00201           {
00202             if(costMatrix_(i, (mrs_natural)matrixPos_(0)) < tmpReal)
00203             {
00204               tmpReal = costMatrix_(i, (mrs_natural)matrixPos_(0));
00205               j = i;
00206             }
00207           }
00208           i = (mrs_natural)(nSmp-1);
00209           totalDis_ = tmpReal;
00210           ctrl_totalDis_->setValue(totalDis_);
00211         }
00212         k = (mrs_natural)(nSmp + nObs - 1);
00213         while(alignment_(j,i) != 0 && k>=0)
00214         {
00215           if(alignment_(j,i) == 1)
00216           {
00217             out(k,0) = i;
00218             out(k,1) = j;
00219             j--;
00220             k--;
00221           }
00222           else if(alignment_(j,i) == 2)
00223           {
00224             out(k,0) = i;
00225             out(k,1) = j;
00226             k--;
00227             if(weight)
00228             {
00229               out(k,0) = i;
00230               out(k,1) = j;
00231               k--;
00232             }
00233             i--;
00234             j--;
00235           }
00236           else if(alignment_(j,i) == 3)
00237           {
00238             out(k,0) = i;
00239             out(k,1) = j;
00240             k--;
00241             i--;
00242           }
00243         }
00244         out(k,0) = i;
00245         out(k,1) = j;
00246       }
00247 
00248       else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00249       {
00250         for(i=0; i<3; ++i)
00251         {
00252           matrixPos_(i) = i;
00253         }
00254         // /|diagonal,vertical:1, /diagonal:2, /-diagonal,horizonal:3
00255 
00256         if(ctrl_startPos_->to<mrs_string>() == "zero")
00257         {
00258           // copying the first SimilarityMatrix
00259           costMatrix_(0,(mrs_natural)matrixPos_(0)) = in(0,0);
00260           alignment_(0,0) = 0;
00261           // calculating the second col
00262           costMatrix_(1,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1);
00263           if(weight)
00264             costMatrix_(1,(mrs_natural)matrixPos_(1)) += in(1,1);
00265           costMatrix_(2,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(2,1);
00266           if(weight)
00267             costMatrix_(2,(mrs_natural)matrixPos_(1)) += in(1,1);
00268           alignment_(1,1) = 2;
00269           alignment_(2,1) = 1;
00270           // calculating the third col
00271           costMatrix_(1,(mrs_natural)matrixPos_(2)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(1,2);
00272           if(weight)
00273             costMatrix_(1,(mrs_natural)matrixPos_(2)) += in(1,1);
00274           alignment_(1,2) = 3;
00275           costMatrix_(2,(mrs_natural)matrixPos_(2)) = costMatrix_(1,(mrs_natural)matrixPos_(1)) + in(2,2);
00276           if(weight)
00277             costMatrix_(2,(mrs_natural)matrixPos_(2)) += in(2,2);
00278           alignment_(2,2) = 2;
00279           costMatrix_(3,(mrs_natural)matrixPos_(2)) = costMatrix_(2,(mrs_natural)matrixPos_(1)) + in(3,2);
00280           if(weight)
00281             costMatrix_(3,(mrs_natural)matrixPos_(2)) += in(3,2);
00282           alignment_(3,2) = 2;
00283           tmpReal = costMatrix_(1,(mrs_natural)matrixPos_(1)) + in(2,2) + in(3,2);
00284           if(weight)
00285             tmpReal += in(2,2);
00286           if(tmpReal < costMatrix_(3,(mrs_natural)matrixPos_(2)))
00287           {
00288             costMatrix_(3,(mrs_natural)matrixPos_(2)) = tmpReal;
00289             alignment_(3,2) = 1;
00290           }
00291         }
00292         else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00293         {
00294           // copying first col of SimilarityMatrix
00295           for(j=0; j<nObs; j++)
00296           {
00297             costMatrix_(j, (mrs_natural)matrixPos_(0)) = in(j,0);
00298             alignment_(j,0) = 0;
00299           }
00300           // calculating the second col
00301           costMatrix_(1,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1);
00302           if(weight)
00303             costMatrix_(1,(mrs_natural)matrixPos_(1)) += in(1,1);
00304           alignment_(1,1) = 2;
00305           for(j=2; j<nObs; j++)
00306           {
00307             costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1);
00308             if(weight)
00309               costMatrix_(j,(mrs_natural)matrixPos_(1)) += in(j,1);
00310             alignment_(j,1) = 2;
00311             tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(0)) + in(j-1,1) + in(j,1);
00312             if(weight)
00313               tmpReal += in(j-1,1);
00314             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00315             {
00316               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00317               alignment_(j,1) = 1;
00318             }
00319           }
00320           // calculating the third col
00321           costMatrix_(1,(mrs_natural)matrixPos_(2)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(1,2);
00322           if(weight)
00323             costMatrix_(1,(mrs_natural)matrixPos_(2)) += in(1,1);
00324           alignment_(1,2) = 3;
00325           for(j=2; j<nObs; j++)
00326           {
00327             costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,2);
00328             if(weight)
00329               costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,2);
00330             alignment_(j,2) = 2;
00331             if(alignment_(j-2,2) != 0)
00332             {
00333               tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,2) + in(j,2);
00334               if(weight)
00335                 tmpReal += in(j-1,2);
00336               if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00337               {
00338                 costMatrix_(j,(mrs_natural)matrixPos_(2));
00339                 alignment_(j,2) = 1;
00340               }
00341             }
00342             tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1) + in(j,2);
00343             if(weight)
00344               tmpReal += in(j,1);
00345             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00346             {
00347               costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00348               alignment_(j,2) = 3;
00349             }
00350           }
00351         }
00352         for(i=0; i<3; ++i)
00353         {
00354           matrixPos_(i)++;
00355           if(matrixPos_(i)>=3)
00356             matrixPos_(i) = 0;
00357         }
00358         // after third col
00359         for(i=3; i<nSmp; ++i)
00360         {
00361           for(j=2; j<nObs; j++)
00362           {
00363             if(alignment_(j-1,i-2) != 0)
00364             {
00365               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i-1) + in(j,i);
00366               if(weight)
00367                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i-1);
00368               alignment_(j,i) = 3;
00369               if(alignment_(j-1,i-1) != 0)
00370               {
00371                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00372                 if(weight)
00373                   tmpReal += in(j,i);
00374                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00375                 {
00376                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00377                   alignment_(j,i) = 2;
00378                 }
00379               }
00380               if(alignment_(j-2,i-1) != 0)
00381               {
00382                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00383                 if(weight)
00384                   tmpReal += in(j-1,i);
00385                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00386                 {
00387                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00388                   alignment_(j,i) = 1;
00389                 }
00390               }
00391             }
00392             else if(alignment_(j-1,i-1) != 0)
00393             {
00394               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00395               if(weight)
00396                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i);
00397               alignment_(j,i) = 2;
00398               if(alignment_(j-2,i-1) != 0)
00399               {
00400                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00401                 if(weight)
00402                   tmpReal += in(j-1,i);
00403                 alignment_(j,i) = 1;
00404               }
00405             }
00406             else if(alignment_(j-2,i-1) != 0)
00407             {
00408               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00409               if(weight)
00410                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j-1,i);
00411               alignment_(j,i) = 1;
00412             }
00413           }
00414           for(j=0; j<3; j++)
00415           {
00416             matrixPos_(j)++;
00417             if(matrixPos_(j) >= 3)
00418               matrixPos_(j) = 0;
00419           }
00420         }
00421 
00422         // backtrace
00423         for(i=0; i<out.getRows(); ++i)
00424         {
00425           for(j=0; j<out.getCols(); j++)
00426           {
00427             out(i,j) = -1;
00428           }
00429         }
00430         if(ctrl_lastPos_->to<mrs_string>() == "end")
00431         {
00432           totalDis_ = costMatrix_((mrs_natural)(nObs-1),(mrs_natural)matrixPos_(1));
00433           ctrl_totalDis_->setValue(totalDis_);
00434           i = (mrs_natural)(nSmp-1);
00435           j = (mrs_natural)(nObs-1);
00436         }
00437         else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00438         {
00439           tmpReal = costMatrix_((mrs_natural)(nObs-1), (mrs_natural)matrixPos_(1));
00440           j = (mrs_natural)(nObs-1);
00441           for(i=0; i<nObs-1; ++i)
00442           {
00443             if(costMatrix_(i, (mrs_natural)matrixPos_(1)) < tmpReal && alignment_(i,(mrs_natural)(nSmp-1)) != 0)
00444             {
00445               tmpReal = costMatrix_(i, (mrs_natural)matrixPos_(1));
00446               j = i;
00447             }
00448           }
00449           i = (mrs_natural)(nSmp-1);
00450           totalDis_ = tmpReal;
00451           ctrl_totalDis_->setValue(totalDis_);
00452         }
00453         k = (mrs_natural)(nSmp + nObs - 1);
00454         while(alignment_(j,i) != 0 && k>=0)
00455         {
00456           if(alignment_(j,i) == 1)
00457           {
00458             out(k,0) = i;
00459             out(k,1) = j;
00460             j--;
00461             k--;
00462             out(k,0) = i;
00463             out(k,1) = j;
00464             k--;
00465             if(weight)
00466             {
00467               out(k,0) = i;
00468               out(k,1) = j;
00469               k--;
00470             }
00471             i--;
00472             j--;
00473           }
00474           else if(alignment_(j,i) == 2)
00475           {
00476             out(k,0) = i;
00477             out(k,1) = j;
00478             k--;
00479             if(weight)
00480             {
00481               out(k,0) = i;
00482               out(k,1) = j;
00483               k--;
00484             }
00485             i--;
00486             j--;
00487           }
00488           else if(alignment_(j,i) == 3)
00489           {
00490             out(k,0) = i;
00491             out(k,1) = j;
00492             k--;
00493             i--;
00494             out(k,0) = i;
00495             out(k,1) = j;
00496             k--;
00497             if(weight)
00498             {
00499               out(k,0) = i;
00500               out(k,1) = j;
00501               k--;
00502             }
00503             i--;
00504             j--;
00505           }
00506         }
00507         out(k,0) = i;
00508         out(k,1) = j;
00509       }
00510     }
00511 
00512     else if(ctrl_mode_->to<mrs_string>() == "OnePass")
00513     {
00514       mrs_natural nTemplates = sizes_.getSize()-1;
00515       if(sizes_.getSize() > 0)
00516       {
00517 
00518         if(ctrl_localPath_->to<mrs_string>() == "normal")
00519         {
00520           for(i=0; i<2; ++i)
00521           {
00522             matrixPos_(i) = i;
00523           }
00524           // |vertical:1, /diagonal:2, _horizonal:3
00525 
00526           if(ctrl_startPos_->to<mrs_string>() == "zero")
00527           {
00528             // copying first SimilarityMatrix
00529             for(l=0; l<nTemplates; l++)
00530             {
00531               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) = in((mrs_natural)beginPos_(l),0);
00532               alignment_((mrs_natural)beginPos_(l),0) = 0;
00533             }
00534             // calculating other cost of the first col
00535             for(l=0; l<nTemplates; l++)
00536             {
00537               for(j=(mrs_natural)beginPos_(l)+1; j<endPos_(l); j++)
00538               {
00539                 costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0) + costMatrix_(j-1,(mrs_natural)matrixPos_(0));
00540                 alignment_(j,0) = 1;
00541               }
00542             }
00543           }
00544           else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00545           {
00546             // copying first col of SimilarityMatrix
00547             for(j=0; j<nObs; j++)
00548             {
00549               costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0);
00550               alignment_(j,0) = 0;
00551             }
00552           }
00553           // after first col
00554           for(i=1; i<nSmp; ++i)
00555           {
00556             for(l=0; l<nTemplates; l++)
00557             {
00558               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),i);
00559               if(weight)
00560                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l),i);
00561               alignment_((mrs_natural)beginPos_(l),i) = -1*(endPos_(l)-1);
00562               tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),i);
00563               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)))
00564               {
00565                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = tmpReal;
00566                 alignment_((mrs_natural)beginPos_(l), i) = 3;
00567               }
00568               for(j=(mrs_natural)beginPos_(l)+1; j<(mrs_natural)endPos_(l); j++)
00569               {
00570                 costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00571                 alignment_(j,i) = 1;
00572                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i);
00573                 if(weight)
00574                   tmpReal += in(j,i);
00575                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00576                 {
00577                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00578                   alignment_(j,i) = 2;
00579                 }
00580                 tmpReal = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in(j,i);
00581                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00582                 {
00583                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00584                   alignment_(j,i) = 3;
00585                 }
00586               }
00587             }
00588             matrixPos_(0) = 1-matrixPos_(0);
00589             matrixPos_(1) = 1-matrixPos_(1);
00590           }
00591 
00592           // backtrace
00593           for(i=0; i<out.getRows(); ++i)
00594           {
00595             for(j=0; j<out.getCols(); j++)
00596             {
00597               out(i,j) = -1;
00598             }
00599           }
00600           if(ctrl_lastPos_->to<mrs_string>() == "end")
00601           {
00602             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(0));
00603             j = (mrs_natural)endPos_(0)-1;
00604             for(l=1; l<nTemplates; l++)
00605             {
00606               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) < tmpReal)
00607               {
00608                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0));
00609                 j = (mrs_natural)endPos_(l)-1;
00610               }
00611             }
00612             totalDis_ = tmpReal;
00613             ctrl_totalDis_->setValue(totalDis_);
00614             i = (mrs_natural)(nSmp-1);
00615           }
00616           else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00617           {
00618             tmpReal = costMatrix_(0, (mrs_natural)matrixPos_(0));
00619             j=0;
00620             for(i=1; i<nObs; ++i)
00621             {
00622               if(costMatrix_(i,(mrs_natural)matrixPos_(0)) < tmpReal)
00623               {
00624                 tmpReal = costMatrix_(i, (mrs_natural)matrixPos_(0));
00625                 j = i;
00626               }
00627             }
00628             i = (mrs_natural)nSmp-1;
00629             totalDis_ = tmpReal;
00630             ctrl_totalDis_->setValue(totalDis_);
00631           }
00632           k = (mrs_natural)(3*nSmp - 1);//+ nObs - 1;
00633           while(alignment_(j,i) != 0 && k>=0)
00634           {
00635             if(alignment_(j,i) == 1)
00636             {
00637               out(k,0) = i;
00638               out(k,1) = j;
00639               j--;
00640               k--;
00641             }
00642             else if(alignment_(j,i) == 2)
00643             {
00644               out(k,0) = i;
00645               out(k,1) = j;
00646               k--;
00647               if(weight)
00648               {
00649                 out(k,0) = i;
00650                 out(k,1) = j;
00651                 k--;
00652               }
00653               i--;
00654               j--;
00655             }
00656             else if(alignment_(j,i) == 3)
00657             {
00658               out(k,0) = i;
00659               out(k,1) = j;
00660               k--;
00661               i--;
00662             }
00663             else if(alignment_(j,i) < 0)
00664             {
00665               out(k,0) = i;
00666               out(k,1) = j;
00667               k--;
00668               if(weight)
00669               {
00670                 out(k,0) = i;
00671                 out(k,1) = j;
00672                 k--;
00673               }
00674               j = -1*(mrs_natural)alignment_(j,i);
00675               i--;
00676             }
00677           }
00678           out(k,0) = i;
00679           out(k,1) = j;
00680         }
00681         else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00682         {
00683           for(i=0; i<3; ++i)
00684           {
00685             matrixPos_(i) = i;
00686           }
00687           // /|diagonal,vertical:1, /diagonal:2, /-diagonal,holizonal:3
00688 
00689           if(ctrl_startPos_->to<mrs_string>() == "zero")
00690           {
00691             // copying first SimilarityMatrix
00692             for(l=0; l<nTemplates; l++)
00693             {
00694               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) = in((mrs_natural)beginPos_(l),0);
00695               alignment_((mrs_natural)beginPos_(l),0) = 0;
00696             }
00697             // calculating the second col
00698             for(l=0; l<nTemplates; l++)
00699             {
00700               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1);
00701               if(weight)
00702                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00703               costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+2,1);
00704               if(weight)
00705                 costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00706               alignment_((mrs_natural)beginPos_(l)+1,1) = 2;
00707               alignment_((mrs_natural)beginPos_(l)+2,1) = 1;
00708             }
00709             // calculating the third col
00710             for(l=0; l<nTemplates; l++)
00711             {
00712               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+1,2);
00713               if(weight)
00714                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,1);
00715               alignment_((mrs_natural)beginPos_(l)+1,2) = 3;
00716               costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+2,2);
00717               if(weight)
00718                 costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+2,2);
00719               alignment_((mrs_natural)beginPos_(l)+2,2) = 2;
00720               costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+3,2);
00721               if(weight)
00722                 costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+3,2);
00723               alignment_((mrs_natural)beginPos_(l)+3,2) = 2;
00724               tmpReal = costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+2,2) + in((mrs_natural)beginPos_(l)+3,2);
00725               if(weight)
00726                 tmpReal += in((mrs_natural)beginPos_(l)+2,2);
00727               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)))
00728               {
00729                 costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) = tmpReal;
00730                 alignment_((mrs_natural)beginPos_(l)+3,2) = 1;
00731               }
00732             }
00733           }
00734           else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00735           {
00736             // copying first col of SimilarityMatrix
00737             for(j=0; j<nObs; j++)
00738             {
00739               costMatrix_(j, (mrs_natural)matrixPos_(0)) = in(j,0);
00740               alignment_(j,0) = 0;
00741             }
00742             // calculating the second col
00743             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(0));
00744             j=(mrs_natural)endPos_(0)-1;
00745             for(l=1; l<nTemplates; l++)
00746             {
00747               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) < tmpReal)
00748               {
00749                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0));
00750                 j=(mrs_natural)endPos_(l)-1;
00751               }
00752             }
00753             for(l=0; l<nTemplates; l++)
00754             {
00755               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),1);
00756               if(weight)
00757                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l),1);
00758               alignment_((mrs_natural)beginPos_(l),1) = -1*j;
00759             }
00760             for(l=0; l<nTemplates; l++)
00761             {
00762               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1);
00763               if(weight)
00764                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00765               alignment_((mrs_natural)beginPos_(l)+1,1) = 2;
00766               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00767               {
00768                 costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1);
00769                 if(weight)
00770                   costMatrix_(j,(mrs_natural)matrixPos_(1)) += in(j,1);
00771                 alignment_(j,1) = 2;
00772                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(0)) + in(j-1,1) + in(j,1);
00773                 if(weight)
00774                   tmpReal += in(j-1,1);
00775                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00776                 {
00777                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00778                   alignment_(j,1) = 1;
00779                 }
00780               }
00781             }
00782             // calculating the third col
00783             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(1));
00784             j=(mrs_natural)endPos_(0)-1;
00785             for(l=1; l<nTemplates; l++)
00786             {
00787               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1)) < tmpReal)
00788               {
00789                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1, (mrs_natural)matrixPos_(1));
00790                 j=(mrs_natural)endPos_(l)-1;
00791               }
00792             }
00793             for(l=0; l<nTemplates; l++)
00794             {
00795               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) = costMatrix_(j,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l),2);
00796               if(weight)
00797                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l),2);
00798               alignment_((mrs_natural)beginPos_(l),2) = -1*j;
00799             }
00800             //tmpReal = costMatrix_(endPos_(0)-1,matrixPos_(1));
00801             //j=endPos_(0)-1;
00802             //for(l=1; l<nTemplates; l++)
00803             //{
00804             //  if(costMatrix_(endPos_(l)-1,matrixPos_(1)) < tmpReal)
00805             //    {
00806             //      tmpReal = costMatrix_(endPos_(l)-1, matrixPos_(1));
00807             //      j=endPos_(l)-1;
00808             //    }
00809             //}
00810             //for(l=0; l<nTemplates; l++)
00811             //{
00812             //  tmpReal = costMatrix_(j,matrixPos_(1)) + 2.0*in(beginPos_(l),matrixPos_(2));
00813             //  if(tmpReal < costMatrix_(beginPos_(l),matrixPos_(2)))
00814             //    {
00815             //      costMatrix_(beginPos_(l),matrixPos_(2)) = tmpReal;
00816             //      alignment_(beginPos_(l),2) = -1*j;
00817             //    }
00818             //}
00819             for(l=0; l<nTemplates; l++)
00820             {
00821               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+1,2);
00822               if(weight)
00823                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,2);
00824               alignment_((mrs_natural)beginPos_(l)+1,2) = 2;
00825               tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+1,2);
00826               if(weight)
00827                 tmpReal += in((mrs_natural)beginPos_(l)+1,1);
00828               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)))
00829               {
00830                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = tmpReal;
00831                 alignment_((mrs_natural)beginPos_(l)+1,2) = 3;
00832               }
00833               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00834               {
00835                 costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1) + in(j,2);
00836                 if(weight)
00837                   costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,1);
00838                 alignment_(j,2) = 3;
00839                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,2);
00840                 if(weight)
00841                   tmpReal += in(j,2);
00842                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00843                 {
00844                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00845                   alignment_(j,2) = 2;
00846                 }
00847                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,2) + in(j,2);
00848                 if(weight)
00849                   tmpReal += in(j-1,2);
00850                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00851                 {
00852                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00853                   alignment_(j,2) = 1;
00854                 }
00855               }
00856             }
00857           }
00858           for(i=0; i<3; ++i)
00859           {
00860             matrixPos_(i)++;
00861             if(matrixPos_(i)>=3)
00862               matrixPos_(i) = 0;
00863           }
00864           // after third col
00865           for(i=3; i<nSmp; ++i)
00866           {
00867             j = -1;
00868             for(l=0; l<nTemplates; l++)
00869             {
00870               if(alignment_((mrs_natural)endPos_(l)-1,i-1) != 0)
00871               {
00872                 if(j<0 || (j>=0&&costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1))<tmpReal))
00873                 {
00874                   tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1));
00875                   j = (mrs_natural)endPos_(l)-1;
00876                 }
00877               }
00878             }
00879             if(j>=0)
00880             {
00881               for(l=0; l<nTemplates; l++)
00882               {
00883                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) = costMatrix_(j,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l),i);
00884                 if(weight)
00885                   costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l),i);
00886                 alignment_((mrs_natural)beginPos_(l),i) = -1*j;
00887               }
00888               //j = -1;
00889               //for(l=0; l<nTemplates; l++)
00890               //  {
00891               //    if(alignment_(endPos_(l)-1,i-2) != 0)
00892               //    {
00893               //if(j<0 || (j>=0&&costMatrix_(endPos_(l)-1,matrixPos_(0))<tmpReal))
00894               //    {
00895               //      tmpReal = costMatrix_(endPos_(l)-1,matrixPos_(0));
00896               //      j = endPos_(l)-1;
00897               //    }
00898               //}
00899               //}
00900               //if(j>=0)
00901               //  {
00902               //    for(l=0; l<nTemplates; l++)
00903               //    {
00904               //      tmpReal = costMatrix_(j,matrixPos_(0)) + 2.0*in(beginPos_(l),i-1) + in(beginPos_(l),i);
00905               //      if(tmpReal < costMatrix_(beginPos_(l),matrixPos_(2)))
00906               //        {
00907               //          costMatrix_(beginPos_(l),matrixPos_(2)) = tmpReal;
00908               //          alignment_(beginPos_(l),i) = -1*j;
00909               //        }
00910               //    }
00911               //}
00912             }/*
00913               else
00914             {
00915               j = -1;
00916               for(l=0; l<nTemplates; l++)
00917                 {
00918                   if(alignment_(endPos_(l)-1,i-2) != 0)
00919                 {
00920                   if(j<0 || (j>=0&&costMatrix_(endPos_(l)-1,matrixPos_(0))<tmpReal))
00921                     {
00922                       tmpReal = costMatrix_(endPos_(l)-1,matrixPos_(0));
00923                       j = endPos_(l)-1;
00924                     }
00925                 }
00926                 }
00927               if(j>=0)
00928                 {
00929                   for(l=0; l<nTemplates; l++)
00930                 {
00931                   costMatrix_(beginPos_(l),matrixPos_(2)) = costMatrix_(j,matrixPos_(0)) + 2.0*in(beginPos_(l),i-1) + in(beginPos_(l),i);
00932                   alignment_(beginPos_(l),i) = -1*j;
00933                 }
00934                 }
00935                 }*/
00936             for(l=0; l<nTemplates; l++)
00937             {
00938               if(alignment_((mrs_natural)beginPos_(l),i-1) != 0)
00939               {
00940                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+1,i);
00941                 if(weight)
00942                   costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,i);
00943                 alignment_((mrs_natural)beginPos_(l)+1,i) = 2;
00944                 if(alignment_((mrs_natural)beginPos_(l),i-2) != 0)
00945                 {
00946                   tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,i-1) + in((mrs_natural)beginPos_(l)+1,i);
00947                   if(weight)
00948                     tmpReal += in((mrs_natural)beginPos_(l)+1,i-1);
00949                   if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)))
00950                   {
00951                     costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = tmpReal;
00952                     alignment_((mrs_natural)beginPos_(l)+1,i) = 3;
00953                   }
00954                 }
00955               }
00956               else if(alignment_((mrs_natural)beginPos_(l),i-2) != 0)
00957               {
00958                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,i-1) + in((mrs_natural)beginPos_(l)+1,i);
00959                 if(weight)
00960                   costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,i-1);
00961                 alignment_((mrs_natural)beginPos_(l)+1,i) = 3;
00962               }
00963               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00964               {
00965                 if(alignment_(j-1,i-2) != 0)
00966                 {
00967                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i-1) + in(j,i);
00968                   if(weight)
00969                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i-1);
00970                   alignment_(j,i) = 3;
00971                   if(alignment_(j-1,i-1) != 0)
00972                   {
00973                     tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00974                     if(weight)
00975                       tmpReal += in(j,i);
00976                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00977                     {
00978                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00979                       alignment_(j,i) = 2;
00980                     }
00981                   }
00982                   if(alignment_(j-2,i-1) != 0)
00983                   {
00984                     tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00985                     if(weight)
00986                       tmpReal += in(j-1,i);
00987                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00988                     {
00989                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00990                       alignment_(j,i) = 1;
00991                     }
00992                   }
00993                 }
00994                 else if(alignment_(j-1,i-1) != 0)
00995                 {
00996                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00997                   if(weight)
00998                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i);
00999                   alignment_(j,i) = 2;
01000                   if(alignment_(j-2,i-1) != 0)
01001                   {
01002                     tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
01003                     if(weight)
01004                       tmpReal += in(j-1,i);
01005                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
01006                     {
01007                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
01008                       alignment_(j,i) = 1;
01009                     }
01010 
01011                   }
01012                 }
01013                 else if(alignment_(j-2,i-1) != 0)
01014                 {
01015                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
01016                   if(weight)
01017                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j-1,i);
01018                   alignment_(j,i) = 1;
01019                 }
01020               }
01021             }
01022             for(j=0; j<3; j++)
01023             {
01024               matrixPos_(j)++;
01025               if(matrixPos_(j) >= 3)
01026                 matrixPos_(j) = 0;
01027             }
01028           }
01029 
01030           // backtrace
01031           for(i=0; i<out.getRows(); ++i)
01032           {
01033             for(j=0; j<out.getCols(); j++)
01034             {
01035               out(i,j) = -1;
01036             }
01037           }
01038           if(ctrl_lastPos_->to<mrs_string>() == "end")
01039           {
01040             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(1));
01041             j = (mrs_natural)endPos_(0)-1;
01042             for(l=1; l<nTemplates; l++)
01043             {
01044               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1)) < tmpReal)
01045               {
01046                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1));
01047                 j = (mrs_natural)endPos_(l)-1;
01048               }
01049             }
01050             totalDis_ = tmpReal;
01051             ctrl_totalDis_->setValue(totalDis_);
01052             i = (mrs_natural)nSmp-1;
01053           }
01054           else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
01055           {
01056             tmpReal = costMatrix_(0,(mrs_natural)matrixPos_(1));
01057             j=0;
01058             for(i=1; i<nObs; ++i)
01059             {
01060               if(costMatrix_(i,(mrs_natural)matrixPos_(1)) < tmpReal)
01061               {
01062                 tmpReal = costMatrix_(i,(mrs_natural)matrixPos_(1));
01063                 j = i;
01064               }
01065             }
01066             i = (mrs_natural)nSmp-1;
01067             totalDis_ = tmpReal;
01068             ctrl_totalDis_->setValue(totalDis_);
01069           }
01070           k = (mrs_natural)(3*nSmp -1);// + nObs - 1;
01071           while(alignment_(j,i) != 0 && k>=0)
01072           {
01073             if(alignment_(j,i) == 1)
01074             {
01075               out(k,0) = i;
01076               out(k,1) = j;
01077               j--;
01078               k--;
01079               out(k,0) = i;
01080               out(k,1) = j;
01081               k--;
01082               if(weight)
01083               {
01084                 out(k,0) = i;
01085                 out(k,1) = j;
01086                 k--;
01087               }
01088               j--;
01089               i--;
01090             }
01091             else if(alignment_(j,i) == 2)
01092             {
01093               out(k,0) = i;
01094               out(k,1) = j;
01095               k--;
01096               if(weight)
01097               {
01098                 out(k,0) = i;
01099                 out(k,1) = j;
01100                 k--;
01101               }
01102               i--;
01103               j--;
01104             }
01105             else if(alignment_(j,i) == 3)
01106             {
01107               out(k,0) = i;
01108               out(k,1) = j;
01109               k--;
01110               i--;
01111               out(k,0) = i;
01112               out(k,1) = j;
01113               k--;
01114               if(weight)
01115               {
01116                 out(k,0) = i;
01117                 out(k,1) = j;
01118                 k--;
01119               }
01120               i--;
01121               j--;
01122             }
01123             else if(alignment_(j,i) < 0)
01124             {
01125               out(k,0) = i;
01126               out(k,1) = j;
01127               k--;
01128               if(weight)
01129               {
01130                 out(k,0) = i;
01131                 out(k,1) = j;
01132                 k--;
01133               }
01134               j = (mrs_natural)(-1*alignment_(j,i));
01135               i--;
01136             }
01137           }
01138           out(k,0) = i;
01139           out(k,1) = j;
01140         }
01141       }
01142       else
01143       {
01144         MRSWARN("DTW::myProcess - invalid sizes vector (does not output a real value)!");
01145       }
01146     }
01147   }
01148 }
01149 
01150 
01151 
01152 
01153