T-013 — Spike: fit one piano note end-to-end with a differentiable model in Julia

Status: done (Salamander C4 pending T-014) · Area: julia, piano · Depends on: T-004 (T-014 for real data; a synthetic target works first)

Read for that: Piano 12 (steps 1 and 3), Piano 13 (the resonator the model must match), P-006.

What:

  1. Choose and pin the Julia stack in training/: DSP.jl, FFTW.jl, Optimisers.jl, Enzyme.jl (and Zygote.jl only if Enzyme fails).
  2. A modal note model: partials with , two-slope decays, amplitudes; render one note.
  3. Loss: multi-resolution STFT log-magnitude on the first 1–2 s, plus partial-frequency error.
  4. Fit a synthetic target first (known parameters must be recovered), then one Salamander note (C4, mid velocity) if T-014’s data is there.
  5. Compare gradients through the per-sample recurrence (Enzyme) with the closed-form sum of damped sinusoids: speed, memory, stability.

Done when: a results page section here (recovered vs true parameters, loss curves, timings); the chosen approach written into Piano 12 step 3 and P-006 closed or rescoped.

Results

Code: training/src/NoteFit.jl (submodule InstrumentsTraining.NoteFit, tested in training/test/notefit.jl) and the driver training/scripts/t013_fit_one_note.jl, which prints the tables below (about 6 minutes with 4 threads; argument 0 runs only the renderer comparison):

julia --project=training -t 4 training/scripts/t013_fit_one_note.jl

Machine: the cloud VM of T-011 (Xeon, 4 vCPUs, ±20 % noise), Julia 1.13.4, Float64 throughout; each fit runs on one thread.

1. The stack

PackageUseVerdict
Enzyme.jl 0.13reverse mode through both renderers (the per-sample recurrence included) and the frequency termchosen; works on Julia 1.13; gradients agree with finite differences (test tolerance 1e-3 relative) and with each other to 1e-11
FFTW.jl 1.10STFTs of the loss; peak trackingchosen. Enzyme has no rules for FFTW calls, so the STFT loss has a hand-written adjoint (one inverse FFT per frame, stft_loss_grad!), and the Enzyme pass through the renderer is seeded with it
Optimisers.jl 0.4Adam with a cosine learning-rate decaychosen
DSP.jl—not added: a Hann window and peak picking are a few lines; it comes in with T-006 if resampling or filter design is needed
Zygote.jl—not needed (Enzyme did not fail)

2. Model, loss, parametrisation

  • Model: 30 partials, ; each partial has two components with one frequency: a slow one, , and a prompt one, (so prompt ≥ slow, and the two cannot swap), with a fraction of the amplitude in the slow one, and one free amplitude per partial. That is 37 parameters, all unconstrained (logs, a logit). is held in semitones (12 log2 f0), so one Adam step moves it by lr × 100 cents. With , a step of 0.03 is 52 cents and the fit oscillated.
  • Loss: multi-resolution STFT (N = 512, 1024, 2048, 4096, hop N/4, Hann) over 1.5 s: the mean absolute difference of , with 80 dB below the target’s peak. Added to it: 0.01 × the mean squared cent error between the model’s and the peaks tracked in the target.
  • Peaks (Piano 12 step 1, minimal: measure_partials): one zero-padded Hann spectrum of the 1.5 s. Partials 1–3 are searched near , and each later one near the prediction of a least-squares fit of to the ones found so far. From a guess 40 cents off it tracked all 30 partials and, on its own, gives and to < 0.0001 cent and < 0.001 %.
  • Target: a synthetic C4 rendered in closed form with random phases (the model uses zero phases, so the loss must be phase-blind). Strike position 0.13 (comb notches near n = 8, 15, 23: partial 23 is 47 dB down); slow T60 21 s at the fundamental, prompt T60 3.6 s; white noise at −70 dB.
  • Start: deliberately worse than the analysis: +30 cents, × 2, every decay-law coefficient × 1.5, = 0.5, amplitudes ±6 dB at random. 400 Adam steps, lr 0.03.

