00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "McAulayQuatieri.h"
00020 #include "peakView.h"
00021 #include "NumericLib.h"
00022
00023 using std::ostringstream;
00024 using std::abs;
00025
00026 using namespace Marsyas;
00027
00028 McAulayQuatieri::McAulayQuatieri(mrs_string name):MarSystem("McAulayQuatieri", name)
00029 {
00030 addControls();
00031 nextGroup_ = 0;
00032 }
00033
00034 McAulayQuatieri::McAulayQuatieri(const McAulayQuatieri& a) : MarSystem(a)
00035 {
00036 ctrl_reset_ = getctrl("mrs_bool/reset");
00037 ctrl_useGroups_ = getctrl("mrs_bool/useGroups");
00038 ctrl_useMemory_ = getctrl("mrs_bool/useMemory");
00039 ctrl_delta_ = getctrl("mrs_real/delta");
00040 ctrl_matchThres_ = getctrl("mrs_real/matchThres");
00041
00042 nextGroup_ = a.nextGroup_;
00043 }
00044
00045 McAulayQuatieri::~McAulayQuatieri()
00046 {
00047 }
00048
00049 MarSystem*
00050 McAulayQuatieri::clone() const
00051 {
00052 return new McAulayQuatieri(*this);
00053 }
00054
00055 void
00056 McAulayQuatieri::addControls()
00057 {
00058 addctrl("mrs_bool/reset", false);
00059 setctrlState("mrs_bool/reset", true);
00060
00061 addctrl("mrs_bool/useMemory", false);
00062
00063
00064 addctrl("mrs_bool/useGroups", false);
00065
00066
00067 addctrl("mrs_real/delta", 0.5);
00068 addctrl("mrs_real/matchThres", 0.5);
00069 }
00070
00071 void
00072 McAulayQuatieri::myUpdate(MarControlPtr sender)
00073 {
00074 MRSDIAG("McAulayQuatieri.cpp - McAulayQuatieri:myUpdate");
00075
00076 MarSystem::myUpdate(sender);
00077
00078 if(ctrl_reset_->to<mrs_bool>())
00079 {
00080 ctrl_reset_->setValue(false, NOUPDATE);
00081 memory_.stretch(0,0);
00082 nextGroup_ = 0;
00083 }
00084 }
00085
00086 mrs_real
00087 McAulayQuatieri::peakTrack(realvec& vec, mrs_natural frame, mrs_natural grpOne, mrs_natural grpTwo)
00088 {
00089 mrs_real dist;
00090 mrs_natural candidate;
00091 mrs_natural lastMatched = -1;
00092 mrs_natural matchedTracks = 0;
00093
00094 mrs_real delta = ctrl_delta_->to<mrs_real>();
00095
00096 if(frame+1 >= vec.getCols())
00097 {
00098 MRSERR("McAulayQuatieri::peakTrack - frame index is bigger than the input vector!");
00099 return -1.0;
00100 }
00101
00102 peakView tmpPeakView(vec);
00103
00104
00105 mrs_natural nextTrack = tmpPeakView.getFrameNumPeaks(0, grpOne);
00106
00107
00108 for(mrs_natural n = 0; n < tmpPeakView.getFrameNumPeaks(frame, grpOne); ++n)
00109 {
00110 mrs_real lastdist = MAXREAL;
00111 candidate = -1;
00112
00113
00114
00115 for(mrs_natural m = lastMatched + 1; m < tmpPeakView.getFrameNumPeaks(frame+1, grpTwo); ++m)
00116 {
00117
00118
00119 tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) = -1.0;
00120
00121 dist = abs(tmpPeakView(n, peakView::pkFrequency, frame, grpOne) - tmpPeakView(m, peakView::pkFrequency, frame+1, grpTwo));
00122 if (dist < delta && dist < lastdist)
00123 {
00124
00125 lastdist = dist;
00126 candidate = m;
00127 }
00128 }
00129
00130
00131
00132 if(candidate >= 0)
00133 {
00134
00135 if(n < tmpPeakView.getFrameNumPeaks(frame, grpOne)-1)
00136 {
00137
00138 dist = abs(tmpPeakView(n+1, peakView::pkFrequency, frame, grpOne) - tmpPeakView(candidate, peakView::pkFrequency, frame+1, grpTwo));
00139 if(dist < lastdist)
00140 {
00141
00142
00143
00144 if(candidate - 1 > lastMatched)
00145 {
00146 if(abs(tmpPeakView(n, peakView::pkFrequency, frame, grpOne) - tmpPeakView(candidate-1, peakView::pkFrequency, frame+1, grpTwo)) < delta)
00147 {
00148
00149 tmpPeakView(candidate-1, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne);
00150 lastMatched = candidate-1;
00151 matchedTracks++;
00152 }
00153 }
00154 }
00155 else
00156 {
00157
00158 tmpPeakView(candidate, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne);
00159 lastMatched = candidate;
00160 matchedTracks++;
00161 }
00162 }
00163 else
00164 {
00165
00166
00167 tmpPeakView(candidate, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne);
00168 lastMatched = candidate;
00169 matchedTracks++;
00170 }
00171 }
00172 }
00173
00174
00175
00176 for(mrs_natural m = 0; m < tmpPeakView.getFrameNumPeaks(frame+1, grpTwo); ++m)
00177 {
00178 if(tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) == -1.0)
00179 tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) = nextTrack++;
00180 }
00181
00182 return matchedTracks;
00183 }
00184
00185 void
00186 McAulayQuatieri::myProcess(realvec& in, realvec& out)
00187 {
00188 mrs_natural t,o,c;
00189 t=0;
00190 o=0;
00191 c=0;
00192
00193 realvec* outPtr;
00194
00195 out(o,t) = in(o,t);
00196
00197
00198
00199 if(ctrl_useMemory_->to<mrs_bool>() && memory_.getSize() != 0)
00200 {
00201
00202
00203 tmp_.stretch(onObservations_, onSamples_+1);
00204 for(o = 0; o < onObservations_; ++o)
00205 tmp_(o, 0) = memory_(o);
00206 for(o = 0; o < onObservations_; ++o)
00207 for(c = 0; c < onSamples_; ++c)
00208 tmp_(o,c+1) = in(o,c);
00209 outPtr = &tmp_;
00210
00211
00212
00213 if(ctrl_useGroups_->to<mrs_bool>())
00214 {
00215 peakView inPV(in);
00216 mrs_realvec inFirstFrame;
00217 in.getCol(0, inFirstFrame);
00218 peakView inFirstFramePV(inFirstFrame);
00219 peakView memPV(memory_);
00220 peakView tmpPV(tmp_);
00221
00222
00223 mrs_natural numInFirstFrameGroups = inFirstFramePV.getNumGroups();
00224 mrs_natural numMemGroups = memPV.getNumGroups();
00225
00226
00227
00228
00229 if(nextGroup_ > 0)
00230 for(mrs_natural f=1; f < tmpPV.getNumFrames(); ++f)
00231 for(mrs_natural p = 0; p < tmpPV.getFrameNumPeaks(f); ++p)
00232 tmpPV(p, peakView::pkGroup, f) = tmpPV(p, peakView::pkGroup, f) + nextGroup_;
00233
00234
00235
00236
00237
00238
00239
00240 realvec frames2Match(inObservations_, 2);
00241
00242
00243 realvec matchScores(numInFirstFrameGroups, numMemGroups);
00244 for(mrs_natural mg=0; mg < numMemGroups; ++mg)
00245 {
00246 for(mrs_natural ig = nextGroup_; ig < nextGroup_ + numInFirstFrameGroups; ++ig)
00247 {
00248
00249 for(o=0; o<inObservations_; ++o)
00250 for(c=0; c < 2; ++c)
00251 frames2Match(o, c) = tmp_(o, c);
00252
00253
00254
00255 matchScores(ig-nextGroup_, mg) = peakTrack(frames2Match, 0, ig, mg);
00256 }
00257 }
00258
00259
00260
00261
00262
00263 realvec assignedGrp(numInFirstFrameGroups);
00264
00265
00266 mrs_real maxScore = matchScores.maxval();
00267 for(o=0; o < matchScores.getRows(); ++o)
00268 for(c=0; c < matchScores.getCols(); ++ c)
00269 matchScores(o,c) = maxScore - matchScores(o,c);
00270
00271 NumericLib::hungarianAssignment(matchScores, assignedGrp);
00272
00273
00274
00275 mrs_natural ig;
00276 for(mrs_natural f=1; f < tmpPV.getNumFrames(); ++f)
00277 {
00278 for(mrs_natural p = 0; p < tmpPV.getFrameNumPeaks(f); ++p)
00279 {
00280
00281 ig = (mrs_natural)(tmpPV(p, peakView::pkGroup, f)) - nextGroup_;
00282
00283 if(assignedGrp(ig) > -1)
00284 {
00285
00286 if((maxScore - matchScores(ig, (mrs_natural)assignedGrp(ig))) / memPV.getFrameNumPeaks(0,(mrs_natural)assignedGrp(ig)) > ctrl_matchThres_->to<mrs_real>())
00287 {
00288
00289 tmpPV(p, peakView::pkGroup, f) = assignedGrp(ig);
00290 }
00291 else
00292 {
00293 tmpPV(p, peakView::pkGroup, f) = nextGroup_;
00294 assignedGrp(ig) = nextGroup_;
00295 nextGroup_++;
00296 }
00297 }
00298 else
00299 {
00300 tmpPV(p, peakView::pkGroup, f) = nextGroup_;
00301 assignedGrp(ig) = nextGroup_;
00302 nextGroup_++;
00303 }
00304 }
00305 }
00306 }
00307 }
00308 else
00309 {
00310
00311
00312 outPtr = &out;
00313 }
00314
00315 peakView tmpPeakView(*outPtr);
00316
00318 mrs_natural numGroups;
00319 mrs_natural g;
00320 if(ctrl_useGroups_->to<mrs_bool>())
00321 {
00322 numGroups = tmpPeakView.getNumGroups();
00323 g = 0;
00324 }
00325 else
00326 {
00327 numGroups = 0;
00328 g = -1;
00329 }
00330
00331 for(; g < numGroups; ++g)
00332 {
00333
00334
00335 if(!ctrl_useMemory_->to<mrs_bool>() || memory_.getSize() == 0)
00336 {
00337 for(mrs_natural n = 0; n < tmpPeakView.getFrameNumPeaks(0, g); ++n)
00338 tmpPeakView(n, peakView::pkTrack, 0) = (mrs_real) n;
00339 }
00340
00341
00342 for(mrs_natural f=0; f < tmpPeakView.getNumFrames()-1; ++f)
00343 peakTrack(*outPtr, f, g, g);
00344 }
00345
00346
00347 if(ctrl_useMemory_->to<mrs_bool>())
00348 {
00349 if(memory_.getSize() != 0)
00350 {
00351
00352
00353 peakView outPeakView(out);
00354 for(mrs_natural f=0; f < outPeakView.getNumFrames(); ++f)
00355 for(mrs_natural p = 0; p < outPeakView.getFrameNumPeaks(f); ++p)
00356 {
00357 outPeakView(p, peakView::pkTrack, f) = tmpPeakView(p, peakView::pkTrack, f+1);
00358 outPeakView(p, peakView::pkGroup, f) = tmpPeakView(p, peakView::pkGroup, f+1);
00359 }
00360 }
00361
00362
00363 memory_.stretch(onObservations_, 1);
00364 for(o = 0; o < onObservations_; ++o)
00365 memory_(o, 0) = out(o, onSamples_-1);
00366 }
00367 }