Skip to main content

hybrid_phi/
quantized_memory.rs

1//! Quantized φ-memory: lossy φ-based encoding with configurable step
2//! Useful for compressing or storing signal "shadows"
3
4use crate::core::{hybrid_phi_approximate, hybrid_phi_inverse};
5
6/// Encode with quantization: round(approx / step) * step
7pub fn phi_quantized_encode(w: f64, n: usize, step: f64) -> f64 {
8    let approx = hybrid_phi_approximate(w, n);
9    (approx / step).round() * step
10}
11
12/// Decode quantized φ-code
13pub fn phi_quantized_decode(quantized: f64, n: usize) -> f64 {
14    hybrid_phi_inverse(quantized, n)
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn test_quantized_phi_memory() {
23        let values = [-1000.0, -42.0, -1.0, 0.0, 1.0, 42.0, 123.456, 999.99];
24        let n = 10;
25        let step = 0.01; // simulate compression
26
27        for &w in &values {
28            let q = phi_quantized_encode(w, n, step);
29            let recovered = phi_quantized_decode(q, n);
30            let err = (w - recovered).abs();
31            assert!(err < step * 1.5, "w = {}, recovered = {}, err = {:.3e}", w, recovered, err);
32        }
33    }
34}