code wiki / _hdl_build / nx_resid_entropy_gate.nx
nx_resid_entropy_gate.nx source
↩ module page · 85 lines · 4293 B
1// nx_resid_entropy_gate.nx -- proves + MEASURES the wired residual entropy coder (nx_resid_entropy): a realistic
2// Laplacian LPC residual is range-coded LOSSLESSLY, far below a fixed N-bit/sample representation and near the Shannon
3// entropy bound. This is the voice codec's v2 q/bit lever, MEASURED (a real bitrate cut at identical quality), not a
4// vs-Opus claim.
5import "nx_syscalls_x86_64.nx"
6import "nx_resid_entropy.nx"
7import "nx_quality_metric.nx"
8
9func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func g_pn(v: i64) -> i64 {
11 let b: *u8 = sys_mmap(28); var x: i64 = v
12 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x }
13 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 }
14 var d: i64=0; var y: i64=x
15 while y>0 { d=d+1; y=y/10 }
16 var i: i64=d-1; y=x
17 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
18 sys_write(1,b,d); return 0
19}
20func g_check(name: *u8, cond: i64) -> i64 {
21 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
22 g_puts(name); g_puts("\n" as *u8); return cond
23}
24
25func main() -> i64 {
26 g_puts("nx_resid_entropy gate (range-code the LPC residual, MEASURED q/bit)\n" as *u8)
27 var pass: i64 = 0; var total: i64 = 0
28 let n: i64 = 256
29 let ALPHA: i64 = 17 // residual symbols for |r| up to 8 (re_zz(8)=16)
30
31 let freq: *i64 = sys_mmap(ALPHA*8) as *i64
32 let cum: *i64 = sys_mmap((ALPHA+1)*8) as *i64
33 let ft: i64 = re_build_model(ALPHA, freq, cum)
34
35 // realistic Laplacian residual: magnitude counts halve (128 zeros, 64 ±1, 32 ±2, ...) -- the shape of real LPC residuals
36 let resid: *i64 = sys_mmap(n*8) as *i64
37 var idx: i64 = 0
38 var mag: i64 = 0
39 while idx < n {
40 if mag > 8 { mag = 8 }
41 var cnt: i64 = 128 >> mag
42 if cnt < 1 { cnt = 1 }
43 var k: i64 = 0
44 while k < cnt {
45 if idx < n {
46 if mag == 0 { resid[idx] = 0 } else { if (k & 1) == 0 { resid[idx] = mag } else { resid[idx] = 0 - mag } }
47 idx = idx + 1
48 }
49 k = k + 1
50 }
51 mag = mag + 1
52 if mag > 8 { if idx < n { resid[idx] = 0; idx = idx + 1 } }
53 }
54
55 // range-code + round-trip
56 let out: *u8 = sys_mmap(2048) as *u8
57 let rc_bytes: i64 = re_encode(resid, n, ALPHA, cum, ft, out, 2048)
58 let dec: *i64 = sys_mmap(n*8) as *i64
59 re_decode(out, rc_bytes, n, ALPHA, cum, ft, dec)
60 var exact: i64 = 1
61 var i: i64 = 0
62 while i < n { if dec[i] != resid[i] { exact = 0 } i = i + 1 }
63
64 // fixed N-bit baseline (ceil(log2(17)) = 5 bits/sample) and the Shannon entropy bound from the actual histogram
65 let fixed_bytes: i64 = (n * 5 + 7) / 8
66 let hist: *i64 = sys_mmap(ALPHA*8) as *i64
67 i = 0; while i < ALPHA { hist[i] = 0; i = i + 1 }
68 i = 0; while i < n { var z: i64 = re_zz(resid[i]); if z >= ALPHA { z = ALPHA - 1 } hist[z] = hist[z] + 1; i = i + 1 }
69 var ent_bits_q16: i64 = 0 // sum cnt * log2(n/cnt) in Q16
70 i = 0
71 while i < ALPHA { if hist[i] > 0 { ent_bits_q16 = ent_bits_q16 + hist[i] * (qm_log2_q16(n) - qm_log2_q16(hist[i])) } i = i + 1 }
72 let entropy_bytes: i64 = (ent_bits_q16 >> 16) / 8
73
74 g_puts(" [measure] residual n=" as *u8); g_pn(n); g_puts(" samples: range-coded=" as *u8); g_pn(rc_bytes)
75 g_puts(" B vs fixed-5bit=" as *u8); g_pn(fixed_bytes); g_puts(" B vs Shannon-bound=" as *u8); g_pn(entropy_bytes); g_puts(" B\n" as *u8)
76 g_puts(" [measure] bitrate cut vs fixed = " as *u8); g_pn((fixed_bytes - rc_bytes) * 100 / fixed_bytes); g_puts("%\n" as *u8)
77
78 pass = pass + g_check("residual round-trips LOSSLESSLY through the range coder" as *u8, exact); total=total+1
79 pass = pass + g_check("range-coded residual < fixed-5bit (real q/bit cut)" as *u8, rc_bytes < fixed_bytes); total=total+1
80 pass = pass + g_check("range-coded within 30% of the Shannon entropy bound (near-optimal)" as *u8, rc_bytes * 10 <= entropy_bytes * 13); total=total+1
81
82 g_puts("---- resid_entropy gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
83 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
84 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
85}