std::vector<int> Distance::topK(const TimeSeries &ts, int k) { const TimeSeriesCollection *db = this->collection; int ts_length = ts.length(); for (int i = 0; i < db->sequences.size(); i++) { ts_length = std::min(ts_length, (int)db->sequences[i].length()); } // Get distances std::vector<std::pair<int, float> > pairs; for (int i = 0; i < db->sequences.size(); i++) { float d = this->distance(ts, db->sequences[i], ts_length); pairs.push_back(std::make_pair(i, d)); } // Sort by value std::sort(pairs.begin(), pairs.end(), comp); // Return the indexes of top K. std::vector<int> ret; for (int i = 0; i < k; i++) { ret.push_back(pairs[i].first); } return ret; }
double Distance::DTW(const TimeSeries &ts1, const TimeSeries &ts2) { int len1 = ts1.length(); int len2 = ts2.length(); double *table_d = new double[len1 * len2]; double *table_g = new double[len1 * len2]; calcCost(ts1, ts2, table_d, table_g, len1, len2); calcGamma(table_d, table_g, len1, len2); double dist = calcSum(table_d, table_g, len1, len2); delete[] table_d; delete[] table_g; return dist; }
int Distance::match(const TimeSeries &ts) { const TimeSeriesCollection *db = this->collection; int ts_length = ts.length(); for (int i = 0; i < db->sequences.size(); i++) { ts_length = std::min(ts_length, (int)db->sequences[i].length()); } float distance_min = this->distance(ts, db->sequences[0], ts_length); float i_min = 0; for (int i = 1; i < db->sequences.size(); i++) { float d = this->distance(ts, db->sequences[i], ts_length); if (d < distance_min) { distance_min = d; i_min = i; } } std::cout << "matched : " << ts_length << std::endl; std::cout << "\t index: " << i_min << ", distance : " << distance_min << std::endl; return i_min; }