3. Recovered vs true parameters

Parametertruestartclosed form, STFT + frecurrence, STFT + frecurrence, STFT only
f0 (cent off)261.63 Hz+30.000-0.000-0.000-0.001
B (rel. error)3.8e-4+100.00 %-0.01 %-0.01 %-0.01 %
α_s (ratio)0.331.5001.0331.0331.188
β_s (ratio)3.0e-81.5001.0021.0021.026
α_d (ratio)1.61.5000.9980.9980.990
β_d (ratio)6.0e-81.5001.0191.0191.097
w (slow fraction)0.250.5000.2550.2550.280
amplitudes, max error (partial n, its level)—5.98 dB (n = 3, -1 dB)0.14 dB (n = 23, -47 dB)0.14 dB (n = 23, -47 dB)0.19 dB (n = 23, -47 dB)
amplitudes, median error—2.80 dB0.01 dB0.01 dB0.05 dB
T60 slow @ 262 Hz20.8 s13.9 s20.1 s20.1 s17.5 s
T60 prompt @ 262 Hz3.57 s2.38 s3.55 s3.55 s3.48 s
T60 slow @ 3000 Hz11.5 s7.7 s11.3 s11.3 s10.3 s
T60 prompt @ 3000 Hz2.52 s1.68 s2.51 s2.51 s2.43 s
wall time——115 s113 s115 s

From this start both losses recover and in 400 steps. With the frequency term the decays come out within 3 %, including the slow T60 of 20 s from only 1.5 s of signal. Without it the slow decay stays 19 % off. The remaining STFT loss of 0.39 is the noise floor plus the phase difference, not misfit (both fits end there).

4. Loss curves

stepclosed form, STFT + frecurrence, STFT + frecurrence, STFT only
1160.6665 (0.9137)160.6665 (0.9137)0.9137 (0.9137)
1036.9440 (0.8084)36.9440 (0.8084)0.8355 (0.8355)
253.1584 (0.6333)3.1584 (0.6333)0.7227 (0.7227)
501.3401 (0.5369)1.3401 (0.5369)0.6169 (0.6169)
1000.5215 (0.4448)0.5215 (0.4448)0.4017 (0.4017)
2000.3932 (0.3932)0.3932 (0.3932)0.3977 (0.3977)
4000.3928 (0.3928)0.3928 (0.3928)0.3930 (0.3930)

Both renderers follow the same path, because their gradients agree to 1e-11.

5. Sweep over starts: the frequency term is needed

start f0start BSTFT + f: f0 error, B error, max amp. errorSTFT only: f0 error, B error, max amp. error
+10 cent× 2.0✗ -2.326 cent, +1.98 %, 1.5 dB✗ -18.658 cent, +107.65 %, 24.3 dB
+10 cent× 0.5✓ -0.000 cent, -0.01 %, 0.1 dB✗ +18.654 cent, -41.81 %, 22.2 dB
+30 cent× 2.0✗ -2.762 cent, +2.49 %, 1.5 dB✗ -14.154 cent, +47.04 %, 25.6 dB
+30 cent× 0.5✓ -0.000 cent, -0.01 %, 0.2 dB✗ +18.654 cent, -41.82 %, 22.8 dB
+60 cent× 2.0✗ -0.741 cent, +0.76 %, 0.8 dB✗ +56.341 cent, +97.87 %, 25.8 dB
+60 cent× 0.5✓ -0.001 cent, -0.01 %, 0.2 dB✗ +15.387 cent, -40.20 %, 22.2 dB
+100 cent× 2.0✗ +5.824 cent, -5.17 %, 2.4 dB✗ +88.696 cent, +95.18 %, 24.2 dB
+100 cent× 0.5✓ -0.001 cent, -0.01 %, 0.1 dB✗ +18.651 cent, -41.81 %, 18.5 dB
-30 cent× 2.0✓ -0.003 cent, -0.01 %, 0.1 dB✗ -14.288 cent, +98.65 %, 24.1 dB
-30 cent× 0.5✓ +0.001 cent, -0.01 %, 0.3 dB✗ -32.889 cent, -61.40 %, 22.0 dB
-60 cent× 2.0✓ -0.000 cent, -0.01 %, 0.2 dB✗ -20.047 cent, +51.24 %, 25.3 dB
-60 cent× 0.5✓ -0.002 cent, -0.01 %, 0.4 dB✗ -36.800 cent, -56.14 %, 21.7 dB

