pub struct EntropyConstrainedQuantizer { /* private fields */ }Expand description
Lagrangian Rate-Distortion optimal scalar quantizer.
Minimizes D + λ·R where D is MSE distortion and R is empirical entropy.
Implements an entropy-regularized Lloyd-Max algorithm:
- Initialize bin edges from equal-mass (percentile) split of the signal.
- Iterate:
- Centroid update: recon[i] = mean of samples assigned to bin i.
- Entropy-regularized edge update:
edge[i] = 0.5·(recon[i-1]+recon[i]) + (λ/(recon[i]-recon[i-1]))·(ln p[i-1] − ln p[i]) - Clamp edges to be strictly monotonic.
- Recompute empirical probabilities.
- Evaluate
cost = D + λ·Rand stop when Δcost < tol.
Implementations§
Source§impl EntropyConstrainedQuantizer
impl EntropyConstrainedQuantizer
Sourcepub fn new(
bin_edges: Vec<f32>,
reconstruction_values: Vec<f32>,
lambda: f32,
) -> Self
pub fn new( bin_edges: Vec<f32>, reconstruction_values: Vec<f32>, lambda: f32, ) -> Self
Construct directly from pre-computed edges and reconstruction values.
Uniform prior probabilities are assumed until fit_lagrangian is called.
Sourcepub fn fit_lagrangian(
signal: &Array1<f32>,
num_levels: usize,
lambda: f32,
max_iters: usize,
tol: f32,
) -> TokenizerResult<Self>
pub fn fit_lagrangian( signal: &Array1<f32>, num_levels: usize, lambda: f32, max_iters: usize, tol: f32, ) -> TokenizerResult<Self>
Fit ECQ via entropy-regularized Lloyd-Max iteration.
Returns Err if num_levels < 2 or the signal is too short.
§Arguments
signal– Input signal samples.num_levels– Number of quantization bins (≥ 2).lambda– Lagrange multiplier; larger → more compression, less fidelity.max_iters– Maximum Lloyd-Max iterations.tol– Convergence threshold on|Δcost|.
Sourcepub fn encode_compressed(
&self,
signal: &Array1<f32>,
) -> TokenizerResult<(Vec<u8>, u64)>
pub fn encode_compressed( &self, signal: &Array1<f32>, ) -> TokenizerResult<(Vec<u8>, u64)>
Compress signal using Huffman coding built from the fitted bin
probabilities.
Returns (compressed_bytes, symbol_count). The symbol_count is
needed for lossless decompression.
Sourcepub fn decode_compressed(
&self,
bytes: &[u8],
_symbol_count: u64,
) -> TokenizerResult<Array1<f32>>
pub fn decode_compressed( &self, bytes: &[u8], _symbol_count: u64, ) -> TokenizerResult<Array1<f32>>
Decompress bytes produced by encode_compressed back to a signal.
The symbol_count must match the value returned by encode_compressed.
Sourcepub fn fit_with_target_rate(
signal: &Array1<f32>,
num_levels: usize,
target_bpp: f64,
max_outer_iters: usize,
) -> TokenizerResult<Self>
pub fn fit_with_target_rate( signal: &Array1<f32>, num_levels: usize, target_bpp: f64, max_outer_iters: usize, ) -> TokenizerResult<Self>
Fit ECQ using binary search over λ to hit a target entropy rate.
§Arguments
signal– Training signal.num_levels– Number of quantization bins.target_bpp– Desired bits per symbol.max_outer_iters– Number of λ bisection steps.
Sourcepub fn compute_entropy_rate(&self, signal: &Array1<f32>) -> f64
pub fn compute_entropy_rate(&self, signal: &Array1<f32>) -> f64
Compute empirical Shannon entropy (bits/symbol) of the signal under this quantizer’s bin partition.
Sourcepub fn empirical_distortion(&self, signal: &Array1<f32>) -> f64
pub fn empirical_distortion(&self, signal: &Array1<f32>) -> f64
Compute mean-squared distortion of the signal under this quantizer.
Sourcepub fn reconstruction_values(&self) -> &[f32]
pub fn reconstruction_values(&self) -> &[f32]
Return a reference to the reconstruction values.
Sourcepub fn target_bits_per_symbol(&self) -> Option<f64>
pub fn target_bits_per_symbol(&self) -> Option<f64>
Return the optional target bits-per-symbol set by fit_with_target_rate.
Trait Implementations§
Source§impl SignalTokenizer for EntropyConstrainedQuantizer
impl SignalTokenizer for EntropyConstrainedQuantizer
Source§fn encode(&self, signal: &Array1<f32>) -> TokenizerResult<Array1<f32>>
fn encode(&self, signal: &Array1<f32>) -> TokenizerResult<Array1<f32>>
Source§fn decode(&self, tokens: &Array1<f32>) -> TokenizerResult<Array1<f32>>
fn decode(&self, tokens: &Array1<f32>) -> TokenizerResult<Array1<f32>>
Source§fn vocab_size(&self) -> usize
fn vocab_size(&self) -> usize
Auto Trait Implementations§
impl Freeze for EntropyConstrainedQuantizer
impl RefUnwindSafe for EntropyConstrainedQuantizer
impl Send for EntropyConstrainedQuantizer
impl Sync for EntropyConstrainedQuantizer
impl Unpin for EntropyConstrainedQuantizer
impl UnsafeUnpin for EntropyConstrainedQuantizer
impl UnwindSafe for EntropyConstrainedQuantizer
Blanket Implementations§
Source§impl<T> BatchTokenizer for Twhere
T: SignalTokenizer,
impl<T> BatchTokenizer for Twhere
T: SignalTokenizer,
Source§fn encode_batch(&self, signals: &Array2<f32>) -> TokenizerResult<Array2<f32>>
fn encode_batch(&self, signals: &Array2<f32>) -> TokenizerResult<Array2<f32>>
Source§fn decode_batch(&self, tokens: &Array2<f32>) -> TokenizerResult<Array2<f32>>
fn decode_batch(&self, tokens: &Array2<f32>) -> TokenizerResult<Array2<f32>>
Source§fn encode_batch_padded_to(
&self,
signals: &[Array1<f32>],
target_len: usize,
) -> TokenizerResult<Array2<f32>>
fn encode_batch_padded_to( &self, signals: &[Array1<f32>], target_len: usize, ) -> TokenizerResult<Array2<f32>>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more