Cepstrumc.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Cepstrumc.c
  2. *
  3. * Copyright (C) 1994-2017 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20020812 GPL header
  20. djmw 20061218 Changed info to Melder_writeLine<x> format.
  21. djmw 20071017 oo_CAN_WRITE_AS_ENCODING.h
  22. djmw 20080122 Version 1: float -> double
  23. djmw 20110304 Thing_new
  24. */
  25. #include "Cepstrumc.h"
  26. #include "DTW.h"
  27. #include "oo_DESTROY.h"
  28. #include "Cepstrumc_def.h"
  29. #include "oo_COPY.h"
  30. #include "Cepstrumc_def.h"
  31. #include "oo_EQUAL.h"
  32. #include "Cepstrumc_def.h"
  33. #include "oo_CAN_WRITE_AS_ENCODING.h"
  34. #include "Cepstrumc_def.h"
  35. #include "oo_WRITE_TEXT.h"
  36. #include "Cepstrumc_def.h"
  37. #include "oo_WRITE_BINARY.h"
  38. #include "Cepstrumc_def.h"
  39. #include "oo_READ_TEXT.h"
  40. #include "Cepstrumc_def.h"
  41. #include "oo_READ_BINARY.h"
  42. #include "Cepstrumc_def.h"
  43. #include "oo_DESCRIPTION.h"
  44. #include "Cepstrumc_def.h"
  45. Thing_implement (Cepstrumc, Sampled, 1);
  46. void structCepstrumc :: v_info () {
  47. structDaata :: v_info ();
  48. MelderInfo_writeLine (U" Start time: ", xmin);
  49. MelderInfo_writeLine (U" End time: ", xmax);
  50. MelderInfo_writeLine (U" Number of frames: ", nx);
  51. MelderInfo_writeLine (U" Time step: ", dx);
  52. MelderInfo_writeLine (U" First frame at: ", x1);
  53. MelderInfo_writeLine (U" Number of coefficients: ", maxnCoefficients);
  54. }
  55. void Cepstrumc_Frame_init (Cepstrumc_Frame me, int nCoefficients) {
  56. my c = NUMvector<double> (0, nCoefficients);
  57. my nCoefficients = nCoefficients;
  58. }
  59. void Cepstrumc_init (Cepstrumc me, double tmin, double tmax, integer nt, double dt, double t1,
  60. int nCoefficients, double samplingFrequency) {
  61. my samplingFrequency = samplingFrequency;
  62. my maxnCoefficients = nCoefficients;
  63. Sampled_init (me, tmin, tmax, nt, dt, t1);
  64. my frame = NUMvector<structCepstrumc_Frame> (1, nt);
  65. }
  66. autoCepstrumc Cepstrumc_create (double tmin, double tmax, integer nt, double dt, double t1,
  67. int nCoefficients, double samplingFrequency) {
  68. try {
  69. autoCepstrumc me = Thing_new (Cepstrumc);
  70. Cepstrumc_init (me.get(), tmin, tmax, nt, dt, t1, nCoefficients, samplingFrequency);
  71. return me;
  72. } catch (MelderError) {
  73. Melder_throw (U"Cepstrum not created.");
  74. }
  75. }
  76. static void regression (Cepstrumc me, integer frame, double r [], integer nr) {
  77. integer nc = 1e6; double sumsq = 0;
  78. for (integer i = 0; i <= my maxnCoefficients; i ++) {
  79. r [i] = 0;
  80. }
  81. if (frame <= nr / 2 || frame >= my nx - nr / 2) {
  82. return;
  83. }
  84. for (integer j = -nr / 2; j <= nr / 2; j ++) {
  85. Cepstrumc_Frame f = & my frame [frame + j];
  86. if (f->nCoefficients < nc) {
  87. nc = f->nCoefficients;
  88. }
  89. sumsq += j * j;
  90. }
  91. for (integer i = 0; i <= nc; i ++) {
  92. for (integer j = -nr / 2; j <= nr / 2; j ++) {
  93. Cepstrumc_Frame f = & my frame [frame + j];
  94. r [i] += f->c [i] * j / sumsq / my dx;
  95. }
  96. }
  97. }
  98. autoDTW Cepstrumc_to_DTW (Cepstrumc me, Cepstrumc thee, double wc, double wle, double wr, double wer, double dtr, int matchStart, int matchEnd, int constraint) {
  99. try {
  100. integer nr = Melder_ifloor (dtr / my dx);
  101. if (my maxnCoefficients != thy maxnCoefficients) {
  102. Melder_throw (U"Cepstrumc orders should be equal.");
  103. }
  104. if (wr != 0 && nr < 2) {
  105. Melder_throw (U"Time window for regression coefficients too small.");
  106. }
  107. if (nr % 2 == 0) {
  108. nr ++;
  109. }
  110. if (wr != 0) {
  111. Melder_casual (U"Number of frames used for regression coefficients ", nr);
  112. }
  113. autoDTW him = DTW_create (my xmin, my xmax, my nx, my dx, my x1, thy xmin, thy xmax, thy nx, thy dx, thy x1);
  114. autoNUMvector <double> ri ((integer) 0, my maxnCoefficients);
  115. autoNUMvector <double> rj ((integer) 0, my maxnCoefficients);
  116. // Calculate distance matrix
  117. autoMelderProgress progress (U"");
  118. for (integer i = 1; i <= my nx; i ++) {
  119. Cepstrumc_Frame fi = & my frame [i];
  120. regression (me, i, ri.peek(), nr);
  121. for (integer j = 1; j <= thy nx; j ++) {
  122. Cepstrumc_Frame fj = & thy frame [j];
  123. longdouble d, dist = 0.0, distr = 0.0;
  124. if (wc != 0) { /* cepstral distance */
  125. for (integer k = 1; k <= fj -> nCoefficients; k ++) {
  126. d = fi -> c [k] - fj -> c [k];
  127. dist += d * d;
  128. }
  129. dist *= wc;
  130. }
  131. // log energy distance
  132. d = fi -> c [0] - fj -> c [0];
  133. dist += wle * d * d;
  134. if (wr != 0) { // regression distance
  135. regression (thee, j, rj.peek(), nr);
  136. for (integer k = 1; k <= fj -> nCoefficients; k ++) {
  137. d = ri [k] - rj [k];
  138. distr += d * d;
  139. }
  140. dist += wr * distr;
  141. }
  142. if (wer != 0) { // regression on c [0]: log(energy)
  143. if (wr == 0) {
  144. regression (thee, j, rj.peek(), nr);
  145. }
  146. d = ri [0] - rj [0];
  147. dist += wer * d * d;
  148. }
  149. dist /= wc + wle + wr + wer;
  150. his z [i] [j] = sqrt ((double) dist); // prototype along y-direction
  151. }
  152. Melder_progress ( (double) i / my nx, U"Calculate distances: frame ",
  153. i, U" from ", my nx, U".");
  154. }
  155. DTW_findPath (him.get(), matchStart, matchEnd, constraint);
  156. return him;
  157. } catch (MelderError) {
  158. Melder_throw (U"DTW not created.");
  159. }
  160. }
  161. autoMatrix Cepstrumc_to_Matrix (Cepstrumc me) {
  162. try {
  163. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1,
  164. 0, my maxnCoefficients, my maxnCoefficients + 1, 1, 0);
  165. for (integer i = 1; i <= my nx; i ++) {
  166. Cepstrumc_Frame him = & my frame [i];
  167. for (integer j = 1; j <= his nCoefficients + 1; j ++) {
  168. thy z [j] [i] = his c [j - 1];
  169. }
  170. }
  171. return thee;
  172. } catch (MelderError) {
  173. Melder_throw (me, U": no Matrix created.");
  174. }
  175. }
  176. /* End of file Cepstrumc.cpp */