-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTW.cpp
More file actions
119 lines (97 loc) · 2.76 KB
/
DTW.cpp
File metadata and controls
119 lines (97 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "DTW.h"
#include "Signal.h"
#include <iostream>
#include <cmath> // for the absolute function
#include <algorithm> // for the max function
#include <float.h>
#include <limits.h>
#include <climits>
DTW::DTW(Signal signalA, Signal signalB)
// Constructor takes in two Signal instances and initializes its own private Signal fields.
{
mySignalA = signalA;
mySignalB = signalB;
myCompared = false; // these Signals have not been compared
}
DTW::DTW(const DTW& copy)
// Copy constructor
{
this->myCompared = copy.myCompared;
this->myComparison = copy.myComparison;
this->setFirst(copy.getFirst());
this->setSecond(copy.getSecond());
}
vector<double> DTW::getComparison()
// Method returns a vector containing the comparison between the two signals.
{
if(myCompared == false)
compare();
return myComparison;
}
Signal DTW::getFirst() const
// Accessor returns SignalA
{
return mySignalA;
}
Signal DTW::getSecond() const
// Accessor returns SignalB
{
return mySignalB;
}
void DTW::setFirst(Signal signalA)
// Mutator modifies the first comparison signal
{
myCompared = false;
mySignalA = signalA;
}
void DTW::setSecond(Signal signalB)
// Mutator modifies the second comparison signal
{
myCompared = false;
mySignalB = signalB;
}
// ------------- Public Interface Ends -----------------------
void DTW::compare()
// Method compares SignalA to SignalB
{
if(getFirst() == getSecond())
{
//Do nothing else but:
myCompared = true;
myComparison = mySignalA.getSignalData();
}
else
{
initiateDTW();
}
}
void DTW::initiateDTW()
// method calls on the data forwarding algorithm
{
// we want to add a locality constraint
int windowSize = abs((int)(mySignalA.getSignalData().size() - mySignalB.getSignalData().size()));
DTWDistance(mySignalA.getSignalData(), mySignalB.getSignalData(), windowSize);
}
double DTW::DTWDistance(vector<double> first, vector<double> second, int windowSize)
// method performs the DTW and updates the myComparison vector
{
double myDTW[first.size()+1][second.size()+1];
int first_size = first.size();
int second_size = second.size();
windowSize = max(windowSize, abs(first_size - second_size));
// intializing all entries to the most "expensive" path possible
for(int i = 0; i < first_size; i++)
for(int j = 0; j < second_size; j++)
myDTW[i][j] = DBL_MAX;
myDTW[0][0] = 0;
myComparison.push_back(myDTW[0][0]);
double cost = 0;
for(int i = 1; i < first_size; i++)
for(int j = max(1, (i-windowSize) ); j < min(second_size, (i + windowSize)); j++)
{
cost = abs(first.at(i) - second.at(i));
myDTW[i][j] = cost + min(myDTW[i-1][j], min(myDTW[i][j-1], myDTW[i-1][j-1])); //where the minimum compares the inseration, deletion and match cases
myComparison.push_back(myDTW[i][j]);
}
return myDTW[first.size()][second.size()];
}