07/03/2017
A rather long time ago, I wrote a blog entry on branchless equivalents of simple functions such as sex, abs, min, max. The Sing EXtension instruction propagates the sign bit in the upper bits, and is typically used in the promotion of, say, a 16 bits signed value into a 32 bits variable.

But this time, I needed something a bit different: I only wanted the sign-extended part. Could I do much better than last time? Turns out, the compiler has a mind of its own.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, C, C-plus-plus, C99, CPU Architecture, hacks | Tagged: abs, g++, grumpy cat, max, min, sex, sign extension |
Permalink
Posted by Steven Pigeon
21/02/2017
Scanning documents or books without expensive hardware and commercial software can be tricky. This week, I give you the script I use to clean up a scanned image (and eventually assemble many of them into a single PDF document).

Read the rest of this entry »
2 Comments |
Bash (Shell), hacks | Tagged: Document, ImageMagick, Scanning, thresholding, Unsharp, Unsharp Mask |
Permalink
Posted by Steven Pigeon
14/02/2017
This week, something short. To run tests, I needed a selection of WAV files. Fortunately for me, I’ve got literally thousands of FLAC files lying around on my computer—yes, I listen to music when I code. So I wrote a simple script that randomly chooses a number of file from a directory tree (and not a single directory) and transcode them from FLAC to WAV. Also very fortunately for me, Bash and the various GNU/Linux utilities make writing a script for this rather easy.

Read the rest of this entry »
Leave a Comment » |
Bash (Shell), hacks | Tagged: bash, DJ Champion, Find, FLAC, head, pipe, script, sort, subshell, WAV |
Permalink
Posted by Steven Pigeon
07/02/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.

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 »
1 Comment |
algorithms, bit twiddling, data compression, hacks | Tagged: alpha-law, companding, ISDN, mu-law, noise, sound, spectrogram, voice, white noise |
Permalink
Posted by Steven Pigeon
31/01/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.

Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, hacks | Tagged: 16 bits, 24 bits, 65793, 8 bits, bits per samples, Samples, sound, WAV |
Permalink
Posted by Steven Pigeon
24/01/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 »
5 Comments |
algorithms, C, C-plus-plus, C99, hacks | Tagged: float, IEEE 754, Interview, std::accumulate, std::sort |
Permalink
Posted by Steven Pigeon
10/01/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 »
19 Comments |
algorithms, bit twiddling, C, C-plus-plus, hacks | Tagged: C++11, compile-time, constexpr, hash, hash function, Switch/case |
Permalink
Posted by Steven Pigeon
06/12/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.

Read the rest of this entry »
3 Comments |
algorithms, data structures, hacks, programming | Tagged: Box, Box-Drawing, Pretty-Printing, recursion, terminal, Tree, Unicode |
Permalink
Posted by Steven Pigeon
22/11/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 »
Leave a Comment » |
hacks | Tagged: histogram, Mathematica, time, tweets, Twitter |
Permalink
Posted by Steven Pigeon