Debian Codegraph @

LPC/Cepstrumc.cpp

raw symbols SCIP
1 /* Cepstrumc.c
2 *
3 * Copyright (C) 1994-2020 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 /*
20 djmw 20020812 GPL header
21 djmw 20061218 Changed info to Melder_writeLine<x> format.
22 djmw 20071017 oo_CAN_WRITE_AS_ENCODING.h
23 djmw 20080122 Version 1: float -> double
24 djmw 20110304 Thing_new
25 */
26
27 #include "Cepstrumc.h"
28 #include "DTW.h"
29
30 #include "oo_DESTROY.h"
31 #include "Cepstrumc_def.h"
32 #include "oo_COPY.h"
33 #include "Cepstrumc_def.h"
34 #include "oo_EQUAL.h"
35 #include "Cepstrumc_def.h"
36 #include "oo_CAN_WRITE_AS_ENCODING.h"
37 #include "Cepstrumc_def.h"
38 #include "oo_WRITE_TEXT.h"
39 #include "Cepstrumc_def.h"
40 #include "oo_WRITE_BINARY.h"
41 #include "Cepstrumc_def.h"
42 #include "oo_READ_TEXT.h"
43 #include "Cepstrumc_def.h"
44 #include "oo_READ_BINARY.h"
45 #include "Cepstrumc_def.h"
46 #include "oo_DESCRIPTION.h"
47 #include "Cepstrumc_def.h"
48
49 Thing_implement (Cepstrumc, Sampled, 1);
50
51 void structCepstrumc :: v1_info () {
52 structDaata :: v1_info ();
53 MelderInfo_writeLine (U" Start time: ", xmin);
54 MelderInfo_writeLine (U" End time: ", xmax);
55 MelderInfo_writeLine (U" Number of frames: ", nx);
56 MelderInfo_writeLine (U" Time step: ", dx);
57 MelderInfo_writeLine (U" First frame at: ", x1);
58 MelderInfo_writeLine (U" Number of coefficients: ", maxnCoefficients);
59 }
60
61 void Cepstrumc_Frame_init (Cepstrumc_Frame me, integer nCoefficients) {
62 my c = zero_VEC (nCoefficients);
63 my nCoefficients = my c.size; // maintain invariant
64 }
65
66 void Cepstrumc_init (Cepstrumc me, double tmin, double tmax, integer nt, double dt, double t1, integer nCoefficients, double samplingFrequency) {
67 my samplingFrequency = samplingFrequency;
68 my maxnCoefficients = nCoefficients;
69 Sampled_init (me, tmin, tmax, nt, dt, t1);
70 my frame = newvectorzero <structCepstrumc_Frame> (nt);
71 }
72
73 autoCepstrumc Cepstrumc_create (double tmin, double tmax, integer nt, double dt, double t1, integer nCoefficients, double samplingFrequency) {
74 try {
75 autoCepstrumc me = Thing_new (Cepstrumc);
76 Cepstrumc_init (me.get(), tmin, tmax, nt, dt, t1, nCoefficients, samplingFrequency);
77 return me;
78 } catch (MelderError) {
79 Melder_throw (U"Cepstrum not created.");
80 }
81 }
82
83 static void regression (VEC const& r, Cepstrumc me, integer frameNumber, integer numberOfRegressionFrames) {
84 Melder_assert (r.size == my maxnCoefficients + 1);
85 r <<= 0.0;
86
87 if (frameNumber <= numberOfRegressionFrames / 2 || frameNumber > my nx - numberOfRegressionFrames / 2)
88 return;
89 integer nc = INTEGER_MAX;
90 longdouble sumsq = 0.0;
91 for (integer j = -numberOfRegressionFrames / 2; j <= numberOfRegressionFrames / 2; j ++) {
92 const Cepstrumc_Frame f = & my frame [frameNumber + j];
93 if (f -> nCoefficients < nc)
94 nc = f -> nCoefficients;
95 sumsq += j * j;
96 }
97 for (integer i = 0; i <= nc; i ++) {
98 for (integer j = -numberOfRegressionFrames / 2; j <= numberOfRegressionFrames / 2; j ++) {
99 const Cepstrumc_Frame f = & my frame [frameNumber + j];
100 r [i + 1] += f -> c [i] * j / (double (sumsq) * my dx);
101 }
102 }
103 }
104
105 autoDTW Cepstrumc_to_DTW (Cepstrumc me, Cepstrumc thee, double wc, double wle, double wr, double wer, double dtr, int matchStart, int matchEnd, int constraint) {
106 try {
107 integer numberOfRegressionFrames = Melder_ifloor (dtr / my dx);
108 Melder_require (my maxnCoefficients == thy maxnCoefficients,
109 U"Cepstrumc orders should be equal.");
110 Melder_require (! (wr != 0.0 && numberOfRegressionFrames < 2),
111 U"Time window for regression coefficients too small.");
112 if (numberOfRegressionFrames % 2 == 0)
113 numberOfRegressionFrames += 1;
114 if (wr != 0.0)
115 Melder_casual (U"Number of frames used for regression coefficients ", numberOfRegressionFrames);
116 autoDTW him = DTW_create (my xmin, my xmax, my nx, my dx, my x1, thy xmin, thy xmax, thy nx, thy dx, thy x1);
117 autoVEC ri = raw_VEC (my maxnCoefficients + 1);
118 autoVEC rj = raw_VEC (my maxnCoefficients + 1);
119 /*
120 Calculate distance matrix.
121 */
122 autoMelderProgress progress (U"");
123 for (integer iframe = 1; iframe <= my nx; iframe ++) {
124 const Cepstrumc_Frame fi = & my frame [iframe];
125 regression (ri.get(), me, iframe, numberOfRegressionFrames);
126 for (integer jframe = 1; jframe <= thy nx; jframe ++) {
127 const Cepstrumc_Frame fj = & thy frame [jframe];
128 longdouble dist = 0.0, distr = 0.0;
129 /*
130 Cepstral distance.
131 */
132 if (wc != 0.0) {
133 for (integer k = 1; k <= fj -> nCoefficients; k ++) {
134 const double dci = fi -> c [k] - fj -> c [k];
135 dist += dci * dci;
136 }
137 dist *= wc;
138 }
139 /*
140 Log energy distance.
141 */
142 const double dc0 = fi -> c [0] - fj -> c [0];
143 dist += wle * dc0 * dc0;
144 /*
145 Regression distance.
146 */
147 if (wr != 0.0) {
148 regression (rj.get(), thee, jframe, numberOfRegressionFrames);
149 for (integer k = 1; k <= fj -> nCoefficients; k ++) {
150 const double drci = ri [k + 1] - rj [k + 1];
151 distr += drci * drci;
152 }
153 dist += wr * distr;
154 }
155 /*
156 Regression on c [0]: log(energy)
157 */
158 if (wer != 0.0) {
159 if (wr == 0.0)
160 regression (rj.get(), thee, jframe, numberOfRegressionFrames);
161 const double drc0 = ri [1] - rj [1];
162 dist += wer * drc0 * drc0;
163 }
164 dist /= wc + wle + wr + wer;
165 his z [iframe] [jframe] = sqrt ((double) dist); // prototype along y-direction
166 }
167 Melder_progress ((double) iframe / my nx, U"Calculate distances: frame ", iframe, U" from ", my nx, U".");
168 }
169 DTW_findPath (him.get(), matchStart, matchEnd, constraint);
170 return him;
171 } catch (MelderError) {
172 Melder_throw (U"DTW not created.");
173 }
174 }
175
176 autoMatrix Cepstrumc_to_Matrix (Cepstrumc me) {
177 try {
178 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0, my maxnCoefficients, my maxnCoefficients + 1, 1, 0);
179
180 for (integer i = 1; i <= my nx; i ++) {
181 const Cepstrumc_Frame him = & my frame [i];
182 for (integer j = 1; j <= his nCoefficients + 1; j ++)
183 thy z [j] [i] = his c [j - 1];
184 }
185 return thee;
186 } catch (MelderError) {
187 Melder_throw (me, U": no Matrix created.");
188 }
189 }
190
191 /* End of file Cepstrumc.cpp */