BACF Follower
| This API is new and unstable: it may change in any release, without a deprecation, until it matures. |
Overview
The BACF follower tracks the lag at which a signal repeats: its period, or a multiple of it. It is built for a time-domain splicer such as the BACF Pitch Shifter, which splices at how far back the sound repeats, whatever the note. The follower is implemented by a class named bacf_follower.
The follower compares the signal with itself one lag back. Each sample becomes one bit, 1 above zero and 0 below (with a little hysteresis), and the XOR of the latest window’s bits with the bits one lag back marks each sample where the two disagree. Where the waveform matches itself, few bits differ; that lag is where it repeats, its period.
The video shows an E chord, ringing, as the lag slides from 1 to 30 ms. At the top, the input, low-passed at 1 kHz, with the latest window (blue) and the window one lag back (orange). Below it, the two windows overlaid, and under them their bits, one a sample, with the XOR marking in red each sample where they disagree (see Bitstream Autocorrelation). At the bottom, the share of bits that differ at each lag. Most lags leave about half the bits mismatched, the level of chance; where the windows are out of phase, more (89% near 3.6 ms). At 24.3 ms, the period the chord’s notes share, only 8% differ. The low E’s own period, 12.1 ms, leaves 42%, since the other strings stand elsewhere in their cycles.
Sliding the lag across the whole range is a search, and the follower searches only to find a lock. After that it stays on that match as the period drifts: at each zero crossing, leading and trailing, it scores the lag it holds and two samples either side, moves to the best, and takes the fraction of a sample from a parabola through the scores. A crossing is where the waveform’s timing can change, so the follower scores there, and does nothing between crossings.
Twice the period matches too, so the lag may be a period or any multiple of one. Octaves are left unsettled on purpose: a splice at twice the period matches as well as one at the period. When the held lag misses, the follower scores twice the lag.
Low-pass the input first, near the top of its fundamental range (about 1 kHz for a guitar): fewer crossings, and lags that follow the fundamental.
Tracking
-
Lock. Unlocked, each pulse, from a leading crossing to the trailing one, is scored when it ends: against up to three pulses before it of similar height and width, within the lag range. The best match under the threshold, 15% of the window’s bits mismatched, is the new lock.
-
Track. Locked, each crossing first moves the lag to its spacing from the earlier crossing of its kind nearest a lag back, when that is within 3%, so the lock follows a bend. It then scores that lag and its two neighbours either side, and if all of them miss, twice the lag and its neighbours. The best under the threshold is the new lag.
-
Loss. Two misses in a row drop the lock.
lag()then holds the last good lag, fraction and all, until a new lock. -
Drop.
drop()releases the lock on demand, at a new note for instance, so the next lock comes from the new material. The held lag stands in meanwhile.
The lag changes only at a crossing, and tr(s) returns it at every sample while locked, and 0 while unlocked: a client that smooths the lag, or acts on a change, compares it with its last value.
Declaration
class bacf_follower
{
public:
static constexpr float threshold = 0.15f;
static constexpr int max_misses = 2;
static constexpr int sweep = 2;
static constexpr int multiples = 2;
static constexpr int partners = 3;
static constexpr std::size_t edge_capacity = 256;
bacf_follower(
duration min_lag
, duration max_lag
, float sps
, decibel hysteresis = -45_dB
, duration window = 11.6_ms
);
float operator()(float s);
bool locked() const;
float lag() const;
float periodicity() const;
std::size_t window() const;
void drop();
void reset();
};
Expressions
Notation
tr,a,b-
Objects of type
bacf_follower. lo,hi-
Objects of type
duration: the shortest and the longest lag. sps-
Floating point value for samples per second.
h-
Object of type
decibel: the comparator’s hysteresis. w-
Object of type
duration: the scoring window. s-
Floating point value for the latest input sample.
Constructors and Assignment
| Expression | Semantics |
|---|---|
|
Construct a |
|
Copy construct from |
|
Assign |
| C++ brace initialization may also be used. |
The window is rounded to whole words of the bitset, at least one: the default is 512 bits at 44.1 kHz, and at 48 kHz 576 with 64-bit words or 544 with 32-bit ones. The follower allocates at construction only.
Function Call
| Expression | Semantics | Return Type |
|---|---|---|
|
Process the input sample |
|
Accessors
| Expression | Semantics | Return Type |
|---|---|---|
|
Get the tracked lag in samples, with its fraction; unlocked, the last good lag, or 0 before any. |
|
|
Get whether a lag is locked. |
|
|
Get the last score’s match: 1 when the bits repeat exactly, about 0.5 for noise. |
|
|
Get the scoring window, in bits (samples). |
|
Operation
| Expression | Semantics | Return Type |
|---|---|---|
|
Drop the lock; |
|
|
Clear the history and start afresh. |
|
Access
| Expression | Semantics | Return Type |
|---|---|---|
|
Get the most mismatched bits a match may have, as a share of the window: 0.15. |
|
|
Get the misses in a row that drop the lock: 2. |
|
|
Get how far either side of the locked lag each crossing scores, in samples: 2. |
|
|
Get the multiple of the lag tried on a miss: 2. |
|
|
Get the similar pulses an unlocked pulse is scored against: 3. |
|
|
Get the crossings and pulses kept for scoring: 256. |
|
Example
Track the period of a signal, in, at sps samples per second, for lags of 2.5 to 45 ms:
#include <q/pitch/bacf_follower.hpp>
#include <q/fx/biquad.hpp> // For lowpass
#include <q/support/literals.hpp>
#include <span>
...
namespace q = cycfi::q;
using namespace q::literals;
float sps; // samples per second
std::span<float const> in; // the input samples
...
auto lp = q::lowpass{1_kHz, sps}; (1)
auto follower = q::bacf_follower{2.5_ms, 45_ms, sps}; (2)
float lag = 0;
for (auto s : in)
{
if (auto l = follower(lp(s))) (3)
lag = l; (4)
}
| 1 | A 1 kHz low pass in front: fewer crossings, lags that follow the fundamental. |
| 2 | Construct the follower for lags from 2.5 to 45 ms: from the period of a high note to the common period of an open G chord. |
| 3 | Process the low-passed sample; while the follower holds a lock it returns the lag, else 0. |
| 4 | The lag, in samples, a period or a multiple of one. |
Performance
| Measured at 48 kHz in blocks of 32 samples (0.67 ms), on an Apple M2 and on an STM32H723 at 550 MHz with caches on. Average: the time a sample, and its share of a sample period. Worst: the slowest block, and its share of the block’s 0.67 ms; over 100%, it runs late. Figures are rounded and vary with the build and the input. |
Measured tracking an E chord, with the 1 kHz low pass in front and lags of 2.5 to 45 ms:
| Apple M2 | STM32H723 | |
|---|---|---|
A sample, on average |
4.5 ns (0.02%) |
0.21 µs (1%) |
Worst block of 32 samples |
6 to 12 µs (1 to 2%) |
21 µs (3%) |
The worst block comes when a crossing scores its lags: five of them, and five more at twice the lag on a miss. On the M2 the figure is mostly the operating system’s scheduling. The follower allocates about 6 KB at 48 kHz, most of it the bitstream.