Class Quantizer
Packs float readings to a fixed precision, for a link charging per byte.
public sealed class Quantizer
- Inheritance
-
Quantizer
- Inherited Members
Examples
var quantizer = new Quantizer(100); // keep two decimal places
byte[] packed = quantizer.Encode(new[] { 20.0f, 20.1f, 20.2f });
float[] readings = quantizer.Decode(packed); // to within 0.01
Remarks
Full-precision floats are wasteful when a reading is only meaningful to two decimal places. Rounding to that precision and delta-encoding the batch turns a day of samples into something a metered link can afford.
Constructors
Quantizer(float)
Creates a quantizer at the given precision.
public Quantizer(float scale)
Parameters
scalefloatThe multiplier applied before rounding;
100keeps two decimal places. Must be positive and finite.
Exceptions
- ArgumentOutOfRangeException
scaleis not positive and finite.
Methods
Decode(ReadOnlySpan<byte>)
Unpacks a batch, to within this quantizer's precision.
public float[] Decode(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The encoding produced by Encode(ReadOnlySpan<float>) at the same scale.
Returns
- float[]
The readings, in order.
Exceptions
- PamojaException
The buffer is malformed.
Encode(ReadOnlySpan<float>)
Quantizes and packs a batch of readings.
public byte[] Encode(ReadOnlySpan<float> readings)
Parameters
readingsReadOnlySpan<float>The readings, in order.
Returns
- byte[]
The packed encoding.
Exceptions
- PamojaException
The native call failed.