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:
- Choose and pin the Julia stack in
training/: DSP.jl, FFTW.jl, Optimisers.jl, Enzyme.jl (and Zygote.jl only if Enzyme fails). - A modal note model: partials with , two-slope decays, amplitudes; render one note.
- Loss: multi-resolution STFT log-magnitude on the first 1–2 s, plus partial-frequency error.
- Fit a synthetic target first (known parameters must be recovered), then one Salamander note (C4, mid velocity) if T-014’s data is there.
- 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.jlMachine: 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
| Package | Use | Verdict |
|---|---|---|
| Enzyme.jl 0.13 | reverse mode through both renderers (the per-sample recurrence included) and the frequency term | chosen; 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.10 | STFTs of the loss; peak tracking | chosen. 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.4 | Adam with a cosine learning-rate decay | chosen |
| 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
| Parameter | true | start | closed form, STFT + f | recurrence, STFT + f | recurrence, 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.33 | 1.500 | 1.033 | 1.033 | 1.188 |
| β_s (ratio) | 3.0e-8 | 1.500 | 1.002 | 1.002 | 1.026 |
| α_d (ratio) | 1.6 | 1.500 | 0.998 | 0.998 | 0.990 |
| β_d (ratio) | 6.0e-8 | 1.500 | 1.019 | 1.019 | 1.097 |
| w (slow fraction) | 0.25 | 0.500 | 0.255 | 0.255 | 0.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 dB | 0.01 dB | 0.01 dB | 0.05 dB |
| T60 slow @ 262 Hz | 20.8 s | 13.9 s | 20.1 s | 20.1 s | 17.5 s |
| T60 prompt @ 262 Hz | 3.57 s | 2.38 s | 3.55 s | 3.55 s | 3.48 s |
| T60 slow @ 3000 Hz | 11.5 s | 7.7 s | 11.3 s | 11.3 s | 10.3 s |
| T60 prompt @ 3000 Hz | 2.52 s | 1.68 s | 2.51 s | 2.51 s | 2.43 s |
| wall time | — | — | 115 s | 113 s | 115 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
| step | closed form, STFT + f | recurrence, STFT + f | recurrence, STFT only |
|---|---|---|---|
| 1 | 160.6665 (0.9137) | 160.6665 (0.9137) | 0.9137 (0.9137) |
| 10 | 36.9440 (0.8084) | 36.9440 (0.8084) | 0.8355 (0.8355) |
| 25 | 3.1584 (0.6333) | 3.1584 (0.6333) | 0.7227 (0.7227) |
| 50 | 1.3401 (0.5369) | 1.3401 (0.5369) | 0.6169 (0.6169) |
| 100 | 0.5215 (0.4448) | 0.5215 (0.4448) | 0.4017 (0.4017) |
| 200 | 0.3932 (0.3932) | 0.3932 (0.3932) | 0.3977 (0.3977) |
| 400 | 0.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 f0 | start B | STFT + f: f0 error, B error, max amp. error | STFT 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.
| Signal | Renderer | forward | Enzyme reverse of the renderer | allocated by it | loss + gradient (incl. STFT) | max rel. gradient difference |
|---|---|---|---|---|---|---|
| 1.5 s | closed form | 34.3 ms | 158 ms | 1 MB | 274 ms | — |
| 1.5 s | recurrence | 12.7 ms | 176 ms | 1 MB | 278 ms | 2.6e-12 |
| 3.0 s | closed form | 70.9 ms | 330 ms | 1 MB | 562 ms | — |
| 3.0 s | recurrence | 23.6 ms | 344 ms | 1 MB | 523 ms | 1.2e-11 |
| 10.0 s | closed form | 199.5 ms | 1122 ms | 4 MB | 1885 ms | — |
| 10.0 s | recurrence | 73.1 ms | 1201 ms | 4 MB | 1826 ms | 3.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
Float64up 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.