You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.9 KiB
98 lines
3.9 KiB
"""Small DSP helpers (numpy only).""" |
|
import numpy as np |
|
|
|
# ------------------------------------------------------------------ resampling |
|
# Kaiser-windowed sinc interpolator. The kernel is tabulated at PHASES |
|
# fractional offsets per input sample (with linear interpolation between |
|
# rows), so evaluating it costs one gather and a multiply-add per tap instead |
|
# of a Bessel function and a sinc. Table error is ~1e-6, well under the |
|
# kernel's own ~3e-5 interpolation error. |
|
PHASES = 1024 |
|
_TABLES = {} |
|
|
|
|
|
def _kernel_table(cutoff, half_taps, beta): |
|
key = (float(cutoff), half_taps, float(beta)) |
|
if key not in _TABLES: |
|
hw = int(np.ceil(half_taps / cutoff)) # half-width in input samples |
|
offs = np.arange(-hw + 1, hw + 1) |
|
f = np.arange(PHASES + 1) / PHASES |
|
d = f[:, None] - offs[None, :] |
|
w = np.i0(beta * np.sqrt(np.clip(1.0 - (d / hw) ** 2, 0.0, None))) / np.i0(beta) |
|
T = cutoff * np.sinc(cutoff * d) * w |
|
_TABLES[key] = (hw, offs, T, np.diff(T, axis=0)) |
|
return _TABLES[key] |
|
|
|
|
|
def resample_at(x, pos, cutoff=1.0, half_taps=6, beta=8.0, chunk=1 << 16): |
|
"""Evaluate complex sequence ``x`` at fractional sample positions ``pos``. |
|
|
|
``cutoff`` is the low-pass cutoff relative to the Nyquist rate of ``x`` |
|
(1.0 = pure interpolation, 1/osr = anti-alias filter for decimating by |
|
``osr``). Samples outside ``x`` are treated as zero. |
|
""" |
|
x = np.asarray(x, dtype=np.complex128) |
|
pos = np.asarray(pos, dtype=np.float64) |
|
hw, offs, T, D = _kernel_table(cutoff, half_taps, beta) |
|
L = len(x) |
|
# zero padding of 2*hw each side; clipping base to [-hw-1, L+hw-1] keeps every |
|
# tap index in the padded array, and a clipped base only ever sees zeros |
|
xp = np.concatenate([np.zeros(2 * hw, complex), x, np.zeros(2 * hw, complex)]) |
|
base = np.floor(pos) |
|
frac = pos - base |
|
base = np.clip(base.astype(np.int64), -hw - 1, L + hw - 1) |
|
|
|
if len(pos) > 1 and not np.any(frac): |
|
step = base[1] - base[0] |
|
if step > 0 and np.all(np.diff(base) == step): |
|
# uniform integer positions (decimation): one FIR, then pick outputs |
|
v = np.convolve(xp, T[0][::-1], mode="valid") # v[j] = sum_t xp[j+t] T0[t] |
|
return v[base + hw + 1] |
|
|
|
out = np.empty(len(pos), dtype=np.complex128) |
|
ph = frac * PHASES |
|
j = np.minimum(ph.astype(np.int64), PHASES - 1) |
|
w = (ph - j)[:, None] |
|
for a in range(0, len(pos), chunk): |
|
s = slice(a, a + chunk) |
|
taps = T[j[s]] + w[s] * D[j[s]] |
|
idx = base[s, None] + (2 * hw) + offs[None, :] |
|
out[s] = np.einsum("ij,ij->i", xp[idx], taps) |
|
return out |
|
|
|
|
|
def resample_at_reference(x, pos, cutoff=1.0, half_taps=6, beta=8.0, chunk=16384): |
|
"""Direct evaluation of the same kernel, kept as the test reference.""" |
|
x = np.asarray(x) |
|
pos = np.asarray(pos, dtype=np.float64) |
|
hw = int(np.ceil(half_taps / cutoff)) |
|
offs = np.arange(-hw + 1, hw + 1) |
|
i0 = np.i0(beta) |
|
out = np.empty(len(pos), dtype=np.complex128) |
|
for a in range(0, len(pos), chunk): |
|
p = pos[a:a + chunk] |
|
base = np.floor(p).astype(np.int64) |
|
idx = base[:, None] + offs[None, :] |
|
d = p[:, None] - idx |
|
w = np.i0(beta * np.sqrt(np.clip(1.0 - (d / hw) ** 2, 0.0, None))) / i0 |
|
h = cutoff * np.sinc(cutoff * d) * w |
|
ok = (idx >= 0) & (idx < len(x)) |
|
out[a:a + chunk] = np.sum(x[np.clip(idx, 0, len(x) - 1)] * h * ok, axis=1) |
|
return out |
|
|
|
|
|
# ------------------------------------------------------------------ misc |
|
def wrap(x, period): |
|
"""Wrap to [-period/2, period/2).""" |
|
return (np.asarray(x) + period / 2.0) % period - period / 2.0 |
|
|
|
|
|
def jacobsen(X, k): |
|
"""Sub-bin peak offset from complex DFT bins around index ``k`` |
|
(rectangular window). Returns delta in bins, |delta| < ~0.5.""" |
|
M = len(X) |
|
xm, x0, xp = X[(k - 1) % M], X[k], X[(k + 1) % M] |
|
den = 2 * x0 - xm - xp |
|
if den == 0: |
|
return 0.0 |
|
return float(np.real((xm - xp) / den))
|
|
|