Braids quantizer: why does it check two values below the upper bound?

Hi, all – I’ve been studying the Braids quantizer code and I’m wondering if anyone here can explain a very specific thing I don’t understand: why, after running std::upper_bound to find the nearest pitch that’s larger than the pitch to be quantized, does it then (at line 86) check two other values before/below that pitch to see if they’re closer? Wouldn’t either the value (codeword) at upper_bound_index or the one at upper_bound_index - 1 always be closer to the input pitch than the one at upper_bound_index - 2?

I’ve read enough of Émilie’s code to be sure she wrote it this way for a reason, but I haven’t figured it out so far.

Thank you!

1 Like

Frankly, I don’t know, I just know that this code was written in half an hour because it was a feature request… Usually features that are an afterthought aren’t as thoroughly implemented and tested as the main stuff…

I don’t think things will break if you replace the 2 by a 1. Maybe I wrote first code that searched between upper_bound_index -2 and < upper_bound_index ; then realized upper_bound_index had to be <= and forgot to replace the 2 by a 1.

(Note to people who might casually read this: It’s not causing any bug, just that it’s wasting a couple of CPU cycles searching the best among items i, i+1, i+2 when the best item is always either i+1 or i+2).


ha! Well the result of your half hour’s afterthought has been very, very useful, so thank you for that, and for taking the time to answer my question.

(And yeah, it works perfectly, of course – I just wondered if there was some obscure bit of computer science that made it more efficient to iterate over those three values instead of just two, or… something like that.)