code wiki / _hdl_build / nx_resid_entropy_gate.nx
nx_resid_entropy_gate.nx source
↩ module page · 93 lines · 4740 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"
8import "nx_gate_verdict.nx"
9
10func 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 }
11func g_pn(v: i64) -> i64 {
12 let b: *u8 = sys_mmap(28); var x: i64 = v
13 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x }
14 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 }
15 var d: i64=0; var y: i64=x
16 while y>0 { d=d+1; y=y/10 }
17 var i: i64=d-1; y=x
18 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
19 sys_write(1,b,d); return 0
20}
21func g_check(name: *u8, cond: i64) -> i64 {
22 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
23 g_puts(name); g_puts("\n" as *u8); return cond
24}
25
26func main() -> i64 {
27 g_puts("nx_resid_entropy gate (range-code the LPC residual, MEASURED q/bit)\n" as *u8)
28 var pass: i64 = 0; var total: i64 = 0
29 let n: i64 = 256
30 let ALPHA: i64 = 17 // residual symbols for |r| up to 8 (re_zz(8)=16)
31
32 let freq: *i64 = sys_mmap(ALPHA*8) as *i64
33 let cum: *i64 = sys_mmap((ALPHA+1)*8) as *i64
34 let ft: i64 = re_build_model(ALPHA, freq, cum)
35
36 // realistic Laplacian residual: magnitude counts halve (128 zeros, 64 ±1, 32 ±2, ...) -- the shape of real LPC residuals
37 let resid: *i64 = sys_mmap(n*8) as *i64
38 var idx: i64 = 0
39 var mag: i64 = 0
40 while idx < n {
41 if mag > 8 { mag = 8 }
42 var cnt: i64 = 128 >> mag
43 if cnt < 1 { cnt = 1 }
44 var k: i64 = 0
45 while k < cnt {
46 if idx < n {
47 if mag == 0 { resid[idx] = 0 } else { if (k & 1) == 0 { resid[idx] = mag } else { resid[idx] = 0 - mag } }
48 idx = idx + 1
49 }
50 k = k + 1
51 }
52 mag = mag + 1
53 if mag > 8 { if idx < n { resid[idx] = 0; idx = idx + 1 } }
54 }
55
56 // range-code + round-trip
57 let out: *u8 = sys_mmap(2048) as *u8
58 let rc_bytes: i64 = re_encode(resid, n, ALPHA, cum, ft, out, 2048)
59 let dec: *i64 = sys_mmap(n*8) as *i64
60 re_decode(out, rc_bytes, n, ALPHA, cum, ft, dec)
61 var exact: i64 = 1
62 var i: i64 = 0
63 while i < n { if dec[i] != resid[i] { exact = 0 } i = i + 1 }
64
65 // fixed N-bit baseline (ceil(log2(17)) = 5 bits/sample) and the Shannon entropy bound from the actual histogram
66 let fixed_bytes: i64 = (n * 5 + 7) / 8
67 let hist: *i64 = sys_mmap(ALPHA*8) as *i64
68 i = 0; while i < ALPHA { hist[i] = 0; i = i + 1 }
69 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 }
70 var ent_bits_q16: i64 = 0 // sum cnt * log2(n/cnt) in Q16
71 i = 0
72 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 }
73 let entropy_bytes: i64 = (ent_bits_q16 >> 16) / 8
74
75 g_puts(" [measure] residual n=" as *u8); g_pn(n); g_puts(" samples: range-coded=" as *u8); g_pn(rc_bytes)
76 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)
77 g_puts(" [measure] bitrate cut vs fixed = " as *u8); g_pn((fixed_bytes - rc_bytes) * 100 / fixed_bytes); g_puts("%\n" as *u8)
78
79 pass = pass + g_check("residual round-trips LOSSLESSLY through the range coder" as *u8, exact); total=total+1
80 pass = pass + g_check("range-coded residual < fixed-5bit (real q/bit cut)" as *u8, rc_bytes < fixed_bytes); total=total+1
81 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
82
83 g_puts("---- resid_entropy gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
84 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
85 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
86 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
87 let ctr__dry: *i64 = gv_ctr()
88 ctr__dry[0] = pass
89 ctr__dry[1] = total
90 let rc__dry: i64 = gv_verdict("RESID-ENTROPY-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
91 sys_exit(rc__dry)
92 return rc__dry
93}