Debian Codegraph @

melder/NUMinterpol.cpp

raw
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"
29 #include "../dwsys/NUM2.h"
30
31 double NUM_interpolate_sinc (constVEC const& y, double x, integer maxDepth) {
32 Melder_assert (isdefined (x));
33 const integer midleft = Melder_ifloor (x), midright = midleft + 1;
34 double result = 0.0;
35 if (y.size < 1)
36 return undefined; // there exists no best guess
37 if (x < 1)
38 return y [1]; // offleft: constant extrapolation
39 if (x > y.size)
40 return y [y.size]; // offright: constant extrapolation
41 if (x == midleft)
42 return y [midleft]; // the interpolated curve goes through the points
43 /*
44 1 < x < y.size && x not integer: interpolate.
45 */
46 Melder_clipRight (& maxDepth, midright - 1);
47 Melder_clipRight (& maxDepth, y.size - midleft);
48 if (maxDepth <= NUM_VALUE_INTERPOLATE_NEAREST)
49 return y [Melder_iround (x)]; // possible because we know that 1 <= x <= y.size
50 if (maxDepth == NUM_VALUE_INTERPOLATE_LINEAR)
51 return y [midleft] + (x - midleft) * (y [midright] - y [midleft]);
52 if (maxDepth == NUM_VALUE_INTERPOLATE_CUBIC) {
53 const double yl = y [midleft], yr = y [midright];
54 const double dyl = 0.5 * (yr - y [midleft - 1]), dyr = 0.5 * (y [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 integer left = midright - maxDepth; // as maxDepth <= midright - 1, we know that left >= 1
62 const 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 enumWindowShape { RAISED_COSINE, RAISED_GAUSSIAN };
142 constexpr enumWindowShape WINDOW_SHAPE = enumWindowShape :: RAISED_COSINE;
143 if (WINDOW_SHAPE == 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 = NUMpi / leftDepth;
148 const double sinWindowPhaseStep = sin (windowPhaseStep);
149 const double cosWindowPhaseStep = cos (windowPhaseStep);
150 /*
151 Initialize the phase of the sinc.
152 */
153 double leftPhase = NUMpi * (x - midleft);
154 double halfSinLeftPhase = 0.5 * sin (leftPhase);
155 /*
156 Initialize the phase of the window.
157 */
158 double windowPhase = leftPhase / leftDepth;
159 double sinWindowPhase = sin (windowPhase);
160 double cosWindowPhase = cos (windowPhase);
161 /*
162 Step through the samples.
163 */
164 for (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 [ix] * sincTimesWindow;
170 /*
171 Update the phase of the sinc.
172 */
173 leftPhase += 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 = NUMpi / rightDepth;
192 const double sinWindowPhaseStep = sin (windowPhaseStep);
193 const double cosWindowPhaseStep = cos (windowPhaseStep);
194 /*
195 Initialize the phase of the sinc.
196 */
197 double rightPhase = NUMpi * (midright - x);
198 double halfSinRightPhase = 0.5 * sin (rightPhase);
199 /*
200 Initialize the phase of the window.
201 */
202 double windowPhase = rightPhase / rightDepth;
203 double sinWindowPhase = sin (windowPhase);
204 double cosWindowPhase = cos (windowPhase);
205 /*
206 Step through the samples.
207 */
208 for (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 [ix] * sincTimesWindow;
214 /*
215 Update the phase of the sinc.
216 */
217 rightPhase += 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 == 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 = NUMpi * (x - midleft);
272 double sinLeftPhase = sin (leftPhase);
273 /*
274 Initialize the phase of the window.
275 */
276 double windowPhase = (-12.0 / NUMpi / NUMpi) * sqr (leftPhase / leftDepth);
277 /*
278 Step through the samples.
279 */
280 for (integer ix = midleft; ix >= left; ix --) {
281 /*
282 Accumulate the contribution of this sample to the result.
283 */
284 const double sincTimesWindow = sinLeftPhase / leftPhase * (exp (windowPhase) - exp (-12.0)) / (1.0 + exp (-12.0));
285 result += y [ix] * sincTimesWindow;
286 /*
287 Update the phase of the sinc.
288 */
289 leftPhase += NUMpi;
290 sinLeftPhase = - sinLeftPhase;
291 /*
292 Update the phase of the window.
293 */
294 windowPhase += (-12.0 / NUMpi / NUMpi) * sqr (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 = NUMpi * (midright - x);
306 double sinRightPhase = sin (rightPhase);
307 /*
308 Initialize the phase of the window.
309 */
310 double windowPhase = (-12.0 / NUMpi / NUMpi) * sqr (rightPhase / rightDepth);
311 /*
312 Step through the samples.
313 */
314 for (integer ix = midright; ix <= right; ix ++) {
315 /*
316 Accumulate the contribution of this sample to the result.
317 */
318 const double sincTimesWindow = sinRightPhase / rightPhase * (exp (windowPhase) - exp (-12.0)) / (1.0 + exp (-12.0));
319 result += y [ix] * sincTimesWindow;
320 /*
321 Update the phase of the sinc.
322 */
323 rightPhase += NUMpi;
324 sinRightPhase = - sinRightPhase;
325 /*
326 Update the phase of the window.
327 */
328 windowPhase += (-12.0 / NUMpi / NUMpi) * sqr (rightPhase / rightDepth);
329 }
330 }
331 }
332 return result;
333 }
334
335 /********** Improving extrema **********/
336 #pragma mark Improving extrema
337
338 struct improve_params {
339 integer depth;
340 constVEC y;
341 bool isMaximum;
342 };
343
344 static double improve_evaluate (double x, void *closure) {
345 Melder_assert (isdefined (x));
346 struct improve_params *me = (struct improve_params *) closure;
347 const double y = NUM_interpolate_sinc (my y, x, my depth);
348 return my isMaximum ? - y : y;
349 }
350
351 double NUMimproveExtremum (constVEC const& y, integer ixmid, integer interpolationDepth, double *ixmid_real, bool isMaximum) {
352 struct improve_params params;
353 double result;
354 if (ixmid <= 1) {
355 *ixmid_real = double (1);
356 return y [1];
357 }
358 if (ixmid >= y.size) {
359 *ixmid_real = double (y.size);
360 return y [y.size];
361 }
362 if (interpolationDepth <= NUM_PEAK_INTERPOLATE_NONE) {
363 *ixmid_real = double (ixmid);
364 return y [ixmid];
365 }
366 if (interpolationDepth == NUM_PEAK_INTERPOLATE_PARABOLIC) {
367 const double dy = 0.5 * (y [ixmid + 1] - y [ixmid - 1]);
368 const double d2y = 2 * y [ixmid] - y [ixmid - 1] - y [ixmid + 1];
369 *ixmid_real = ixmid + dy / d2y;
370 return y [ixmid] + 0.5 * dy * dy / d2y;
371 }
372 /*
373 Cubic or sinc interpolation.
374 */
375 params. depth = (
376 interpolationDepth == NUM_PEAK_INTERPOLATE_CUBIC ? NUM_VALUE_INTERPOLATE_CUBIC :
377 interpolationDepth == NUM_PEAK_INTERPOLATE_SINC70 ? NUM_VALUE_INTERPOLATE_SINC70 :
378 NUM_VALUE_INTERPOLATE_SINC700
379 );
380 params. y = y;
381 params. isMaximum = isMaximum;
382 *ixmid_real = NUMminimize_brent (improve_evaluate, ixmid - 1, ixmid + 1, & params, 1e-10, & result);
383 return isMaximum ? - result : result;
384 }
385
386 double NUMimproveMinimum (constVEC const& y, integer ixmid, integer interpolationDepth, double *ixmid_real) {
387 return NUMimproveExtremum (y, ixmid, interpolationDepth, ixmid_real, false);
388 }
389 double NUMimproveMaximum (constVEC const& y, integer ixmid, integer interpolationDepth, double *ixmid_real) {
390 return NUMimproveExtremum (y, ixmid, interpolationDepth, ixmid_real, true);
391 }
392
393 /********** Viterbi **********/
394
395 void NUM_viterbi (
396 integer numberOfFrames, integer maxnCandidates,
397 integer (*getNumberOfCandidates) (integer iframe, void *closure),
398 double (*getLocalCost) (integer iframe, integer icand, void *closure),
399 double (*getTransitionCost) (integer iframe, integer icand1, integer icand2, void *closure),
400 void (*putResult) (integer iframe, integer place, void *closure),
401 void *closure)
402 {
403 autoMAT delta = raw_MAT (numberOfFrames, maxnCandidates);
404 autoINTMAT psi = raw_INTMAT (numberOfFrames, maxnCandidates);
405 autoINTVEC numberOfCandidates = raw_INTVEC (numberOfFrames);
406 for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
407 numberOfCandidates [iframe] = getNumberOfCandidates (iframe, closure);
408 for (integer icand = 1; icand <= numberOfCandidates [iframe]; icand ++)
409 delta [iframe] [icand] = - getLocalCost (iframe, icand, closure);
410 }
411 for (integer iframe = 2; iframe <= numberOfFrames; iframe ++) {
412 for (integer icand2 = 1; icand2 <= numberOfCandidates [iframe]; icand2 ++) {
413 double maximum = -1e308;
414 integer place = 0;
415 for (integer icand1 = 1; icand1 <= numberOfCandidates [iframe - 1]; icand1 ++) {
416 const double value = delta [iframe - 1] [icand1] + delta [iframe] [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_throw (U"Viterbi algorithm cannot compute a track because of weird values.");
425 delta [iframe] [icand2] = maximum;
426 psi [iframe] [icand2] = place;
427 }
428 }
429 /*
430 Find the end of the most probable path.
431 */
432 integer place;
433 double maximum = delta [numberOfFrames] [place = 1];
434 for (integer icand = 2; icand <= numberOfCandidates [numberOfFrames]; icand ++) {
435 if (delta [numberOfFrames] [icand] > maximum)
436 maximum = delta [numberOfFrames] [place = icand];
437 }
438 /*
439 Backtrack.
440 */
441 for (integer iframe = numberOfFrames; iframe >= 1; iframe --) {
442 putResult (iframe, place, closure);
443 place = psi [iframe] [place];
444 }
445 }
446
447 /******************/
448
449 struct parm2 {
450 integer ntrack;
451 integer ncomb;
452 INTMAT indices;
453 double (*getLocalCost) (integer iframe, integer icand, integer itrack, void *closure);
454 double (*getTransitionCost) (integer iframe, integer icand1, integer icand2, integer itrack, void *closure);
455 void (*putResult) (integer iframe, integer place, integer itrack, void *closure);
456 void *closure;
457 };
458
459 static integer getNumberOfCandidates_n (integer iframe, void *closure) {
460 struct parm2 *me = (struct parm2 *) closure;
461 (void) iframe;
462 return my ncomb;
463 }
464 static double getLocalCost_n (integer iframe, integer jcand, void *closure) {
465 struct parm2 *me = (struct parm2 *) closure;
466 double localCost = 0.0;
467 for (integer itrack = 1; itrack <= my ntrack; itrack ++)
468 localCost += my getLocalCost (iframe, my indices [jcand] [itrack], itrack, my closure);
469 return localCost;
470 }
471 static double getTransitionCost_n (integer iframe, integer jcand1, integer jcand2, void *closure) {
472 struct parm2 *me = (struct parm2 *) closure;
473 double transitionCost = 0.0;
474 for (integer itrack = 1; itrack <= my ntrack; itrack ++)
475 transitionCost += my getTransitionCost (iframe,
476 my indices [jcand1] [itrack], my indices [jcand2] [itrack], itrack, my closure);
477 return transitionCost;
478 }
479 static void putResult_n (integer iframe, integer jplace, void *closure) {
480 struct parm2 *me = (struct parm2 *) closure;
481 for (integer itrack = 1; itrack <= my ntrack; itrack ++)
482 my putResult (iframe, my indices [jplace] [itrack], itrack, my closure);
483 }
484
485 void NUM_viterbi_multi (
486 integer nframe, integer ncand, integer ntrack,
487 double (*getLocalCost) (integer iframe, integer icand, integer itrack, void *closure),
488 double (*getTransitionCost) (integer iframe, integer icand1, integer icand2, integer itrack, void *closure),
489 void (*putResult) (integer iframe, integer place, integer itrack, void *closure),
490 void *closure)
491 {
492 struct parm2 parm;
493
494 if (ntrack > ncand) Melder_throw (U"(NUM_viterbi_multi:) "
495 U"Number of tracks (", ntrack, U") should not exceed number of candidates (", ncand, U").");
496 const integer ncomb = Melder_iround (NUMcombinations (ncand, ntrack));
497 if (ncomb > 10'000'000) Melder_throw (U"(NUM_viterbi_multi:) "
498 U"Unrealistically high number of combinations (", ncomb, U").");
499 parm. ntrack = ntrack;
500 parm. 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 autoINTMAT indices = zero_INTMAT (ncomb, ntrack);
516 autoINTVEC icand = to_INTVEC (ntrack); // start out with "1 2 3"
517 integer jcomb = 0;
518 for (;;) {
519 jcomb ++;
520 for (integer itrack = 1; itrack <= ntrack; itrack ++)
521 indices [jcomb] [itrack] = icand [itrack];
522 integer itrack = ntrack;
523 for (; itrack >= 1; itrack --) {
524 if (++ icand [itrack] <= ncand - (ntrack - itrack)) {
525 for (integer jtrack = itrack + 1; jtrack <= ntrack; jtrack ++)
526 icand [jtrack] = icand [itrack] + jtrack - itrack;
527 break;
528 }
529 }
530 if (itrack == 0) break;
531 }
532 Melder_assert (jcomb == ncomb);
533 parm. indices = indices.get();
534 parm. getLocalCost = getLocalCost;
535 parm. getTransitionCost = getTransitionCost;
536 parm. putResult = putResult;
537 parm. closure = closure;
538 NUM_viterbi (nframe, ncomb, getNumberOfCandidates_n, getLocalCost_n, getTransitionCost_n, putResult_n, & parm);
539 }
540
541 /* End of file NUMinterpol.cpp */