Converged (|f0| < 0.1 cent, |B| < 1 %): with f 8 of 12, STFT only 0 of 12

  • STFT only: 0 of 12 in 200 steps. The fits settle in local minima 15–90 cents and 40–100 % in away, with amplitudes 20+ dB off: once partials sit more than a bin from their target peaks, the log-magnitude loss gives no useful pull. That the 400-step run from +30 cents / × 2 converged (section 3) was luck, not a property of the loss.
  • With the frequency term: 8 of 12 in 200 steps. The other four (positive offsets with × 2) were slow, not stuck: and trade off along a valley. With 400 steps all four converged (within 0.09 cent and 0.08 %; checked separately).
  • So the fit needs both: and initialised from the step-1 peak analysis, which already gives them to < 0.0001 cent on this note, and the frequency term kept in the joint fit.

6. Closed form vs recurrence

Forward pass; the Enzyme reverse pass of the renderer alone (seeded with ones), with its GC-allocated memory; a whole loss-and-gradient step; best of 3. The last column is the difference between the two renderers’ gradients.

SignalRendererforwardEnzyme reverse of the rendererallocated by itloss + gradient (incl. STFT)max rel. gradient difference
1.5 sclosed form34.3 ms158 ms1 MB274 ms—
1.5 srecurrence12.7 ms176 ms1 MB278 ms2.6e-12
3.0 sclosed form70.9 ms330 ms1 MB562 ms—
3.0 srecurrence23.6 ms344 ms1 MB523 ms1.2e-11
10.0 sclosed form199.5 ms1122 ms4 MB1885 ms—
10.0 srecurrence73.1 ms1201 ms4 MB1826 ms3.0e-12

(the STFT loss and its adjoint alone: 101 ms for 1.5 s)

  • Speed: a gradient step costs the same with both renderers (~0.27 s for 1.5 s). The Enzyme reverse pass is 55–65 % of it and the STFT loss with its adjoint ~35 %. The recurrence’s forward pass is 2.5–3× faster than the closed form, but its reverse pass costs 14× its forward pass (closed form: 5×).
  • Memory: the GC-allocated memory of the reverse pass is small and grows with length (1 MB for 1.5 s, 4 MB for 10 s). Enzyme may also keep its cache outside the GC, so this is a lower bound; peak process memory was not measured.
  • Stability: no problem in Float64 up to 10 s (480,000 steps): the two renderers’ gradients still agree to 3e-12.
  • Decision: fit with the recurrence in delta form. It is the same arithmetic as the Rust engine, so golden vectors compare like with like, and the only form that extends to the coupled hammer (stage 2). The closed form stays as the test oracle.

7. Not done here

  • Salamander C4 (item 4, second half) needs T-014’s data, so it moves to T-006. A real note also needs what the synthetic one lacks: beating mode pairs (detuned components), an onset search, a noise and room floor, and phantom partials.
  • Cost: 400 steps on 1.5 s at 48 kHz × 30 partials take ~2 minutes on one thread. A keyboard-wide joint fit over curves (Piano 12 step 3: 30 Salamander notes × 16 velocities) costs ~500× that per step, unless the notes run in parallel (they are independent given the curves) on shorter windows. That is a question for T-006/T-007, not a blocker.