shaper_calibrate: speed up SHAPER_CALIBRATE smoother fitting ~2x (bit-identical)#914
Open
Hannott wants to merge 1 commit into
Open
Conversation
Contributor
|
Nice speedup — the decomposition is exact (trapz is linear and the Two small things before merge: please add a Signed-off-by trailer to the commit per our commit conventions, and drop the Co-Authored-By/Generated-with attribution from the commit and PR description. The build CI failure is just a Docker Hub pull timeout — rerunning it. |
Hannott
force-pushed
the
shaper-calibrate-speedup-upstream
branch
from
July 14, 2026 18:12
e1e3f7d to
89d1b6b
Compare
Contributor
Author
|
@rogerlz Done |
find_max_accel bisects the max acceleration for each candidate smoother at each test frequency, calling _get_smoother_smoothing ~30 times per bisection (150k+ times for a full autotune). Each call rebuilt a 100-point grid, evaluated the smoother polynomial, and ran 5 trapezoid integrations - all of which depend only on the smoother, not on the accel/scv values the bisection varies. The smoothing offsets are linear in the accel- and scv-dependent terms: offset_180 = (accel/2) * I2 offset_90_x = scv*Jp1 + (accel/2)*Jp2 offset_90_y = scv*Jn1 - (accel/2)*Jn2 where I2/Jp1/Jp2/Jn1/Jn2 are fixed geometry integrals of the smoother. Precompute and cache those per smoother (_calc_smoother_integrals) and evaluate the closed form in _get_smoother_smoothing. Results are bit-identical (max abs difference 2e-16 across all smoothers and a wide accel/scv grid; find_best_shaper produces identical recommendations, vibration, smoothing, and max_accel for every candidate). End-to-end find_best_shaper on synthetic data drops from ~7.5s to ~3.5s - the compute that runs after a resonance sweep to generate the shaper graph, so it directly reduces the post-test wait. Signed-off-by: Åsmund Collin <aakjaergaard@gmail.com>
Hannott
force-pushed
the
shaper-calibrate-speedup-upstream
branch
from
July 14, 2026 19:10
89d1b6b to
1d5dde2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Speeds up the CPU-bound
find_best_shaperautotune that runs after a resonance sweep — the compute that fits shaper candidates and produces the shaper graph (e.g. via Shake&Tune). End-to-end drops ~2.2× (7.5s → 3.5s on synthetic data) with bit-identical results. Pure performance change; self-contained (no dependency on other PRs).The bottleneck
Profiling
find_best_shapershowed ~81% of the time in_get_smoother_smoothing, called ~153,000 times for a full 9-candidate autotune.find_max_accelbisects the max acceleration for each candidate smoother at each test frequency (~30 evaluations per bisection), and every evaluation rebuilt a 100-point grid, evaluated the smoother polynomial, and ran 5 trapezoid integrations — all of which depend only on the smoother, not on theaccel/scvvalues the bisection varies.The fix
The smoothing offsets are linear in the accel- and scv-dependent terms, so they decompose into fixed geometry integrals of the smoother kernel:
_calc_smoother_integralscomputesI2, Jp1, Jp2, Jn1, Jn2(integrals oft²·wandt·wover the kernel — full range and each half) once per smoother and caches them;_get_smoother_smoothingevaluates the closed form. The bisection's ~30 evaluations per frequency now share one geometry computation.The masked half-range integrals (
t≥0/t<0) are split exactly the way the original masked-then-integrate code did, so the decomposition is algebraically exact — not an approximation.Verification
find_best_shaperon synthetic resonance data produces the identical recommended shaper, frequency, vibration, smoothing, and max_accel for every candidate (full result table diff: no differences).ruff check/ruff formatpass.The remaining post-sweep cost is
estimate_smoother's convolution, intentionally left untouched: the only ways to speed it are FFT convolution (changes numerics) or reducing grid resolution (changes results), neither worth trading the exact-results guarantee for.Running ShakeTune CREATE_VIBRATIONS_PROFILE massively reduced calculation time. Tested on RPi 4:
08:37:25 // Machine estimated vibration symmetry: 25.7%
08:36:13 // This may take some time (5-8min)
08:36:13 // Machine vibrations profile generation...
Signed-off-by: Åsmund Collin aakjaergaard@gmail.com