Chris@235
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@235
|
2
|
Chris@235
|
3 /*
|
Chris@235
|
4 Vamp feature extraction plugin using the MATCH audio alignment
|
Chris@235
|
5 algorithm.
|
Chris@235
|
6
|
Chris@235
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@235
|
8 Copyright (c) 2007-2020 Simon Dixon, Chris Cannam, and Queen Mary
|
Chris@235
|
9 University of London, Copyright (c) 2014-2015 Tido GmbH.
|
Chris@235
|
10
|
Chris@235
|
11 This program is free software; you can redistribute it and/or
|
Chris@235
|
12 modify it under the terms of the GNU General Public License as
|
Chris@235
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@235
|
14 License, or (at your option) any later version. See the file
|
Chris@235
|
15 COPYING included with this distribution for more information.
|
Chris@235
|
16 */
|
Chris@235
|
17
|
Chris@235
|
18 #ifndef FULL_DTW_H
|
Chris@235
|
19 #define FULL_DTW_H
|
Chris@235
|
20
|
Chris@235
|
21 #include <vector>
|
Chris@235
|
22 #include <functional>
|
Chris@235
|
23
|
Chris@235
|
24 #include "DistanceMetric.h"
|
Chris@235
|
25 #include "MatchTypes.h"
|
Chris@235
|
26
|
Chris@235
|
27 /** Represents a full-matrix dynamic time warping aligner. Unlike the
|
Chris@235
|
28 * combination of Matcher and Finder, which implement a "live" online
|
Chris@235
|
29 * DTW that stores only a part of the matrix, FullDTW works offline,
|
Chris@235
|
30 * requiring all features up-front and storing the whole cost
|
Chris@235
|
31 * matrix. This is far simpler than Matcher/Finder, can calculate
|
Chris@235
|
32 * subsequence matches, and always returns the optimal path, but it
|
Chris@235
|
33 * is also much more expensive - O(mn) rather than O(m+n) in both
|
Chris@235
|
34 * time and space.
|
Chris@235
|
35 */
|
Chris@235
|
36 class FullDTW
|
Chris@235
|
37 {
|
Chris@235
|
38 public:
|
Chris@235
|
39 struct Parameters {
|
Chris@235
|
40
|
Chris@235
|
41 Parameters(double hopTime_) :
|
Chris@235
|
42 hopTime(hopTime_),
|
Chris@235
|
43 diagonalWeight(1.0),
|
Chris@235
|
44 subsequence(false)
|
Chris@235
|
45 {}
|
Chris@235
|
46
|
Chris@235
|
47 /** Spacing of audio frames (determines the amount of overlap
|
Chris@235
|
48 * or skip between frames). This value is expressed in
|
Chris@235
|
49 * seconds.
|
Chris@235
|
50 */
|
Chris@235
|
51 double hopTime;
|
Chris@235
|
52
|
Chris@235
|
53 /** Weight applied to cost of diagonal step relative to
|
Chris@235
|
54 * horizontal or vertical step. The default of 1.0 is normal
|
Chris@235
|
55 * for cases that are not expected to differ wildly in speed.
|
Chris@235
|
56 */
|
Chris@235
|
57 double diagonalWeight;
|
Chris@235
|
58
|
Chris@235
|
59 /** Whether this is a subsequence matcher. The FullDTW aligns
|
Chris@235
|
60 * two sequences, and by default it assumes that they are
|
Chris@235
|
61 * anchored to one another at both ends (i.e. they both span
|
Chris@235
|
62 * the same material). If subsequence is true, then it
|
Chris@235
|
63 * instead assumes that the second sequence is intended to
|
Chris@235
|
64 * match some subsequence of the first, and it returns a
|
Chris@235
|
65 * match against the best-matching subsequence rather than
|
Chris@235
|
66 * the whole of the first sequence.
|
Chris@235
|
67 */
|
Chris@235
|
68 bool subsequence;
|
Chris@235
|
69 };
|
Chris@235
|
70
|
Chris@235
|
71 /** Constructor for FullDTW.
|
Chris@235
|
72 *
|
Chris@235
|
73 * A FullDTW expects to be provided with two sequences of feature
|
Chris@235
|
74 * vectors calculated by some external code (for example, a
|
Chris@235
|
75 * FeatureExtractor) which it will align.
|
Chris@235
|
76 */
|
Chris@235
|
77 FullDTW(Parameters params, DistanceMetric::Parameters dparams);
|
Chris@235
|
78
|
Chris@235
|
79 /**
|
Chris@235
|
80 * Align the sequence s2 against the sequence s1, returning the
|
Chris@235
|
81 * index into s1 for each element in s2. If the subsequence
|
Chris@235
|
82 * parameter was set on construction, then the alignment is
|
Chris@235
|
83 * against the best-matching subsequence of s1; otherwise it is
|
Chris@235
|
84 * against the whole of s1.
|
Chris@235
|
85 */
|
Chris@235
|
86 std::vector<size_t> align(const featureseq_t &s1,
|
Chris@235
|
87 const featureseq_t &s2);
|
Chris@235
|
88
|
Chris@235
|
89 private:
|
Chris@235
|
90 Parameters m_params;
|
Chris@235
|
91 DistanceMetric m_metric;
|
Chris@235
|
92
|
Chris@235
|
93 struct CostOption {
|
Chris@235
|
94 bool present;
|
Chris@235
|
95 pathcost_t cost;
|
Chris@235
|
96 };
|
Chris@235
|
97
|
Chris@235
|
98 pathcost_t choose(CostOption x, CostOption y, CostOption d);
|
Chris@235
|
99 pathcostmat_t costSequences(const featureseq_t &s1, const featureseq_t &s2);
|
Chris@235
|
100 };
|
Chris@235
|
101
|
Chris@235
|
102 #endif
|