8-bit Audio Companding

February 7, 2017

Computationally inexpensive sound compression is always difficult, at least if you want some quality. One could think, for example, that taking the 8 most significant bits of 16 bits will give us 2:1 (lossy) compression but without too much loss. However, cutting the 8 least significant bits leads to noticeable hissing. However, we do not have to compress linearly, we can apply some transformation, say, vaguely exponential to reconstruct the sound.

ssound-blocks

That’s the idea behind μ-law encoding, or “logarithmic companding”. Instead of quantizing uniformly, we have large (original) values widely spaced but small (original) value, the assumption being that the signal variation is small when the amplitude is small and large when the amplitude is great. ITU standard G.711 proposes the following table:

Read the rest of this entry »


Stretching samples

January 31, 2017

So for an experiment I ended up needing conversions between 8 bits and 16 bits samples. To upscale an 8 bit sample to 16 bits, it is not enough to simply shift it by 8 bits (or multiply it by 256, same difference) because the largest value you get isn’t 65535 but merely 65280. Fortunately, stretching correctly from 8 bit to 16 bit isn’t too difficult, even quite straightforward.

stretching-snorlax

Read the rest of this entry »


Whatever sums your floats

January 24, 2017

While flipping the pages of a “Win this interview” book—just being curious, not looking for a new job—I saw this seemingly simple question: how would you compute the sum of a series of floats contained in a array? The book proceeded with the simple, obvious answer. But… is it that obvious?

Read the rest of this entry »


Strings in C++ Switch/Case statements

January 10, 2017

Something that used to bug me—used to, because I am so accustomed to work around it that I rarely notice the problem—is that in neither C nor C++ you can use strings (const char * or std::string) in switch/case statement. Indeed, the switch/case statement works only on integral values (an enum, an integral type such as char and int, or an object type with implicit cast to an integral type). But strings aren’t of integral types!

In pure C, we’re pretty much done for. The C preprocessor is too weak to help us built compile-time expression out of strings (or, more exactly, const char *), and there’sn’t much else in the language to help us. However, things are a bit different in C++.

Read the rest of this entry »


Pretty Printing a Tree in a Terminal

December 6, 2016

More often than I’d like, simple tasks turn out to be not that simple. For example, displaying (beautifully) a binary tree for debugging purpose in a terminal. Of course, one could still use lots of parentheses, but that does not lend itself to a very fast assessment of the tree. We could, however, use box drawing characters, as in DOS’s goode olde tymes, since they’re now part of the Unicode standard.

tree-01

Read the rest of this entry »


Tweet time!

November 22, 2016

I’ve been using twitter for about five years, and I wondered if my use of it changed over time, and more precisely, linked to my wake/sleep cycle. That’s fortunately kind of simple to check because you can simply request your whole Twitter archive, delivered as a plain CSV File! Let’s see how we can juice it.

Read the rest of this entry »


Square Roots (Part V)

November 15, 2016

Last week we examined the complexity of obtaining k in the decomposition n=2^k+b for some integer n. This week, we’ll have a look at how we can use it to speed-up square root extraction. Recall, we’re interested in k because

2^k \leqslant 2^k+b < 2^{k+1},

with 0 \leqslant b < 2^k, which allows us to get easy bounds on \sqrt{n}. Better, we also have that

\sqrt{2^k} \leqslant \sqrt{2^k+b} \leqslant \sqrt{2^{k+1}},

and we know how to compute \sqrt{2^k}=2\frac{k}{2} (somewhat efficiently! Let’s combine all this and see how it speeds up things.

Read the rest of this entry »