Hi everyone,
is there some kind of web-resource with c/c++ source-code for common
dithering algorithms out there?
Best regards,
Tobias
Dithering algorithms
-
- KVRist
- 127 posts since 26 Apr, 2002 from Germany
-
- KVRian
- 709 posts since 16 Dec, 2005 from Novato, California, USA
There are loads of good reading on the subject, try this:
http://www.users.qwest.net/~volt42/cade ... lained.pdf
To answer you question, try these:
http://www.musicdsp.org/archive.php?classid=5#61
http://www.musicdsp.org/archive.php?classid=5#77
http://www.musicdsp.org/archive.php?classid=5#95 (another approach)
http://www.musicdsp.org/archive.php?classid=5#121
The simplest one doesn't need much explanation, just add a noise that is 1-bit loud in the target bit-depth, that will make the quantization noise loose it's dependency on the program material.
More advanced approaches tries to shape the noise to make it less hearable (ie. high-pass filter it, to put it above hearable)
Assuming 32bit float to 16bit fixed, the simplest pseudo-code would be:
After the dithering noise is added, you convert to fixed as usual.
If you want to go fancier than that, adjust your dithernoise() function...
A note on converting to fixedpoint though... if you are converting to signed using the normal c-casts, you will have a ugly problem around 0, since c clamps toward zero (both -.9 and .9 becomes 0). The simplest way to fix is to use roundf(). There are faster ways I won't go in to right now.
Jonas
http://www.users.qwest.net/~volt42/cade ... lained.pdf
To answer you question, try these:
http://www.musicdsp.org/archive.php?classid=5#61
http://www.musicdsp.org/archive.php?classid=5#77
http://www.musicdsp.org/archive.php?classid=5#95 (another approach)
http://www.musicdsp.org/archive.php?classid=5#121
The simplest one doesn't need much explanation, just add a noise that is 1-bit loud in the target bit-depth, that will make the quantization noise loose it's dependency on the program material.
More advanced approaches tries to shape the noise to make it less hearable (ie. high-pass filter it, to put it above hearable)
Assuming 32bit float to 16bit fixed, the simplest pseudo-code would be:
Code: Select all
// totally un-tested code...
// return between (-1,1] (supposed to at least)
float frand() { return (rand() / (RAND_MAX * .5f)) - 1.f; }
// return small values
float dithernoise() { return frand() / (1<<16) }
void process( float* out, int count )
{
for ( count ; --count, ++out )
*out += dithernoise();
}
If you want to go fancier than that, adjust your dithernoise() function...
A note on converting to fixedpoint though... if you are converting to signed using the normal c-casts, you will have a ugly problem around 0, since c clamps toward zero (both -.9 and .9 becomes 0). The simplest way to fix is to use roundf(). There are faster ways I won't go in to right now.
Jonas
