code wiki / _hdl_build / nx_quality_metric_gate.nx

nx_quality_metric_gate.nx source

↩ module page · 61 lines · 3264 B

1// nx_quality_metric_gate.nx -- proves the sovereign PSNR/MSE/RMSE metrics (nx_quality_metric). Native, fast. 2import "nx_syscalls.nx" 3import "nx_quality_metric.nx" 4import "nx_gate_verdict.nx" 5 6func 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 } 7func g_pn(v: i64) -> i64 { 8 let b: *u8 = sys_mmap(28); var x: i64 = v 9 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 } 10 var d: i64=0; var y: i64=x 11 while y>0 { d=d+1; y=y/10 } 12 var i: i64=d-1; y=x 13 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 14 sys_write(1,b,d); return 0 15} 16func g_check(name: *u8, cond: i64) -> i64 { 17 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } 18 g_puts(name); g_puts("\n" as *u8); return cond 19} 20func g_abs(v: i64) -> i64 { if v<0 { return 0-v } return v } 21 22func main() -> i64 { 23 g_puts("nx_quality_metric gate (PSNR/MSE/RMSE, MEASURED)\n" as *u8) 24 var pass: i64 = 0; var total: i64 = 0 25 let n: i64 = 64 26 let a: *u8 = sys_mmap(n); let b: *u8 = sys_mmap(n); let c: *u8 = sys_mmap(n) 27 var i: i64 = 0 28 while i < n { a[i] = (40 + (i % 30)) as u8; b[i] = (a[i] + 10) as u8; c[i] = (a[i] + 5) as u8; i = i + 1 } 29 30 // 1) identical -> MSE 0, PSNR capped 31 let mse0: i64 = qm_mse(a, a, n) 32 let psnr0: i64 = qm_psnr_cdb(a, a, n) 33 pass = pass + g_check("identical frames -> MSE 0, PSNR capped (99 dB)" as *u8, (mse0 == 0) & (psnr0 == 9900)); total=total+1 34 35 // 2) uniform +10 error -> MSE exactly 100, RMSE 10 36 let mse10: i64 = qm_mse(a, b, n) 37 let rmse10: i64 = qm_rmse(a, b, n) 38 g_puts(" [measure] uniform error 10: MSE=" as *u8); g_pn(mse10); g_puts(" RMSE=" as *u8); g_pn(rmse10); g_puts("\n" as *u8) 39 pass = pass + g_check("MSE/RMSE exact (uniform +10 -> MSE 100, RMSE 10)" as *u8, (mse10 == 100) & (rmse10 == 10)); total=total+1 40 41 // 3) PSNR matches the textbook value (MSE 100 -> 28.13 dB = 2813 cdb) 42 let psnr10: i64 = qm_psnr_cdb(a, b, n) 43 g_puts(" [measure] MSE 100 -> PSNR=" as *u8); g_pn(psnr10); g_puts(" cdB (textbook 2813)\n" as *u8) 44 pass = pass + g_check("PSNR within 0.6 dB of textbook (MSE 100 -> ~28.13 dB)" as *u8, g_abs(psnr10 - 2813) <= 60); total=total+1 45 46 // 4) monotone: smaller error (c, +5) -> higher PSNR than larger error (b, +10) 47 let psnr5: i64 = qm_psnr_cdb(a, c, n) 48 g_puts(" [measure] error 5 PSNR=" as *u8); g_pn(psnr5); g_puts(" > error 10 PSNR=" as *u8); g_pn(psnr10); g_puts("\n" as *u8) 49 pass = pass + g_check("PSNR monotone: less distortion -> higher PSNR" as *u8, psnr5 > psnr10); total=total+1 50 51 g_puts("---- quality-metric gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 52 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 53 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 54 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 55 let ctr__dry: *i64 = gv_ctr() 56 ctr__dry[0] = pass 57 ctr__dry[1] = total 58 let rc__dry: i64 = gv_verdict("QUALITY-METRIC-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 59 sys_exit(rc__dry) 60 return rc__dry 61}