comparison align/DTW.h @ 755:b27815194752 pitch-align

Half-written rewrite of this part of SML code
author Chris Cannam
date Fri, 24 Apr 2020 17:24:56 +0100
parents
children f32df46d0c84
comparison
equal deleted inserted replaced
754:d0d47127d8cc 755:b27815194752
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_DTW_H
16 #define SV_DTW_H
17
18 #include <vector>
19
20 class DTW
21 {
22 public:
23 typedef double cost_t;
24
25 struct CostOption {
26 bool present;
27 cost_t cost;
28 };
29
30 enum class Direction {
31 None,
32 Up,
33 Down
34 };
35
36 struct Value {
37 Direction direction;
38 cost_t cost;
39 };
40
41 static cost_t choose(CostOption x, CostOption y, CostOption d) {
42 if (x.present && y.present) {
43 if (!d.present) {
44 throw std::logic_error("if x & y both exist, so must diagonal");
45 }
46 return std::min(std::min(x.cost, y.cost), d.cost);
47 } else if (x.present) {
48 return x.cost;
49 } else if (y.present) {
50 return y.cost;
51 } else {
52 return 0.0;
53 }
54 }
55
56 static cost_t calculateCost(Value a, Value b) {
57 auto together = [](cost_t c1, cost_t c2) {
58 auto diff = std::abs(c1 - c2);
59 return (diff < 1.0 ? -1.0 :
60 diff > 3.0 ? 1.0 :
61 0.0);
62 };
63 auto opposing = [](cost_t c1, cost_t c2) {
64 auto diff = c1 + c2;
65 return (diff < 2.0 ? 1.0 :
66 2.0);
67 };
68 if (a.direction == Direction::None || b.direction == Direction::None) {
69 if (a.direction == b.direction) {
70 return 0.0;
71 } else {
72 return 1.0;
73 }
74 } else {
75 if (a.direction == b.direction) {
76 return together (a.cost, b.cost);
77 } else {
78 return opposing (a.cost, b.cost);
79 }
80 }
81 }
82
83 static std::vector<std::vector<cost_t>> costSeries(std::vector<Value> s1,
84 std::vector<Value> s2) {
85
86 }
87
88 };
89
90 #endif