// Undercut tuner: adjusts bar length L, undercut depth d and undercut length a so the first // three bending partials land on f1 = note, f2 = r2·f1, f3 = r3·f1. Partials whose target // lies above the cutoff are left free (as makers do on the top bars); a tiny pull toward // the nominal arch then keeps the problem well posed. Levenberg–Marquardt on log errors. import { bendFreqs } from './fe.js'; import { maxDepth, noteFreq, nominalDesign } from './bar.js'; export const DEFAULT_TARGETS = { r2: 4, r3: 10, fCut: 9000 }; const A_LO = 0.2, A_HI = 0.95; const logit = (u) => Math.log(u / (1 - u)); const sigm = (t) => 1 / (1 + Math.exp(-t)); const clampU = (u) => Math.min(1 - 1e-6, Math.max(1e-6, u)); function toParams(b) { return [Math.log(b.L), logit(clampU(b.d / maxDepth(b))), logit(clampU((b.a - A_LO) / (A_HI - A_LO)))]; } function fromParams(base, t) { const b = { ...base, L: Math.exp(t[0]) }; b.d = sigm(t[1]) * maxDepth(b); b.a = A_LO + (A_HI - A_LO) * sigm(t[2]); return b; } export function partialTargets(note, targets) { const f1 = noteFreq(note); return { f: [f1, f1 * targets.r2, f1 * targets.r3], on: [true, f1 * targets.r2 <= targets.fCut, f1 * targets.r3 <= targets.fCut] }; } // cents error of each partial against its target export function partialErrors(f, note, targets) { const t = partialTargets(note, targets); return f.map((fi, i) => ({ f: fi, target: t.f[i], on: t.on[i], cents: 1200 * Math.log2(fi / t.f[i]) })); } function residuals(t, base, mat, tg, nominal, ne) { const b = fromParams(base, t); const f = bendFreqs(b, mat, 3, ne); const r = [Math.log(f[0] / tg.f[0])]; r.push(tg.on[1] ? Math.log(f[1] / tg.f[1]) : 0); r.push(tg.on[2] ? Math.log(f[2] / tg.f[2]) : 0); const w = 3e-3; r.push(w * (t[1] - nominal[1]), w * (t[2] - nominal[2])); return r; } function solve3(A, g) { const M = A.map((row, i) => [...row, g[i]]); for (let c = 0; c < 3; c++) { let p = c; for (let r = c + 1; r < 3; r++) if (Math.abs(M[r][c]) > Math.abs(M[p][c])) p = r; [M[c], M[p]] = [M[p], M[c]]; for (let r = c + 1; r < 3; r++) { const f = M[r][c] / M[c][c]; for (let k = c; k < 4; k++) M[r][k] -= f * M[c][k]; } } const x = [0, 0, 0]; for (let r = 2; r >= 0; r--) { let s = M[r][3]; for (let k = r + 1; k < 3; k++) s -= M[r][k] * x[k]; x[r] = s / M[r][r]; } return x; } const cost = (r) => r.reduce((a, v) => a + v * v, 0); function lm(start, mat, note, targets, ne) { const tg = partialTargets(note, targets); const nominal = toParams(nominalDesign(note, mat)); let t = toParams(start); let r = residuals(t, start, mat, tg, nominal, ne), c = cost(r); let lam = 1e-3; for (let it = 0; it < 60 && c > 1e-14; it++) { const J = []; for (let k = 0; k < 3; k++) { const tt = [...t]; tt[k] += 1e-6; const rk = residuals(tt, start, mat, tg, nominal, ne); J.push(rk.map((v, i) => (v - r[i]) / 1e-6)); } const A = [0, 1, 2].map((i) => [0, 1, 2].map((j) => J[i].reduce((a, v, q) => a + v * J[j][q], 0))); const g = [0, 1, 2].map((i) => -J[i].reduce((a, v, q) => a + v * r[q], 0)); let improved = false; for (let tries = 0; tries < 12; tries++) { const Ad = A.map((row, i) => row.map((v, j) => v + (i === j ? lam * (A[i][i] + 1e-9) : 0))); const dt = solve3(Ad, g); const step = Math.max(...dt.map(Math.abs)); const s = step > 1.5 ? 1.5 / step : 1; // limit wild steps const tn = t.map((v, i) => v + s * dt[i]); const rn = residuals(tn, start, mat, tg, nominal, ne), cn = cost(rn); if (cn < c) { t = tn; r = rn; c = cn; lam = Math.max(1e-9, lam / 4); improved = true; break; } lam *= 5; } if (!improved) break; } return { design: fromParams(start, t), cost: c }; } /** * Tune a design for a note. Starts from `start` (or the nominal bar) and also from the * nominal bar, keeping the better result. Width, thickness and arch shape are kept. */ export function tuneDesign(note, mat, targets = DEFAULT_TARGETS, start = null, ne = 120) { const nominal = nominalDesign(note, mat); const base = start ? { ...start } : nominal; let best = lm(base, mat, note, targets, ne); if (start && best.cost > 1e-8) { const alt = lm({ ...nominal, W: base.W, h0: base.h0, p: base.p }, mat, note, targets, ne); if (alt.cost < best.cost) best = alt; } // final polish of f1 at full resolution (length only) let b = best.design; for (let k = 0; k < 3; k++) { const f1 = bendFreqs(b, mat, 1)[0]; b = { ...b, L: b.L * Math.sqrt(f1 / noteFreq(note)) }; } return b; }