| 1 | /* CCs_to_DTW.c |
| 2 | * |
| 3 | * Dynamic Time Warp of two CCs. |
| 4 | * |
| 5 | * Copyright (C) 1993-2018 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | djmw 2001 |
| 23 | djmw 20020315 GPL header |
| 24 | djmw 20080122 float -> double |
| 25 | */ |
| 26 | |
| 27 | #include "CCs_to_DTW.h" |
| 28 | |
| 29 | static void regression (const VEC r, const CC me, const integer frameNumber, const integer numberOfCoefficients) { |
| 30 | |
| 31 | // sum(i^2;i=-n..n) = 2n^3/3 + n^2 +n/3 = n (n (2n/3 + 1) + 1/3); |
| 32 | |
| 33 | const integer numberOfCoefficientsd2 = numberOfCoefficients / 2; // numberOfCoefficients is always odd and > 2! |
| 34 | const double sumsq = numberOfCoefficientsd2 * (numberOfCoefficientsd2 * (numberOfCoefficients / 3.0 + 1.0) + 1.0 / 3.0); |
| 35 | |
| 36 | if (frameNumber <= numberOfCoefficientsd2 || frameNumber >= my nx - numberOfCoefficientsd2) |
| 37 | return; |
| 38 | |
| 39 | r <<= 0.0; |
| 40 | |
| 41 | const integer nmin = CC_getMinimumNumberOfCoefficients (me, frameNumber - numberOfCoefficientsd2, frameNumber + numberOfCoefficientsd2); |
| 42 | const integer nmax = CC_getMaximumNumberOfCoefficients (me, frameNumber - numberOfCoefficientsd2, frameNumber + numberOfCoefficientsd2); |
| 43 | |
| 44 | //TRACE |
| 45 | trace (U"frame number: ", frameNumber); |
| 46 | trace (U"numberOfCoefficients: ", numberOfCoefficients); |
| 47 | trace (U"numberOfCoefficientsd2: ", numberOfCoefficientsd2); |
| 48 | trace (U"number of frames: ", my frame.size); |
| 49 | trace (U"minimumNumberOfCoefficients: ", nmin); |
| 50 | trace (U"maximumNumberOfCoefficients: ", nmax); |
| 51 | trace (U"sumsq: ", sumsq); |
| 52 | for (integer i = 1; i <= nmin + 1; i ++) { |
| 53 | longdouble ri = 0.0; |
| 54 | for (integer j = -numberOfCoefficientsd2; j <= numberOfCoefficientsd2; j ++) { |
| 55 | const CC_Frame cf = & my frame [frameNumber + j]; |
| 56 | if (i <= cf -> c.size) { |
| 57 | const double c = ( i == 1 ? cf -> c0 : cf -> c [i] ); |
| 58 | ri += c * j; |
| 59 | } |
| 60 | } |
| 61 | r [i] = double (ri) / sumsq / my dx; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | autoDTW CCs_to_DTW (const CC me, const CC thee, |
| 66 | const double coefficientWeight, const double logEnergyWeight, |
| 67 | const double coefficientRegressionWeight, const double logEnergyRegressionWeight, |
| 68 | const double regressionWindowLength |
| 69 | ) { |
| 70 | try { |
| 71 | integer numberOfCoefficients = Melder_ifloor (regressionWindowLength / my dx); |
| 72 | |
| 73 | Melder_require (my maximumNumberOfCoefficients == thy maximumNumberOfCoefficients, |
| 74 | U"The maximum number of coefficients should be equal."); |
| 75 | Melder_require (! (coefficientRegressionWeight != 0.0 && numberOfCoefficients < 2), |
| 76 | U"Time window for regression is too small."); |
| 77 | |
| 78 | if (numberOfCoefficients % 2 == 0) |
| 79 | numberOfCoefficients ++; |
| 80 | |
| 81 | Melder_assert (numberOfCoefficients > 1 || (coefficientRegressionWeight == 0.0 && logEnergyRegressionWeight == 0.0)); |
| 82 | //TRACE |
| 83 | trace(1, U" regression window length ", regressionWindowLength, U" dx ", my dx, U" #coeff ", numberOfCoefficients); |
| 84 | autoDTW him = DTW_create (my xmin, my xmax, my nx, my dx, my x1, thy xmin, thy xmax, thy nx, thy dx, thy x1); |
| 85 | trace(2); |
| 86 | autoVEC ri = raw_VEC (my maximumNumberOfCoefficients + 1); |
| 87 | trace(3); |
| 88 | autoVEC rj = raw_VEC (my maximumNumberOfCoefficients + 1); |
| 89 | trace(4); |
| 90 | |
| 91 | /* |
| 92 | Calculate distance matrix. |
| 93 | */ |
| 94 | autoMelderProgress progress (U"CCs to DTW"); |
| 95 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 96 | trace (U"iframe ", iframe, U"/", my nx); |
| 97 | const CC_Frame fi = & my frame [iframe]; |
| 98 | trace(5); |
| 99 | if (coefficientRegressionWeight != 0.0 || logEnergyRegressionWeight != 0.0) |
| 100 | regression (ri.get(), me, iframe, numberOfCoefficients); |
| 101 | |
| 102 | trace(6); |
| 103 | for (integer jframe = 1; jframe <= thy nx; jframe ++) { |
| 104 | //trace (U"jframe ", jframe, U"/", thy nx); |
| 105 | const CC_Frame fj = & thy frame [jframe]; |
| 106 | longdouble dist = 0.0; |
| 107 | |
| 108 | if (coefficientWeight != 0.0) { |
| 109 | for (integer k = 1; k <= fj -> numberOfCoefficients; k ++) { |
| 110 | const double d = fi -> c [k] - fj -> c [k]; |
| 111 | dist += d * d; |
| 112 | } |
| 113 | dist *= coefficientWeight; |
| 114 | } |
| 115 | |
| 116 | if (logEnergyWeight != 0.0) { |
| 117 | const double d = fi -> c0 - fj -> c0; |
| 118 | dist += logEnergyWeight * d * d; |
| 119 | } |
| 120 | |
| 121 | if (coefficientRegressionWeight != 0.0) { |
| 122 | longdouble distr = 0.0; |
| 123 | regression (rj.get(), thee, jframe, numberOfCoefficients); |
| 124 | for (integer k = 2; k <= fj -> numberOfCoefficients + 1; k ++) { |
| 125 | const double d = ri [k] - rj [k]; |
| 126 | distr += d * d; |
| 127 | } |
| 128 | dist += coefficientRegressionWeight * distr; |
| 129 | } |
| 130 | |
| 131 | if (logEnergyRegressionWeight != 0.0) { |
| 132 | if (coefficientRegressionWeight == 0.0) |
| 133 | regression (rj.get(), thee, jframe, numberOfCoefficients); |
| 134 | const double d = ri [1] - rj [1]; |
| 135 | dist += logEnergyRegressionWeight * d * d; |
| 136 | } |
| 137 | |
| 138 | dist /= coefficientWeight + logEnergyWeight + coefficientRegressionWeight + logEnergyRegressionWeight; |
| 139 | his z [iframe] [jframe] = sqrt ((double) dist); // prototype along y-direction |
| 140 | } |
| 141 | |
| 142 | if (iframe % 10 == 1) |
| 143 | Melder_progress (0.999 * iframe / my nx, U"Calculate distances: frame ", iframe, U" from ", my nx, U"."); |
| 144 | } |
| 145 | return him; |
| 146 | } catch (MelderError) { |
| 147 | Melder_throw (U"DTW not created from CCs."); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* End of file CCs_to_DTW.cpp */ |