Debian Codegraph @

melder/NUMinterpol.cpp

raw symbols SCIP
1 /* NUMinterpol.cpp
2 *
3 * Copyright (C) 1992-2008,2011,2012,2014,2015,2017,2018,2020 Paul Boersma
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.
13 * See the GNU 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 * pb 2002/03/07 GPL
21 * pb 2003/06/19 ridders3 replaced with ridders
22 * pb 2003/07/09 gsl
23 * pb 2007/01/27 use #defines for value interpolation
24 * pb 2007/08/20 built a "weird value" check into NUMviterbi (bug report by Adam Jacks)
25 * pb 2011/03/29 C++
26 */
27
28 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
29 #include "../dwsys/NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
30
31 double NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (constVECcxx . . $ constVEC# const& y, double x, integercxx . . $ integer# maxDepth) {
32 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (isdefined (x));
33 const integercxx . . $ integer# midleft = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (x), midright = midleft + 1;
34 double result = 0.0;
35 if (y.sizecxx . . $ constvector#size. < 1)
36 return undefinedcxx . . $ undefined.; // there exists no best guess
37 if (x < 1)
38 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]; // offleft: constant extrapolation
39 if (x > y.sizecxx . . $ constvector#size.)
40 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).y.sizecxx . . $ constvector#size.]; // offright: constant extrapolation
41 if (x == midleft)
42 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midleft]; // the interpolated curve goes through the points
43 /*
44 1 < x < y.size && x not integer: interpolate.
45 */
46 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& maxDepth, midright - 1);
47 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& maxDepth, y.sizecxx . . $ constvector#size. - midleft);
48 if (maxDepth <= NUM_VALUE_INTERPOLATE_NEARESTcxx . . $ `melder/NUMinterpol.h:24:9`!)
49 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (x)]; // possible because we know that 1 <= x <= y.size
50 if (maxDepth == NUM_VALUE_INTERPOLATE_LINEARcxx . . $ `melder/NUMinterpol.h:25:9`!)
51 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midleft] + (x - midleft) * (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midright] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midleft]);
52 if (maxDepth == NUM_VALUE_INTERPOLATE_CUBICcxx . . $ `melder/NUMinterpol.h:26:9`!) {
53 const double yl = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midleft], yr = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midright];
54 const double dyl = 0.5 * (yr - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midleft - 1]), dyr = 0.5 * (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).midright + 1] - yl);
55 const double fil = x - midleft, fir = midright - x;
56 return yl * fir + yr * fil - fil * fir * (0.5 * (dyr - dyl) + (fil - 0.5) * (dyl + dyr - 2 * (yr - yl)));
57 }
58 /*
59 maxDepth >= 3: sinc interpolation
60 */
61 const integercxx . . $ integer# left = midright - maxDepth; // as maxDepth <= midright - 1, we know that left >= 1
62 const integercxx . . $ integer# right = midleft + maxDepth; // as maxDepth <= y.size - midleft, we know that right <= y.size
63 /*
64 The sinc function contains a sine wave with a phase shift of pi per sample. For sample `i`:
65 sinc [i] = sin (pi * (i - x)) / (pi * (i - x)) =
66 = sin (pi * (x - i)) / (pi * (x - i))
67 The sought interpolated value will then be
68 result = sum_i=left^right sinc [i] * y [i] * window [i] / sum_i=left^right window [i]
69 where `window` is a raised cosine:
70 window [i] = 0.5 + 0.5 * cos (pi * (x - i) / (x - left + 1)) (for `left` <= `i` <= `midleft`)
71 window [i] = 0.5 + 0.5 * cos (pi * (i - x) / (right - x + 1)) (for `midright` <= `i` <= `right`)
72
73 We can check whether a more symmetric window is better, e.g.
74 window [i] = 0.5 + 0.5 * cos (pi * (i - x) / (maxDepth + 0.5)) (for `left` <= `i` <= `right`)
75 That turns out to be about 0.1 dB worse, but `maxDepth` + 2.0 does much better on the script `fon/resamplingDepth.praat`;
76 the aliasing-suppressions are (in dB):
77
78 Depth: 700 50 20 10 6 3 2 1
79 52.366 41.348 39.357 38.267 36.956 27.897 20.839 19.383
80 52.365 41.328 39.309 38.180 36.734 27.901 20.839 19.383 maxDepth + 0.5
81 52.261 41.298 39.269 38.068 36.485 24.870 20.839 19.383 maxDepth + 0.0
82 52.371 41.358 39.349 38.284 36.967 30.742 20.839 19.383 maxDepth + 1.0
83 52.377 41.387 39.388 38.375 37.177 32.949 20.839 19.383 maxDepth + 1.5
84 52.382 41.417 39.426 38.445 37.337 34.032 20.839 19.383 maxDepth + 2.0 but strong reflection of f=0 at fsamp
85 52.387 41.446 39.462 38.490 37.429 *34.042 20.839 19.383 maxDepth + 2.5
86 52.392 41.476 39.496 *38.510 *37.450 33.504 20.839 19.383 maxDepth + 3.0
87 52.397 41.505 39.528 38.503 37.406 32.830 20.839 19.383 maxDepth + 3.5
88 52.401 41.533 39.558 38.472 37.309 32.193 20.839 19.383 maxDepth + 4.0
89 52.443 41.856 *39.728 37.254 35.390 29.054 20.839 19.383 maxDepth + 10.0
90 52.503 42.287 39.523 35.747 34.030 28.212 20.839 19.383 maxDepth + 20.0
91 52.676 42.933 38.803 34.627 33.309 27.900 20.839 19.383 maxDepth + 50.0
92 52.951 43.185 38.434 34.349 33.163 27.847 20.839 19.383 maxDepth + 100.0
93 53.432 43.242 38.283 34.263 33.121 27.833 20.839 19.383 maxDepth + 200.0
94 54.116 *43.246 38.235 34.239 33.110 27.829 20.839 19.383 maxDepth + 400.0
95 54.766 43.245 38.222 34.232 33.107 27.828 20.839 19.383 maxDepth + 800.0
96 55.109 43.245 38.218 34.231 33.106 27.828 20.839 19.383 maxDepth + 1600.0
97 55.213 43.245 38.217 34.230 33.106 27.828 20.839 19.383 maxDepth + 3200.0
98 *55.246 43.245 38.217 34.230 33.105 27.828 20.839 19.383 maxDepth + 1e9
99 53.975 42.453 *39.728 38.347 *37.451 32.949 20.839 19.383 maxDepth * 1.5
100
101 As we will be approximating the sine and cosine values by matrix multiplication
102 (instead of recomputing them every time we need them)
103 we will start where the values of the sinc function are highest, i.e. at `midleft` and `midright`.
104
105 First interpolate over the samples on the left, i.e. for `left` <= `i` <= `midleft`.
106
107 With
108 leftPhase [i] = pi * (x - i)
109 leftDepth = x - left + 1 ; or maxDepth + 0.5
110 we get
111 sinc [i] * window [i] = 0.5 * sin (leftPhase [i]) / leftPhase [i] * (1.0 + cos (leftPhase [i] / leftDepth))
112 Iterate for `i` from `midleft` to `left`:
113 result := 0
114 leftPhase := pi * (x - midleft)
115 windowPhase := leftPhase / leftDepth
116 for i from midleft downto left
117 sincTimesWindow := 0.5 * sin (leftPhase) / leftPhase * (1.0 + cos (windowPhase))
118 result += y [i] * sincTimesWindow
119 leftPhase += pi
120 windowPhase += pi / leftDepth
121 endfor
122 Adding a phase of pi just reverses the sign of the sine. So we can simplify the loop:
123 result := 0
124 leftPhase := pi * (x - midleft)
125 windowPhase := leftPhase / leftDepth
126 windowPhaseStep = pi / leftDepth
127 halfSinLeftPhase := 0.5 * sin (leftPhase)
128 for i from midleft downto left
129 sincTimesWindow := halfSinLeftPhase / leftPhase * (1.0 + cos (windowPhase))
130 result += y [i] * sincTimesWindow
131 leftPhase += pi
132 halfSinLeftPhase := - halfSinLeftPhase
133 windowPhase += windowPhaseStep
134 endfor
135 The computationally slow part of this is the computation of the cosine within the loop.
136 This can be sped up by computing both the sine and cosine of the current `windowPhase` and then
137 computing the sine and cosine of the next `windowPhase`, i.e. the current `windowPhase` plus `windowPhaseStep`:
138 sin (windowPhase + windowPhaseStep) = sin (windowPhase) * cos (windowPhaseStep) + cos (windowPhase) * sin (windowPhaseStep)
139 cos (windowPhase + windowPhaseStep) = cos (windowPhase) * cos (windowPhaseStep) - sin (windowPhase) * sin (windowPhaseStep)
140 */
141 enum class enumWindowShapecxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape# { RAISED_COSINEcxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape#RAISED_COSINE., RAISED_GAUSSIANcxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape#RAISED_GAUSSIAN. };
142 constexpr enumWindowShapecxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape# WINDOW_SHAPE = enumWindowShapecxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape# :: RAISED_COSINEcxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape#RAISED_COSINE.;
143 if (WINDOW_SHAPE == enumWindowShapecxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape# :: RAISED_COSINEcxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape#RAISED_COSINE.) {
144 {// scope: left half
145 //const double leftDepth = x - left + 1.0;
146 const double leftDepth = maxDepth + 0.5;
147 const double windowPhaseStep = NUMpicxx . . $ NUMpi. / leftDepth;
148 const double sinWindowPhaseStep = sincxx . . $ sin(fee4f1d22bc8e617). (windowPhaseStep);
149 const double cosWindowPhaseStep = coscxx . . $ cos(fee4f1d22bc8e617). (windowPhaseStep);
150 /*
151 Initialize the phase of the sinc.
152 */
153 double leftPhase = NUMpicxx . . $ NUMpi. * (x - midleft);
154 double halfSinLeftPhase = 0.5 * sincxx . . $ sin(fee4f1d22bc8e617). (leftPhase);
155 /*
156 Initialize the phase of the window.
157 */
158 double windowPhase = leftPhase / leftDepth;
159 double sinWindowPhase = sincxx . . $ sin(fee4f1d22bc8e617). (windowPhase);
160 double cosWindowPhase = coscxx . . $ cos(fee4f1d22bc8e617). (windowPhase);
161 /*
162 Step through the samples.
163 */
164 for (integercxx . . $ integer# ix = midleft; ix >= left; ix --) {
165 /*
166 Accumulate the contribution of this sample to the result.
167 */
168 const double sincTimesWindow = halfSinLeftPhase / leftPhase * (1.0 + cosWindowPhase);
169 result += y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] * sincTimesWindow;
170 /*
171 Update the phase of the sinc.
172 */
173 leftPhase += NUMpicxx . . $ NUMpi.;
174 halfSinLeftPhase = - halfSinLeftPhase;
175 /*
176 Update the phase of the window.
177 */
178 //windowPhase += windowPhaseStep; // superfluous statement (though semantically correct)
179 const double nextSinWindowPhase = cosWindowPhase * sinWindowPhaseStep + sinWindowPhase * cosWindowPhaseStep;
180 const double nextCosWindowPhase = cosWindowPhase * cosWindowPhaseStep - sinWindowPhase * sinWindowPhaseStep;
181 sinWindowPhase = nextSinWindowPhase;
182 cosWindowPhase = nextCosWindowPhase;
183 }
184 }
185 /*
186 Then interpolate over the samples on the right, i.e. for `midright` <= `i` <= `right`.
187 */
188 {// scope: right half
189 //const double rightDepth = right - x + 1.0;
190 const double rightDepth = maxDepth + 0.5;
191 const double windowPhaseStep = NUMpicxx . . $ NUMpi. / rightDepth;
192 const double sinWindowPhaseStep = sincxx . . $ sin(fee4f1d22bc8e617). (windowPhaseStep);
193 const double cosWindowPhaseStep = coscxx . . $ cos(fee4f1d22bc8e617). (windowPhaseStep);
194 /*
195 Initialize the phase of the sinc.
196 */
197 double rightPhase = NUMpicxx . . $ NUMpi. * (midright - x);
198 double halfSinRightPhase = 0.5 * sincxx . . $ sin(fee4f1d22bc8e617). (rightPhase);
199 /*
200 Initialize the phase of the window.
201 */
202 double windowPhase = rightPhase / rightDepth;
203 double sinWindowPhase = sincxx . . $ sin(fee4f1d22bc8e617). (windowPhase);
204 double cosWindowPhase = coscxx . . $ cos(fee4f1d22bc8e617). (windowPhase);
205 /*
206 Step through the samples.
207 */
208 for (integercxx . . $ integer# ix = midright; ix <= right; ix ++) {
209 /*
210 Accumulate the contribution of this sample to the result.
211 */
212 const double sincTimesWindow = halfSinRightPhase / rightPhase * (1.0 + cosWindowPhase);
213 result += y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] * sincTimesWindow;
214 /*
215 Update the phase of the sinc.
216 */
217 rightPhase += NUMpicxx . . $ NUMpi.;
218 halfSinRightPhase = - halfSinRightPhase;
219 /*
220 Update the phase of the window.
221 */
222 //windowPhase += windowPhaseStep; // superfluous statement (though semantically correct)
223 const double nextSinWindowPhase = cosWindowPhase * sinWindowPhaseStep + sinWindowPhase * cosWindowPhaseStep;
224 const double nextCosWindowPhase = cosWindowPhase * cosWindowPhaseStep - sinWindowPhase * sinWindowPhaseStep;
225 sinWindowPhase = nextSinWindowPhase;
226 cosWindowPhase = nextCosWindowPhase;
227 }
228 }
229 } else if (WINDOW_SHAPE == enumWindowShapecxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape# :: RAISED_GAUSSIANcxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f).enumWindowShape#RAISED_GAUSSIAN.) {
230 /*
231 Check whether an exponential window is better, e.g.
232 window [i] = (exp (-12.0 * ((i - x) / (maxDepth + 0.5))^2) - exp (-12.0)) / (1.0 - exp (-12.0)) for `left` <= `i` <= `right`
233 With
234 leftPhase [i] = pi * (x - i)
235 leftDepth = maxDepth + 0.5
236 we get
237 sinc [i] * window [i] = sin (leftPhase [i]) / leftPhase [i] *
238 * exp (-12.0/pi^2 * (leftPhase [i] / leftDepth)^2) - exp (-12.0)) / (1.0 + exp (-12.0))
239 Iterate for `i` from `midleft` to `left`:
240 result := 0
241 leftPhase := pi * (x - midleft)
242 windowPhase := -12/pi * leftPhase / leftDepth
243 for i from midleft downto left
244 sincTimesWindow := sin (leftPhase) / leftPhase * (exp (windowPhase) - exp (-12.0)) / (1.0 + exp (-12.0))
245 result += y [i] * sincTimesWindow
246 leftPhase += pi
247 windowPhase += pi / leftDepth
248 endfor
249 Adding a phase of pi just reverses the sign of the sine. So we can simplify the loop:
250 result := 0
251 leftPhase := pi * (x - midleft)
252 windowPhase := -12/pi^2 * (leftPhase / leftDepth)^2
253 sinLeftPhase := sin (leftPhase)
254 for i from midleft downto left
255 sincTimesWindow := sinLeftPhase / leftPhase * (exp (windowPhase) - exp (-12.0)) / (1.0 + exp (-12.0))
256 result += y [i] * sincTimesWindow
257 leftPhase += pi
258 sinLeftPhase := - sinLeftPhase
259 windowPhase = -12/pi^2 * (leftPhase / leftDepth)^2
260 endfor
261 The computationally slow part of this is the computation of the exponential within the loop.
262 This can be sped up by computing the exponential of the current `windowPhase` and then
263 computing the exponential of the next `windowPhase`, i.e. the current `windowPhase` plus `windowPhaseStep`:
264 exp (windowPhase + windowPhaseStep) = exp (windowPhase) * exp (windowPhaseStep)
265 */
266 {// scope: left half
267 const double leftDepth = maxDepth + 0.5;
268 /*
269 Initialize the phase of the sinc.
270 */
271 double leftPhase = NUMpicxx . . $ NUMpi. * (x - midleft);
272 double sinLeftPhase = sincxx . . $ sin(fee4f1d22bc8e617). (leftPhase);
273 /*
274 Initialize the phase of the window.
275 */
276 double windowPhase = (-12.0 / NUMpicxx . . $ NUMpi. / NUMpicxx . . $ NUMpi.) * sqrcxx . . $ sqr(ada6a8422704cf8a). (leftPhase / leftDepth);
277 /*
278 Step through the samples.
279 */
280 for (integercxx . . $ integer# ix = midleft; ix >= left; ix --) {
281 /*
282 Accumulate the contribution of this sample to the result.
283 */
284 const double sincTimesWindow = sinLeftPhase / leftPhase * (expcxx . . $ exp(fee4f1d22bc8e617). (windowPhase) - expcxx . . $ exp(fee4f1d22bc8e617). (-12.0)) / (1.0 + expcxx . . $ exp(fee4f1d22bc8e617). (-12.0));
285 result += y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] * sincTimesWindow;
286 /*
287 Update the phase of the sinc.
288 */
289 leftPhase += NUMpicxx . . $ NUMpi.;
290 sinLeftPhase = - sinLeftPhase;
291 /*
292 Update the phase of the window.
293 */
294 windowPhase += (-12.0 / NUMpicxx . . $ NUMpi. / NUMpicxx . . $ NUMpi.) * sqrcxx . . $ sqr(ada6a8422704cf8a). (leftPhase / leftDepth);
295 }
296 }
297 /*
298 Then interpolate over the samples on the right, i.e. for `midright` <= `i` <= `right`.
299 */
300 {// scope: right half
301 const double rightDepth = maxDepth + 0.5;
302 /*
303 Initialize the phase of the sinc.
304 */
305 double rightPhase = NUMpicxx . . $ NUMpi. * (midright - x);
306 double sinRightPhase = sincxx . . $ sin(fee4f1d22bc8e617). (rightPhase);
307 /*
308 Initialize the phase of the window.
309 */
310 double windowPhase = (-12.0 / NUMpicxx . . $ NUMpi. / NUMpicxx . . $ NUMpi.) * sqrcxx . . $ sqr(ada6a8422704cf8a). (rightPhase / rightDepth);
311 /*
312 Step through the samples.
313 */
314 for (integercxx . . $ integer# ix = midright; ix <= right; ix ++) {
315 /*
316 Accumulate the contribution of this sample to the result.
317 */
318 const double sincTimesWindow = sinRightPhase / rightPhase * (expcxx . . $ exp(fee4f1d22bc8e617). (windowPhase) - expcxx . . $ exp(fee4f1d22bc8e617). (-12.0)) / (1.0 + expcxx . . $ exp(fee4f1d22bc8e617). (-12.0));
319 result += y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] * sincTimesWindow;
320 /*
321 Update the phase of the sinc.
322 */
323 rightPhase += NUMpicxx . . $ NUMpi.;
324 sinRightPhase = - sinRightPhase;
325 /*
326 Update the phase of the window.
327 */
328 windowPhase += (-12.0 / NUMpicxx . . $ NUMpi. / NUMpicxx . . $ NUMpi.) * sqrcxx . . $ sqr(ada6a8422704cf8a). (rightPhase / rightDepth);
329 }
330 }
331 }
332 return result;
333 }
334
335 /********** Improving extrema **********/
336 #pragma mark Improving extrema
337
338 struct improve_paramscxx . . $ improve_params# {
339 integercxx . . $ integer# depthcxx . . $ improve_params#depth.;
340 constVECcxx . . $ constVEC# ycxx . . $ improve_params#y.;
341 bool isMaximumcxx . . $ improve_params#isMaximum.;
342 };
343
344 static double improve_evaluatecxx . . $ improve_evaluate(81b005370e6958c4). (double x, void *closure) {
345 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (isdefined (x));
346 struct improve_paramscxx . . $ improve_params# *me = (struct improve_paramscxx . . $ improve_params# *) closure;
347 const double y = NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ improve_params#y., x, mycxx . . $ `melder/melder.h:44:9`! depthcxx . . $ improve_params#depth.);
348 return mycxx . . $ `melder/melder.h:44:9`! isMaximumcxx . . $ improve_params#isMaximum. ? - y : y;
349 }
350
351 double NUMimproveExtremumcxx . . $ NUMimproveExtremum(b8820d1df15d30e8). (constVECcxx . . $ constVEC# const& y, integercxx . . $ integer# ixmid, integercxx . . $ integer# interpolationDepth, double *ixmid_real, bool isMaximum) {
352 struct improve_paramscxx . . $ improve_params# params;
353 double result;
354 if (ixmid <= 1) {
355 *ixmid_real = double (1);
356 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1];
357 }
358 if (ixmid >= y.sizecxx . . $ constvector#size.) {
359 *ixmid_real = double (y.sizecxx . . $ constvector#size.);
360 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).y.sizecxx . . $ constvector#size.];
361 }
362 if (interpolationDepth <= NUM_PEAK_INTERPOLATE_NONEcxx . . $ `melder/NUMinterpol.h:32:9`!) {
363 *ixmid_real = double (ixmid);
364 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid];
365 }
366 if (interpolationDepth == NUM_PEAK_INTERPOLATE_PARABOLICcxx . . $ `melder/NUMinterpol.h:33:9`!) {
367 const double dy = 0.5 * (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid + 1] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid - 1]);
368 const double d2y = 2 * y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid - 1] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid + 1];
369 *ixmid_real = ixmid + dy / d2y;
370 return y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ixmid] + 0.5 * dy * dy / d2y;
371 }
372 /*
373 Cubic or sinc interpolation.
374 */
375 params. depthcxx . . $ improve_params#depth. = (
376 interpolationDepth == NUM_PEAK_INTERPOLATE_CUBICcxx . . $ `melder/NUMinterpol.h:34:9`! ? NUM_VALUE_INTERPOLATE_CUBICcxx . . $ `melder/NUMinterpol.h:26:9`! :
377 interpolationDepth == NUM_PEAK_INTERPOLATE_SINC70cxx . . $ `melder/NUMinterpol.h:35:9`! ? NUM_VALUE_INTERPOLATE_SINC70cxx . . $ `melder/NUMinterpol.h:28:9`! :
378 NUM_VALUE_INTERPOLATE_SINC700cxx . . $ `melder/NUMinterpol.h:29:9`!
379 );
380 params. ycxx . . $ improve_params#y. =cxx . . $ constvector#`operator=`(6d3133e561d4cdd5). y;
381 params. isMaximumcxx . . $ improve_params#isMaximum. = isMaximum;
382 *ixmid_real = NUMminimize_brentcxx . . $ NUMminimize_brent(132a2f8a908660df). (improve_evaluatecxx . . $ improve_evaluate(81b005370e6958c4)., ixmid - 1, ixmid + 1, & params, 1e-10, & result);
383 return isMaximum ? - result : result;
384 }
385
386 double NUMimproveMinimumcxx . . $ NUMimproveMinimum(c86a71e1ecac36cd). (constVECcxx . . $ constVEC# const& y, integercxx . . $ integer# ixmid, integercxx . . $ integer# interpolationDepth, double *ixmid_real) {
387 return NUMimproveExtremumcxx . . $ NUMimproveExtremum(b8820d1df15d30e8). (y, ixmid, interpolationDepth, ixmid_real, false);
388 }
389 double NUMimproveMaximumcxx . . $ NUMimproveMaximum(c86a71e1ecac36cd). (constVECcxx . . $ constVEC# const& y, integercxx . . $ integer# ixmid, integercxx . . $ integer# interpolationDepth, double *ixmid_real) {
390 return NUMimproveExtremumcxx . . $ NUMimproveExtremum(b8820d1df15d30e8). (y, ixmid, interpolationDepth, ixmid_real, true);
391 }
392
393 /********** Viterbi **********/
394
395 void NUM_viterbicxx . . $ NUM_viterbi(d75934b11c111a17). (
396 integercxx . . $ integer# numberOfFrames, integercxx . . $ integer# maxnCandidates,
397 integercxx . . $ integer# (*getNumberOfCandidates) (integercxx . . $ integer# iframe, void *closure),
398 double (*getLocalCost) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand, void *closure),
399 double (*getTransitionCost) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand1, integercxx . . $ integer# icand2, void *closure),
400 void (*putResult) (integercxx . . $ integer# iframe, integercxx . . $ integer# place, void *closure),
401 void *closure)
402 {
403 autoMATcxx . . $ autoMAT# delta = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (numberOfFrames, maxnCandidates);
404 autoINTMATcxx . . $ autoINTMAT# psi = raw_INTMATcxx . . $ raw_INTMAT(4cdd710bd00a100c). (numberOfFrames, maxnCandidates);
405 autoINTVECcxx . . $ autoINTVEC# numberOfCandidates = raw_INTVECcxx . . $ raw_INTVEC(dab5d3ef28d6a25). (numberOfFrames);
406 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
407 numberOfCandidates [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe] = getNumberOfCandidates (iframe, closure);
408 for (integercxx . . $ integer# icand = 1; icand <= numberOfCandidates [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; icand ++)
409 delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand] = - getLocalCost (iframe, icand, closure);
410 }
411 for (integercxx . . $ integer# iframe = 2; iframe <= numberOfFrames; iframe ++) {
412 for (integercxx . . $ integer# icand2 = 1; icand2 <= numberOfCandidates [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; icand2 ++) {
413 double maximum = -1e308;
414 integercxx . . $ integer# place = 0;
415 for (integercxx . . $ integer# icand1 = 1; icand1 <= numberOfCandidates [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe - 1]; icand1 ++) {
416 const double value = delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe - 1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand1] + delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand2]
417 - getTransitionCost (iframe, icand1, icand2, closure);
418 if (value > maximum) {
419 maximum = value;
420 place = icand1;
421 }
422 }
423 if (place == 0)
424 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Viterbi algorithm cannot compute a track because of weird values.");
425 delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand2] = maximum;
426 psi [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand2] = place;
427 }
428 }
429 /*
430 Find the end of the most probable path.
431 */
432 integercxx . . $ integer# place;
433 double maximum = delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).numberOfFrames] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).place = 1];
434 for (integercxx . . $ integer# icand = 2; icand <= numberOfCandidates [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).numberOfFrames]; icand ++) {
435 if (delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).numberOfFrames] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icand] > maximum)
436 maximum = delta [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).numberOfFrames] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).place = icand];
437 }
438 /*
439 Backtrack.
440 */
441 for (integercxx . . $ integer# iframe = numberOfFrames; iframe >= 1; iframe --) {
442 putResult (iframe, place, closure);
443 place = psi [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).place];
444 }
445 }
446
447 /******************/
448
449 struct parm2cxx . . $ parm2# {
450 integercxx . . $ integer# ntrackcxx . . $ parm2#ntrack.;
451 integercxx . . $ integer# ncombcxx . . $ parm2#ncomb.;
452 INTMATcxx . . $ INTMAT# indicescxx . . $ parm2#indices.;
453 double (*getLocalCostcxx . . $ parm2#getLocalCost.) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand, integercxx . . $ integer# itrack, void *closure);
454 double (*getTransitionCostcxx . . $ parm2#getTransitionCost.) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand1, integercxx . . $ integer# icand2, integercxx . . $ integer# itrack, void *closure);
455 void (*putResultcxx . . $ parm2#putResult.) (integercxx . . $ integer# iframe, integercxx . . $ integer# place, integercxx . . $ integer# itrack, void *closure);
456 void *closurecxx . . $ parm2#closure.;
457 };
458
459 static integercxx . . $ integer# getNumberOfCandidates_ncxx . . $ getNumberOfCandidates_n(9d83663a1222538d). (integercxx . . $ integer# iframe, void *closure) {
460 struct parm2cxx . . $ parm2# *me = (struct parm2cxx . . $ parm2# *) closure;
461 (void) iframe;
462 return mycxx . . $ `melder/melder.h:44:9`! ncombcxx . . $ parm2#ncomb.;
463 }
464 static double getLocalCost_ncxx . . $ getLocalCost_n(e2193e750319682c). (integercxx . . $ integer# iframe, integercxx . . $ integer# jcand, void *closure) {
465 struct parm2cxx . . $ parm2# *me = (struct parm2cxx . . $ parm2# *) closure;
466 double localCost = 0.0;
467 for (integercxx . . $ integer# itrack = 1; itrack <= mycxx . . $ `melder/melder.h:44:9`! ntrackcxx . . $ parm2#ntrack.; itrack ++)
468 localCost += mycxx . . $ `melder/melder.h:44:9`! getLocalCostcxx . . $ parm2#getLocalCost. (iframe, mycxx . . $ `melder/melder.h:44:9`! indicescxx . . $ parm2#indices. [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).jcand] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrack], itrack, mycxx . . $ `melder/melder.h:44:9`! closurecxx . . $ parm2#closure.);
469 return localCost;
470 }
471 static double getTransitionCost_ncxx . . $ getTransitionCost_n(557e8ff0ab70c1a1). (integercxx . . $ integer# iframe, integercxx . . $ integer# jcand1, integercxx . . $ integer# jcand2, void *closure) {
472 struct parm2cxx . . $ parm2# *me = (struct parm2cxx . . $ parm2# *) closure;
473 double transitionCost = 0.0;
474 for (integercxx . . $ integer# itrack = 1; itrack <= mycxx . . $ `melder/melder.h:44:9`! ntrackcxx . . $ parm2#ntrack.; itrack ++)
475 transitionCost += mycxx . . $ `melder/melder.h:44:9`! getTransitionCostcxx . . $ parm2#getTransitionCost. (iframe,
476 mycxx . . $ `melder/melder.h:44:9`! indicescxx . . $ parm2#indices. [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).jcand1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrack], mycxx . . $ `melder/melder.h:44:9`! indicescxx . . $ parm2#indices. [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).jcand2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrack], itrack, mycxx . . $ `melder/melder.h:44:9`! closurecxx . . $ parm2#closure.);
477 return transitionCost;
478 }
479 static void putResult_ncxx . . $ putResult_n(fefdcffd337bd722). (integercxx . . $ integer# iframe, integercxx . . $ integer# jplace, void *closure) {
480 struct parm2cxx . . $ parm2# *me = (struct parm2cxx . . $ parm2# *) closure;
481 for (integercxx . . $ integer# itrack = 1; itrack <= mycxx . . $ `melder/melder.h:44:9`! ntrackcxx . . $ parm2#ntrack.; itrack ++)
482 mycxx . . $ `melder/melder.h:44:9`! putResultcxx . . $ parm2#putResult. (iframe, mycxx . . $ `melder/melder.h:44:9`! indicescxx . . $ parm2#indices. [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).jplace] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrack], itrack, mycxx . . $ `melder/melder.h:44:9`! closurecxx . . $ parm2#closure.);
483 }
484
485 void NUM_viterbi_multicxx . . $ NUM_viterbi_multi(23bde724377b0527). (
486 integercxx . . $ integer# nframe, integercxx . . $ integer# ncand, integercxx . . $ integer# ntrack,
487 double (*getLocalCost) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand, integercxx . . $ integer# itrack, void *closure),
488 double (*getTransitionCost) (integercxx . . $ integer# iframe, integercxx . . $ integer# icand1, integercxx . . $ integer# icand2, integercxx . . $ integer# itrack, void *closure),
489 void (*putResult) (integercxx . . $ integer# iframe, integercxx . . $ integer# place, integercxx . . $ integer# itrack, void *closure),
490 void *closure)
491 {
492 struct parm2cxx . . $ parm2# parm;
493
494 if (ntrack > ncand) Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"(NUM_viterbi_multi:) "
495 U"Number of tracks (", ntrack, U") should not exceed number of candidates (", ncand, U").");
496 const integercxx . . $ integer# ncomb = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (NUMcombinationscxx . . $ NUMcombinations(f630275aa3e30a1e). (ncand, ntrack));
497 if (ncomb > 10'000'000) Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"(NUM_viterbi_multi:) "
498 U"Unrealistically high number of combinations (", ncomb, U").");
499 parm. ntrackcxx . . $ parm2#ntrack. = ntrack;
500 parm. ncombcxx . . $ parm2#ncomb. = ncomb;
501
502 /*
503 For ncand == 5 and ntrack == 3, parm.indices is going to contain:
504 1 2 3
505 1 2 4
506 1 2 5
507 1 3 4
508 1 3 5
509 1 4 5
510 2 3 4
511 2 3 5
512 2 4 5
513 3 4 5
514 */
515 autoINTMATcxx . . $ autoINTMAT# indices = zero_INTMATcxx . . $ zero_INTMAT(4cdd710bd00a100c). (ncomb, ntrack);
516 autoINTVECcxx . . $ autoINTVEC# icand = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (ntrack); // start out with "1 2 3"
517 integercxx . . $ integer# jcomb = 0;
518 for (;;) {
519 jcomb ++;
520 for (integercxx . . $ integer# itrack = 1; itrack <= ntrack; itrack ++)
521 indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).jcomb] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrack] = icand [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).itrack];
522 integercxx . . $ integer# itrack = ntrack;
523 for (; itrack >= 1; itrack --) {
524 if (++ icand [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).itrack] <= ncand - (ntrack - itrack)) {
525 for (integercxx . . $ integer# jtrack = itrack + 1; jtrack <= ntrack; jtrack ++)
526 icand [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).jtrack] = icand [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).itrack] + jtrack - itrack;
527 break;
528 }
529 }
530 if (itrack == 0) break;
531 }
532 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (jcomb == ncomb);
533 parm. indicescxx . . $ parm2#indices. =cxx . . $ matrix#`operator=`(2dbd85345837395a). indices.getcxx . . $ automatrix#get(b2b81450b5bdda66).();
534 parm. getLocalCostcxx . . $ parm2#getLocalCost. = getLocalCost;
535 parm. getTransitionCostcxx . . $ parm2#getTransitionCost. = getTransitionCost;
536 parm. putResultcxx . . $ parm2#putResult. = putResult;
537 parm. closurecxx . . $ parm2#closure. = closure;
538 NUM_viterbicxx . . $ NUM_viterbi(d75934b11c111a17). (nframe, ncomb, getNumberOfCandidates_ncxx . . $ getNumberOfCandidates_n(9d83663a1222538d)., getLocalCost_ncxx . . $ getLocalCost_n(e2193e750319682c)., getTransitionCost_ncxx . . $ getTransitionCost_n(557e8ff0ab70c1a1)., putResult_ncxx . . $ putResult_n(fefdcffd337bd722)., & parm);
539 }
540
541 /* End of file NUMinterpol.cpp */