Skip to main content

Quantizer

Struct Quantizer 

Source
pub struct Quantizer { /* private fields */ }
Expand description

Packs a batch of f32 readings into a compact byte form for a metered link.

A quantizer rounds each reading to a fixed precision - set by the scale, where 100.0 keeps two decimal places - turns it into an integer, and delta-encodes the batch with encode_deltas. This is lossy by exactly the rounding step, which is the right trade for a cheap sensor on an expensive link: a fridge temperature to the nearest hundredth of a degree costs a byte or two per sample instead of four. The same scale must be used to encode and decode.

§Examples

use pamoja_codec::Quantizer;

// Quantize to 0.1 precision and pack a slowly-rising series.
let quantizer = Quantizer::new(10.0);
let readings = [20.0, 20.1, 20.2, 20.3];
let packed = quantizer.encode(&readings);
assert!(packed.len() < readings.len() * 4); // smaller than four bytes per reading

let restored = quantizer.decode(&packed).unwrap();
assert!((restored[2] - 20.2).abs() < 0.05);

Implementations§

Source§

impl Quantizer

Source

pub fn new(scale: f32) -> Self

Creates a quantizer with the given precision scale.

§Arguments
  • scale - the multiplier applied before rounding; 100.0 keeps two decimal places. Must be positive.
§Returns

The quantizer.

Source

pub fn encode(&self, readings: &[f32]) -> Vec<u8>

Quantizes and delta-encodes a batch of readings.

§Arguments
  • readings - the readings to pack, in order.
§Returns

The compact encoding of the batch.

Source

pub fn decode(&self, bytes: &[u8]) -> Result<Vec<f32>>

Decodes a batch back into readings, to within the quantizer’s precision.

§Arguments
  • bytes - the encoding produced by encode with the same scale.
§Returns

The decoded readings, in order.

§Errors

Returns Error::Codec if bytes is malformed.

Trait Implementations§

Source§

impl Clone for Quantizer

Source§

fn clone(&self) -> Quantizer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Quantizer

Source§

impl Debug for Quantizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.