Skip to content

Conversation

@jclsn
Copy link
Contributor

@jclsn jclsn commented Feb 4, 2026

Rework of the pitch detection logic

Pitch detection in xwax

if (zero_crossing == true)
    pitch_filter_update(M_PI/2);
else
    pitch_filter_update(0.0);

Every time a zero crossing is detected (which happens four times per cycle of the sinusoid), the filter is updated and else it is not updated at all, because there is no information.

This has the advantage that you can easily mentally abstract movement if not trained in signal processing, but an actual DSP engineer would never do this. If you only do measurements on zero-crossing and otherwise feed 0.0 into the filter, the output is very noisy. Feeding 0.0 into the filter on most samples makes the velocity degrade to then snap back when a zero-crossing is found.

New approach

  1. Turn the signal into clean sinusoids by e.g taking the derivative
  2. Compute the phase difference Δφ from the sine-cosine pair
  3. Feed the phase difference into the Kalman filter to obtain a smooth dΔφ
  4. Compute the instantaneous frequency by
$$f = \frac{d\Delta\Phi}{2\pi}$$

From here on computing the pitch is trivial:

$$pitch = \frac{f}{\mathrm{f}_\mathrm{timecode}}$$

This updates the filter on every sample and achieves faster direction changes. The performance is great and the scratch feeling is way superior!

Some other fixes

  • Tunes the Q and R for the Kalman filter to match the new input
  • Fixes a division by zero bug in the Kalman filter
  • Improves wrapping robustness of the delayline ringbuffer
  • Removes the complex mode switching logic, since it added smoothness at the cost of higher sticker drift
  • Draws the monitor only when a signal threshold of -40dB is exceeded to not draw noise
  • Some minor cleanups and renamings

@jclsn jclsn force-pushed the instantaneous-frequency-detector branch from 7d3dd5b to f9d7042 Compare February 4, 2026 15:15
jclsn added 5 commits February 5, 2026 10:30
- Wrap safely around the ringbuffer, as a cast to size_t of a negative
  number maybe very large
Computes the phase difference of two given samples of a sine and cosine
pair.
It is actually an exponential weighted moving average filter.
@jclsn jclsn force-pushed the instantaneous-frequency-detector branch from 3e02f7a to 46aacb8 Compare February 5, 2026 10:07
jclsn added 4 commits February 5, 2026 11:20
Preparation for the instantaneous frequency detector, which requires the
derivative of the signal to be taken also for non-MK2 timecodes.
Rename struct pitch to struct pitch_filter, as this is more correct.

Also needed to make room for the new tc->pitch value.
Also reorder the values for better readability.
Completely substitutes the xwax legacy pitch detection with an
instantaneous frequency detector that updates the pitch on every sample,
as opposed to only when zero crossings are detected.
@jclsn jclsn force-pushed the instantaneous-frequency-detector branch 2 times, most recently from 388aec7 to c640310 Compare February 5, 2026 14:00
jclsn added 5 commits February 5, 2026 16:44
… coefficients

Testing showed that too many mode switches increase sticker drift. The
new frequency detector also provides smoother output without those
switches.
Without a small dot can be visible when the record is not spinning,
due to the gain compensation of the scaled derivative.
Smoothing to aggresively will oppose reactivity.
Was at 15 fps before. It feels better with 30.
@jclsn jclsn force-pushed the instantaneous-frequency-detector branch from a5d7c34 to 8848807 Compare February 5, 2026 15:45
@jclsn jclsn marked this pull request as draft February 5, 2026 17:43
@jclsn jclsn marked this pull request as draft February 5, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant