Search code examples
cquantization

Does C have a Quantization function?


I have a buffer with many positive 16bit values (which are stored as doubles) that I would like to quantize to 8bit (0-255 values).

According to Wikipedia the process would be:

  • Normalize 16 bit values. I.e. find the largest and divide with this.
  • Use the Q(x) formula with M=8.

So I wonder, if C have a function that can do this quantization, or does anyone know of a C implementation that I could use?

Lots of love, Louise


Solution

  • Assuming the value d is in the interval [0.0, max]:

    unsigned char quantize(double d, double max)
    {
        return (unsigned char)((d / max) * 255.0);
    }
    

    I'm not sure what you mean by "16-bit values;" double precision values are 64-bit on any system using IEEE-754. However, if you have values of another numeric type, the process is effectively the same.