Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C have a Quantization function?

Tags:

c

quantization

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

like image 792
Louise Avatar asked Dec 08 '25 10:12

Louise


1 Answers

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.

like image 151
James McNellis Avatar answered Dec 10 '25 10:12

James McNellis