Chapter 17 of 22 Part IV: Information Processing

The Fingerprint of Sound

5 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

Tone (Ch. 17 Example 1)

File: block-01.js (byte-identical) · 582 bytes · SHA-256: a8b7066764e8…
Nyquist Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function tone(f, fs, n, amplitude = 1) {
  return Array.from({length:n}, (_,i) =>
    amplitude * Math.sin(2*Math.PI*f*i/fs));
}
function spectrum(x, fs) {
  const n = x.length, result = [];
  for (let k=0; k<=Math.floor(n/2); k++) {
    let re=0, im=0;
    for (let i=0; i<n; i++) {
      const a=2*Math.PI*k*i/n;
      re += x[i]*Math.cos(a);
      im -= x[i]*Math.sin(a);
    }
    result.push({hz:k*fs/n,
      strength:Math.hypot(re,im)/n});
  }
  return result;
}
function dominant(x, fs) {
  return spectrum(x,fs).slice(1)
    .reduce((a,b)=>a.strength>=b.strength?a:b).hz;
}
Download .js

Fingerprint (Ch. 17 Example 2)

File: block-02.js (byte-identical) · 618 bytes · SHA-256: eafed5219b51…
Nyquist Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function fingerprint(signal, fs, size=128) {
  const keys=[];
  for(let i=0;i+size<=signal.length;i+=size) {
    keys.push(dominant(signal.slice(i,i+size),fs));
  }
  return keys;
}
function match(query, catalog) {
  if(!query.length) return [];
  const scores=[];
  for(const [name, track] of Object.entries(catalog)) {
    let best=0, offset=-1;
    for(let i=0;i+query.length<=track.length;i++) {
      const score=query.filter((f,j)=>f===track[i+j]).length;
      if(score>best){best=score;offset=i;}
    }
    scores.push({name,offset,score:best/query.length});
  }
  return scores.sort((a,b)=>b.score-a.score);
}
Download .js

Catalog — The Fingerprint of Sound (Example 3)

File: block-03.js (byte-identical) · 119 bytes · SHA-256: 4edc37c7438f…
Nyquist Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const catalog = {A:[128,256,192,320,128,384],
 B:[128,192,128,256,384,320]};
console.log(match([192,320,128],catalog));
Download .js

Cancel (Ch. 17 Example 4)

File: block-04.js (byte-identical) · 184 bytes · SHA-256: de67b2379559…
Nyquist Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function cancel(noise, gain=1, phase=0) {
  return noise.map((v,i)=>v+gain*Math.sin(i*2*Math.PI/32+phase));
}
function rms(x) {
  return Math.sqrt(x.reduce((s,v)=>s+v*v,0)/x.length);
}
Download .js

Noise — The Fingerprint of Sound (Example 5)

File: block-05.js (byte-identical) · 148 bytes · SHA-256: 2e7c67ddccff…
Nyquist Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const noise = tone(1,32,128);
const remaining = cancel(noise,1,Math.PI);
console.log(rms(noise).toFixed(3));
console.log(rms(remaining).toFixed(3));
Download .js