code wiki / _hdl_build / nx_vq_dred_gate.nx
nx_vq_dred_gate.nx source
↩ module page · 95 lines · 5923 B
1// nx_vq_dred_gate.nx -- proves the VQ moonshot rung (nx_vq): vector-quantising the LPC reflection envelope to a codebook
2// index collapses the DRED redundancy bitrate toward the 12-32 kb/s neural target, with bounded distortion. Builds a
3// codebook from period-varied LPC analyses (the classical stand-in for a trained codebook), VQ-encodes an in-codebook
4// vector (exact) and a held-out vector (generalisation), and measures bits + distortion + the DRED tail at VQ rates.
5import "nx_syscalls_x86_64.nx"
6import "nx_lpc_autocorr.nx"
7import "nx_lpc_levinson.nx"
8import "nx_vq.nx"
9import "nx_gate_verdict.nx"
10
11func 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 }
12func g_pn(v: i64) -> i64 {
13 let b: *u8 = sys_mmap(28); var x: i64 = v
14 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x }
15 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 }
16 var d: i64=0; var y: i64=x
17 while y>0 { d=d+1; y=y/10 }
18 var i: i64=d-1; y=x
19 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
20 sys_write(1,b,d); return 0
21}
22func g_check(name: *u8, cond: i64) -> i64 {
23 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
24 g_puts(name); g_puts("\n" as *u8); return cond
25}
26func st16(buf: *u8, idx: i64, v: i64) -> i64 { var x: i64 = v; if x < 0 { x = x + 65536 } buf[idx*2] = x & 0xff; buf[idx*2+1] = (x >> 8) & 0xff; return 0 }
27func ld64(buf: *u8, byteoff: i64) -> i64 { var v: i64 = 0; var b: i64 = 0; while b < 8 { v = v | (buf[byteoff + b] << (b*8)); b = b + 1 } return v }
28
29func fill_parabola(s: *u8, n: i64, period: i64) -> i64 { var i: i64=0; while i<n { let p: i64 = i % period; st16(s, i, 40 + p*(period-p)*2); i=i+1 } return 0 }
30func fill_triangle(s: *u8, n: i64, period: i64) -> i64 { var i: i64=0; while i<n { let p: i64 = i % period; var tri: i64 = p; if p*2 > period { tri = period - p } st16(s, i, 40 + tri*6); i=i+1 } return 0 }
31
32func main() -> i64 {
33 g_puts("nx_vq DRED gate (VQ the reflection envelope -> DRED bitrate toward neural target, MEASURED)\n" as *u8)
34 var pass: i64 = 0; var total: i64 = 0
35 let n: i64 = 64; let order: i64 = 10; let K: i64 = 16
36
37 let s_pcm: *u8 = sys_mmap(n*2)
38 let R: *u8 = sys_mmap((order+1)*8)
39 let kb: *u8 = sys_mmap((order+1)*8)
40 let ab: *u8 = sys_mmap((order+1)*8)
41 let eb: *u8 = sys_mmap((order+1)*8)
42 let cb: *i64 = sys_mmap(K*order*8) as *i64 // codebook: K entries x order coeffs
43 let tv: *i64 = sys_mmap(order*8) as *i64
44
45 // build the codebook: K period-varied parabola signals -> their reflection envelopes (reuse the scratch buffers)
46 var k: i64 = 0
47 while k < K {
48 fill_parabola(s_pcm, n, 8 + k)
49 nx_lpc_autocorr(s_pcm, n, order, R)
50 nx_lpc_levinson(R, order, kb, ab, eb)
51 var i: i64 = 0
52 while i < order { cb[k*order + i] = ld64(kb, i*8); i = i + 1 }
53 k = k + 1
54 }
55
56 // test 1: an IN-codebook vector (parabola period 12 = entry index 4) -> exact match
57 fill_parabola(s_pcm, n, 12)
58 nx_lpc_autocorr(s_pcm, n, order, R); nx_lpc_levinson(R, order, kb, ab, eb)
59 var i: i64 = 0; while i < order { tv[i] = ld64(kb, i*8); i = i + 1 }
60 let idx1: i64 = vq_encode(tv, cb, K, order)
61 let dist1: i64 = vq_dist2(tv, cb, idx1, order)
62
63 // test 2: a HELD-OUT vector (triangle period 12, a shape not in the codebook) -> nearest entry
64 fill_triangle(s_pcm, n, 12)
65 nx_lpc_autocorr(s_pcm, n, order, R); nx_lpc_levinson(R, order, kb, ab, eb)
66 i = 0; while i < order { tv[i] = ld64(kb, i*8); i = i + 1 }
67 let idx2: i64 = vq_encode(tv, cb, K, order)
68 let dist_chosen: i64 = vq_dist2(tv, cb, idx2, order)
69 let dist_far: i64 = vq_dist2(tv, cb, 15, order) // a deliberately far entry (period 23)
70
71 let vqb: i64 = vq_index_bits(K) // bits per vector with VQ
72 let i8b: i64 = order * 8 // bits per vector with scalar i8
73 // DRED tail (10-frame redundancy): i8 = 10*(order i8 + 1 energy) bytes ; VQ = 10*(vqb index + 8 energy) bits
74 let i8_tail: i64 = 10 * (order + 1)
75 let vq_tail: i64 = (10 * (vqb + 8) + 7) / 8
76
77 g_puts(" [measure] in-codebook: idx=" as *u8); g_pn(idx1); g_puts(" dist=" as *u8); g_pn(dist1); g_puts(" held-out: idx=" as *u8); g_pn(idx2); g_puts(" dist=" as *u8); g_pn(dist_chosen); g_puts(" (far-entry dist=" as *u8); g_pn(dist_far); g_puts(")\n" as *u8)
78 g_puts(" [measure] bits/vector: VQ=" as *u8); g_pn(vqb); g_puts(" vs scalar-i8=" as *u8); g_pn(i8b); g_puts(" DRED tail: VQ=" as *u8); g_pn(vq_tail); g_puts("B (~" as *u8); g_pn(vq_tail*50*8/1000); g_puts(" kb/s) vs i8=" as *u8); g_pn(i8_tail); g_puts("B (~44 kb/s); neural target 12-32 kb/s\n" as *u8)
79
80 pass = pass + g_check("VQ finds the exact codebook entry for an in-codebook vector (dist 0)" as *u8, dist1 == 0); total=total+1
81 pass = pass + g_check("VQ slashes bits/vector (codebook index vs scalar-i8)" as *u8, vqb * 4 < i8b); total=total+1
82 pass = pass + g_check("held-out vector maps to a genuinely-near entry (>= 2x closer than a far entry)" as *u8, dist_chosen * 2 <= dist_far); total=total+1
83 pass = pass + g_check("VQ DRED tail bitrate << i8 (into the neural 12-32 kb/s band)" as *u8, vq_tail * 3 < i8_tail); total=total+1
84
85 g_puts("---- vq dred gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
86 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
87 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
88 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
89 let ctr__dry: *i64 = gv_ctr()
90 ctr__dry[0] = pass
91 ctr__dry[1] = total
92 let rc__dry: i64 = gv_verdict("VQ-DRED-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
93 sys_exit(rc__dry)
94 return rc__dry
95}